Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Android versions run which Java versions?

Tags:

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

like image 899
Andreas Avatar asked Jan 10 '19 13:01

Andreas


People also ask

Which version of Java is used for Android?

Class libraryJava 8 source code that works in latest version of Android, can be made to work in older versions of Android.

Does Android work with Java 11?

Android SDK tools are not yet compatible with Java 11 and need Java 8 (1.8) to run correctly. The Bitrise stacks provide both Java 8 and Java 11, with Java 8 being active by default. If you need a Java 11 runtime for your project to compile, you can switch to Java 11 only before it's needed by the next Step.

What version of Java does Android 11 use?

Java 8 APIs are available starting with Android 8 (API level 26). Some Java 9 APIs (like List. of()) are available starting with Android 11 (API level 30).

Is Java 1.8 the same as Java 8?

In short – 8 is product version number and 1.8 is the developer version number (or internal version number). The product is the same, JDK 8, anyways.


2 Answers

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.

like image 94
CommonsWare Avatar answered Sep 29 '22 19:09

CommonsWare


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.

like image 20
claypooj Avatar answered Sep 29 '22 19:09

claypooj