• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Does Android Studio support Java 14? if so how....

Nightpoison

Newbie
I want to use one of the new features of Java 14, Records. I want to know if android studio would support java 14, and if so how do I setup Android studio to reference the java 14 jdk.

I know I can update the JDK Location by going to file>project structure> SDK Location and down to JDK Location. But it seems to default to the Android studio jre. If I update this to my installation of Java 14, how will this affect my current applications in Android Studio? Is this where I need to point to the java jdk?

I attempted to update this location but it says to choose a valid jdk 8 directory.

Thoughts?
 
Here's my understanding:
There's two things going on here when you ask this question:
1.) What jdk version of code runs on an android?, and
2.) What jdk version can you use to build your android code on Android Studio

Android itself supports Java 1.7 and Java 1.8. Technically speaking it supports only a subset of Java 1.7, and Java 1.8. For example, it doesn't support Java swing.

Android studio technically only support's up to Java 11. In other words it only supports class file format major version 55. Java 14 produces class file format 58. I'm not sure what the technical differences are between those two class formats, but there are some differences else Oracle wouldn't have given it a different version number. That said, I have been building Android projects using Java 14, and I have not had any problems doing that and that's because I target java 1.8 which means I use a Java 14 compiler to produce Java 1.8 code. You will get a gradle build warning: D8: "One or more classes has class file version >= 56 which is not officially supported" if you produce java code with a class file version is greater than 55 (which is java 11). You should also be careful with 3rd party library's that you use if they have been compile with a java compiler greater than java 11. I'm not sure if you get warning for library dependencies that have been compiled with a version greater than java 11. I believe you'll get some weird issue if you use language features not compatible with java 1.8.

The important thing is that in your "android" gradle build block, you should specify source and target jvm to be 1.7 or 1.8. That said, I've seen folks in other forums who showed their build.gradle scripts set these to higher values. I have questioned at least one person whether this works, and got the response back that they have not had any issues. That said, it just the same thing as me using Java 14 for android studio. Things work as long as you don't use features of java not available on your target machine. The problem of course is what features might break things? The answer is that nobody seems to know for sure. If it works, your good to go, and if it doesn't you might have a hard time diagnosing problems that you run into.

I prefaced this answer with "Here's my understanding". So, if you read this this and find something I'm saying is wrong, please add to this posted question. Configuration and compatibility are some of the hardest issues with have to deal with as developers. In addition, things keep evolving, which compounds the problem. Things said today, might not be correct in a year or month from now!
 
Last edited:
I've done some more investigation and now have a slightly different understanding. The version of Gradle that you use determines the max version of java that you can use for your gradle builds. See docs.gradle.org/current/userguide/compatibility.html. For example, I'm using gradle 7.4 and I use Java 14 to build my custom gradle plugins. The version of java that you use for your gradle builds has nothing to do with the version of java that android supports. It's the combination of the version of your android gradle plugin and your android sdk that determines what java language features you can use. Your andoid/SDK/build-tools directory contains a D8.bat file. D8 is the build tool used for converting java into dex. That batch file finds an appropriate java jdk to run the d8.jar file also contain in your build-tools directory. I'm not sure if this program takes java files as input or java class files, but I think it's the later. In my case, the java jdk it uses to run the d8.jar is a java 8 compiler. That compiler (jdk version) gets set up by Android Studio. Be aware that newer versions of Android Studio could embed a different compiler. I'm using Android Studio 2021.2.1, which is the most current version at the time of this posting. That warning message D8: "One or more classes has class file version >= 56 which is not officially supported", comes from that process. So essentially, that process seems to be saying that it ownly reliably supports java class file formats up to verion 55 which is of course Java 11. That said, android itself currently only supports Java 7 in its entirety and a subset of java 8. Here's a link to the subset of java 8 features that android supports: Use Java 8 language features and APIs | Android Developers. That link says that in order to those java 8 features you must set sourceCompatibility and targetCompatibility to Java 1.8. My guess is that if you set those options to a higher value it treats it like you set it to 1.8; it doesn't give you any errors; and it just confuses everyone as to what that means since most of us provide configuration data by example of what others have done. Again, that's just a guess. So what that D8 program does, is to take your input and builds dex code that is compatible to run on android. So if you use, some features that are not java 7 or part of the subset of java 8 that android supports, it apparently figures out a way to do that, perhaps with the addition of some added support code which may vary depending on the verion of the SDK version you're targeting. This process is known as desugaring. I'm going to guess that you'll get some kind of error if you provide a language feature that the d8 program doesn't support. That's probably why android documentation is pretty vague on the issue of java compatibility; it depends on your configuration.
 
Last edited:
Back
Top Bottom