androidandroid-studiobuild.gradleandroid-studio-2.4jack-toolchain

How to enable Java 8 language features in Android studio


Now release with Android Studio 2.4 Preview 4, it is now supported Java 8 language features. Jack is no longer required, and need 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 enable Java 8 features to use in the android studio project?


Solution

  • Enable Java 8 Support:

    To start using supported Java 8 language features, update the Android plugin to 2.4.0-alpha4 (or higher) and add the following to your module’s build.gradle file:

    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 module’s build.gradle file:

    android {
        ...
        defaultConfig {
            ...
            // Remove this block.
            jackOptions {
                enabled true
            }
        }
    
    }
    

    Note: If your project is using Jack, Retrolambda, or DexGuard, then Android studio default uses Java 8 support provided by those tool.

    Disable Java 8 Support:

    We can also disable Java 8 features in your project in case you are facing any issue related Java 8. We can update gradle.properties file by adding below line to disable Java 8 features:

    android.enableDesugar=false
    

    Check Use Java 8 language features for more details about Java 8 features.