Question

Which Android versions run which Java versions?

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...

 48  52520  48
1 Jan 1970

Solution

 32

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.

2019-01-10

Solution

 21

From the "Supporting Older Versions" AOSP documentation found at https://source.android.com/setup/build/older-versions#jdk:

Supported versions

Android 7.0 (Nougat) – Android 8.0 (Oreo):

Ubuntu: OpenJDK 8

Mac OS X: JDK 8u45 or higher

Android 5.x (Lollipop) – Android 6.0 (Marshmallow):

Ubuntu: OpenJDK 7

Mac OS X: jdk-7u71-macosx-x64.dmg

Android 2.3.x (Gingerbread) – Android 4.4.x (KitKat):

Ubuntu: Java JDK 6

Mac OS X: Java JDK 6

Android 1.5 (Cupcake) – Android 2.2.x (Froyo):

Ubuntu: Java JDK 5

The documentation does not presently (Feb 2022) make any correlation between Android >=9.0 and JDK (that I could find); I suspect this has been overlooked since it does not appear that the setup documentation has been thoroughly updated since sometime prior to the Ubuntu 20.04 LTS release. It does mention that the latest master branch (Android 12.0.0_r32) comes with a prebuilts folder that has the necessary JDK in it.

2022-02-24