How to enable Java 8 language features in Android studio
Now released Android Studio 2.4 Preview 4, it now supports Java 8 language features . Jack is no longer needed and needs to disable Jack to use the improved Java 8 support built into the default toolchain.
Now we need to disable Jack and switch to the default toolchain. How do I enable Java 8 features in an android studio project?
source to share
Enable Java 8 support :
To start using the Java 8 language supported features, update your Android plugin to version 2.4.0-alpha4 (or higher) and add the build.gradle
following file to your modules :
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Disable jackOptions :
We can disable Jack and switch to the default toolchain by removing the jackOptions block from the build.gradle modules files:
android {
...
defaultConfig {
...
// Remove this block.
jackOptions {
enabled true
}
}
}
Note. If your project uses Jack, Retrolambda, or DexGuard, then by default Android studio uses the Java 8 support provided by that tool.
Disable Java 8 support:
We can also disable Java 8 features in your project if you encounter any problem related to Java 8. We can update the file gradle.properties
by adding below line to disable Java 8 features:
android.enableDesugar=false
Check Using Java 8 Language Features for more details on Java 8 features.
source to share
I know this has already been answered, but after new update Gradle and android studio is jackOptions
deprecated.
android {
.....
defaultConfig {
..........
//remove jackOptions and add
android.compileOptions.sourceCompatibility 1.8
android.compileOptions.targetCompatibility 1.8
}
// Keep the following configuration in order to target Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
source to share