Issue
I've just tried to run my app compiled using Java 8 on an Android 4.0 device. While I'm used to taking great care to look at the Android API levels in the Android documentation to make sure I'm only using APIs that are available on Android 4.0, I'm not so used to making sure I'm not using any features in Java itself that aren't available on Android 4.0.
Consider the following code, it tries to import the initializeScrollbars()
API from View
class because, for whatever reason, it has been removed from the official SDK:
try {
final Method initializeScrollbars = android.view.View.class.getDeclaredMethod("initializeScrollbars", TypedArray.class);
initializeScrollbars.invoke(this, a);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
While this code works fine on my Android 8.0 test system, it doesn't work on Android 4.0. The error is:
Could not find method java.lang.ReflectiveOperationException.printStackTrace
After some research I found out that ReflectiveOperationException
is not available before Java 7 and so, apparently, Android 4.0 does not support Java 7.
This makes me wonder: Is there an overview which clearly shows which Android versions come with which Java version? e.g. how can I find out the first Android version that supports Java 7? And how can I find out the first Android version that supports Java 8?
This really must be easy to find but I'm just failing to see it. Googling always leads to results in which people are asking about the Java versions supported by Android Studio, not by Android itself.
So, can anybody please shed some light onto this? I know it must be somewhere really obvious, but I don't seem to find it...
Solution
and so, apparently, Android 4.0 does not support Java 7.
By your definition, Android does not support any version of Java. The java
and javax
classes in the Android SDK do not exactly match any version of Java, whether a numerical version (e.g., 6, 7, 8) or whatever you want to consider Java SE/EE/ME to be.
Is there an overview which clearly shows which Android versions come with which Java version
In terms of language features (e.g., lambda expressions), quoting the documentation:
Android Studio 3.0 and later supports all Java 7 language features and a subset of Java 8 language features that vary by platform version
Here, "Android Studio" is really referring to the build tools that compile your source code and create the Dalvik bytecode. Current tools can support all Java 7 and a few Java 8 features on all versions of Android. Additional Java 8 features are only available on API Level 24+, usually because they rely upon certain classes that were only added to the Android SDK at that point.
But your concern seems to be with classes and methods, in which case there is no simple mapping of any Java version to any Android version.
Moreover, you are using reflection to hack into framework classes, which means your results will not only vary by Android version but by device model, as device manufacturers can and do change the implementation of framework classes.
Answered By - CommonsWare
Answer Checked By - Cary Denson (JavaFixing Admin)