Reputation: 303
For example java.nio.file.FILES was added in api 26. So if I compile my app against api 26 can I use that same class in android versions older than api 26? In original JVM, bytecodes from higher jdk can't be run on lower jre, so that is what worries me.
Upvotes: 3
Views: 132
Reputation: 6648
When my configured minimium SDK is lower than that required to use some feature, like java.nio.file.FILES
I will get a warning from the Lint component of Android Studio, or IntelliJ. It will advise I annotate the method containing the new feature. NoSuchMethodError
is thrown when a method from a more recent SDK (API 26) is called from a lower OS (say API 19).
@RequiresApi(Build.VERSION_CODES.KITKAT) public void foo() {}
if (Build.VERSION.SDK_INT >= 19) { foo(); } else { bar(); }
You can switch
to a branch using the newer code if the deployment OS is sufficient by reading https://developer.android.com/reference/android/os/Build.VERSION_CODES
See https://stackoverflow.com/a/38638312/866333
import java.lang.reflect.Field; Field[] fields = Build.VERSION_CODES.class.getFields(); String osName = fields[Build.VERSION.SDK_INT + 1].getName(); Log.d("Android OsName:",osName);
In terms of JDK support, https://developer.android.com/studio/write/java8-support, indicates that a subset of Java 8 features, such as Lambda expressions are usable on "Any" SDK (API level) so long as you are using Android Studio 3. However, most of the Java 8 goodness, sadly, seems to require API 24.
Upvotes: 1