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.
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.
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.
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.
As per the docs, JDK 17 isn't supported yet in Android Studio.
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
}
}
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With