Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Language Level Java 8 (Retrolambda) on AndroidStudio 4+?

So, someone backported Lambdas for Java 8 back to Java 6, and 7. Apparently it also works for Android. The project is called Retrolambda. I wanted to play around with this on AndroidStudio, but it seems in recent version they have removed the ability to set Java 8 as the Language Level (so I don't get compile errors while using Lambdas). Does anyone know a way around this? I imagine it'd have something to do with the fact that AndroidStudio is a modified IntelliJ. Any help would be appreciated.

like image 947
spierce7 Avatar asked Jan 02 '14 07:01

spierce7


People also ask

Can I use Java 8 in Android Studio?

Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.

Which version of Java is used in Android Studio?

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

Can I use JDK 11 for Android studio?

Up to Android Studio 3.5 beta 2 one could use JDK11 for compiling project by configuring it in Project Structure -> SDK Location -> JDK Location. This way Android Studio is run under bundled JDK8 but Gradle is using selected JDK11. Starting from Android Studio 3.5 beta 3 this setup is no longer supported.

Can I use Java 17 with Android studio?

As per the docs, JDK 17 isn't supported yet in Android Studio.


2 Answers

I've found that you can set the "language level" to Java 8 in Android Studio by modifying the build.gradle to include the following:

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
like image 171
Jason Voegele Avatar answered Oct 10 '22 14:10

Jason Voegele


It is removed on new project, but it is available in the properties after the project is created. Note that I haven't actually compiled with any new Java 8 features yet, so let's hope that works.

Right click on your project, go to Open Module Settings. I think there's a bug somewhere, as once I navigate away from those language settings tab, I can never get back to it.

To work around that, you can edit MyProject/.idea/misc.xml and look for the languageLevel attribute.

To enable Project settings once you navigate away, edit MyProject/.idea/workspace.xml and search for project.structure.last.edited. Set the value to "Project".

like image 39
joe Avatar answered Oct 10 '22 13:10

joe