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?

+2


source to share


5 answers


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.

+11


source


Simple process -

Right click on Project > Open Module Setting (F4) > Modules (app) >

      



Choose -

Source Compatibility - 1.8
Target Compatibility - 1.8

      

+2


source


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
       }
  }

      

0


source


Clear answer-

Just add the following at the application level build.gradle

andSync

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

      

0


source


android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

      

And enable jackOption in one module

 defaultConfig {
            jackOptions {
            enabled true
        }
    }

      

0


source







All Articles