Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
pRaNaY Avatar asked Apr 15 '17 06:04

pRaNaY


People also ask

Can I use Java 8 in Android Studio?

Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still it doesn't have invokedynamic, only the new syntax sugar. If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba.

Which Java version do I need for Android studio?

Since Android apps are written in Java, you will need the Oracle Java compiler and libraries on your system. These are collectively called the Java Development Kit or "JDK" for short. (If you are certain that you already have JDK 1.8 or higher on your computer, such as from taking CS 106A, you can skip to Step 2.)

Which Java version is used in Android?

Current versions of Android use the latest Java language and its libraries (but not full graphical user interface (GUI) frameworks), not the Apache Harmony Java implementation, that older versions used. Java 8 source code that works in latest version of Android, can be made to work in older versions of Android.


Video Answer


4 Answers

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.

like image 95
pRaNaY Avatar answered Oct 26 '22 06:10

pRaNaY


I know this has been already answered, but after the new Gradle and android studio update, jackOptions is 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
       }
  }
like image 33
Bishoy Kamel Avatar answered Oct 26 '22 04:10

Bishoy Kamel


Simple process -

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

Select -

Source Compatibility - 1.8
Target Compatibility - 1.8
like image 29
Paresh P. Avatar answered Oct 26 '22 06:10

Paresh P.


Clean answer-

Just add following to app level build.gradle and Sync

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
like image 1
Khemraj Sharma Avatar answered Oct 26 '22 04:10

Khemraj Sharma