Java 8 features in Android Studio are enabled from the Android module’s Gradle file. Modern Android builds use the default D8/R8 toolchain and desugaring, so you do not need the old Jack toolchain. You can keep writing ordinary Java code, but when your app or one of its dependencies uses Java 8 language features, the app module should declare Java 8 source and target compatibility.

The older Android Studio 3.0 preview guidance still matters for legacy projects: Jack was deprecated and should be removed. For current Android Studio projects, the important difference is between Java 8 language features such as lambda expressions and Java 8 library APIs such as java.time, java.util.stream, and java.util.Optional. Language features are enabled with compileOptions. Many Java 8+ library APIs on older Android versions need core library desugaring as an additional step.

Use Java 8 features in Android Studio - www.tutorialkart.com
Use default Java 8 features in Android Studio

Java 8 support in Android Studio: language features, APIs, and JDK are different

Before changing Gradle files, separate these three ideas. They are related, but they do not mean the same thing in an Android project.

Android Java settingWhat it controlsCommon example
sourceCompatibilityWhich Java syntax the Java compiler accepts.Lambda expressions and method references.
targetCompatibilityThe Java bytecode level generated for Java source.Keep it aligned with sourceCompatibility.
Core library desugaringWhether selected newer Java library APIs are rewritten so they can run on older Android releases.java.time.LocalDate, java.util.stream.Stream, and Optional.
JDK used by GradleThe JDK that runs Android Studio or Gradle and compiles the project.Often a newer JDK such as the JetBrains Runtime bundled with Android Studio.

For the official compatibility details, refer to Android Developers pages on Java 8 language features and APIs, the Java 8+ API desugaring support table, and Java versions in Android builds.

Remove Jack before using Java 8 features in Android Studio

To disable Jack toolchain from your Android Application, remove the jackOptions snippet from defaultConfig block in build.gradle file.

In a legacy project, search the app module’s build.gradle file for jackOptions. If you find a block like the following, remove it rather than trying to combine Jack with the default toolchain.

</>
Copy
android {
    defaultConfig {
        // Remove this old Jack configuration if it exists.
        // jackOptions {
        //     enabled true
        // }
    }
}

Enable Java 8 language features in app build.gradle

To start using Java 8 features in your Android Application, update Android plugin to 3.0.0-alpha1 or higher and add the following compileOptions to build.gradle file in your app folder.

</>
Copy
android {
    . . .
    defaultConfig {
        . . . 
    }
    buildTypes {
        . . .
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

The same idea in a current Kotlin DSL module file, build.gradle.kts, is written with assignment syntax.

</>
Copy
android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

If your module also contains Kotlin source files, keep the Kotlin JVM target consistent with the Java target used by the module. The exact Kotlin configuration can vary by Android Gradle Plugin and Kotlin plugin version, so check the syntax used in your project before editing.

Java 8 language features supported on Android API levels through desugaring

Following are the Java 8 features supported by all sdk versions (you may keep any minSdkVersion) :

  • Lambda expressions. Android does not support serializing lambda expressions, so avoid relying on serialized lambdas.
  • Method references.
  • Type annotations. Type annotation information is generally compile-time information; do not depend on it as a runtime feature on old Android releases.
  • Default and static interface methods.
  • Repeating annotations.
  • try-with-resources support extended to all Android API levels by the Android Gradle Plugin.

Here is a small Java 8 syntax example that compiles when Java 8 language features are enabled.

</>
Copy
import java.util.Arrays;
import java.util.List;

public class Java8Demo {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("ant", "bee", "cat");

        names.forEach(name -> System.out.println(name.toUpperCase()));
    }
}
ANT
BEE
CAT

Use Java 8 APIs such as Stream, Optional, and java.time on older Android versions

And following are the Java 8 APIs that are supported with a minSdkVersion of 24 (or higher) :

  • java.lang.annotation.Repeatable
  • AnnotatedElement.getAnnotationsByType(Class)
  • java.util.stream
  • java.lang.FunctionalInterface
  • java.lang.reflect.Method.isDefault()
  • java.util.function

For modern Android builds, you can often use many Java 8+ APIs on devices below API 24 by enabling core library desugaring. This is separate from the simple sourceCompatibility and targetCompatibility setup above. Use it when your code or a library calls Java library APIs that are not available on your app’s lower Android versions.

Groovy DSL example for build.gradle:

</>
Copy
android {
    defaultConfig {
        // Required only when minSdkVersion is 20 or lower.
        multiDexEnabled true
    }

    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
}

Kotlin DSL example for build.gradle.kts:

</>
Copy
android {
    defaultConfig {
        // Required only when minSdk is 20 or lower.
        multiDexEnabled = true
    }

    compileOptions {
        isCoreLibraryDesugaringEnabled = true
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")
}

Check the official Android desugaring table before changing the dependency version. The version of desugar_jdk_libs must be compatible with the Android Gradle Plugin version used by your project.

When Java 8 code still fails in Android Studio

If Java 8 syntax or APIs still fail after adding the Gradle settings, check these common causes before changing app code:

  • The compileOptions block was added to the root Gradle file instead of the app module that contains the Java code.
  • A library module uses Java 8 APIs but does not have the same Java 8 or desugaring configuration.
  • The project uses java.util.stream, java.time, or Optional on old Android versions without core library desugaring.
  • The project uses unsupported signature-polymorphic methods such as MethodHandle.invoke or MethodHandle.invokeExact while targeting a lower Android version.
  • Gradle is running with a different JDK on the command line than the one configured inside Android Studio.

If you experience any issues with the latest Java 8 features, you may disable it by disabling Desugar in your gradle.properties file as shown in the following.

android.enableDesugar=false

That property is an old troubleshooting switch from early desugar support. In a current project, do not add it as a normal fix, because it disables the Java 8 feature support you are trying to use. Prefer fixing the module Gradle configuration, Android Gradle Plugin version, or desugaring dependency instead.

Java 8 Android Studio setup checklist

  • Remove any old jackOptions block from the app module.
  • Add sourceCompatibility and targetCompatibility to every Android module that uses Java 8 language features.
  • Add core library desugaring only when Java 8+ library APIs must run on older Android versions.
  • Use a desugar_jdk_libs version that matches your Android Gradle Plugin version.
  • Test the app on the lowest Android version supported by minSdkVersion, not only on the latest emulator.

FAQs on Java 8 features in Android Studio

Does Android use Java 8?

Android apps can be written with Java 8 language features when the Android Gradle Plugin and module Gradle settings support them. Android devices do not run a normal desktop JVM; Android builds compile Java or Kotlin code into DEX code for Android Runtime.

How do I enable Java 8 in Android Studio?

Add compileOptions with sourceCompatibility JavaVersion.VERSION_1_8 and targetCompatibility JavaVersion.VERSION_1_8 in the app module’s Gradle file. If you use Java 8 library APIs on old Android versions, also enable core library desugaring and add the desugar_jdk_libs dependency.

Why does java.util.stream fail even after enabling Java 8 syntax?

java.util.stream is a Java library API, not just syntax. On devices where that API is not part of the Android platform, you need core library desugaring or a higher minimum Android API level.

Is Jack required for Java 8 features in Android Studio?

No. Jack was deprecated. Android Studio and the Android Gradle Plugin use the default toolchain with desugaring, so legacy jackOptions should be removed from old projects.

Is Java 8 no longer supported for Android development?

Java 8 language compatibility is still commonly configured in Android projects. The separate question of public support for a desktop Java 8 JDK is not the same as whether Android Gradle builds can compile Java 8 source. For Android projects, follow the Java version guidance for your Android Gradle Plugin, Gradle, and JDK.

Java 8 features in Android Studio setup summary

In this Android Tutorial, we have learnt a way for how to use Java 8 features in Android Application development. For a legacy Android Studio project, remove Jack and add Java 8 compileOptions in the app module. For a modern project that uses Java 8 APIs such as java.time, Stream, or Optional on older Android versions, also enable core library desugaring and test on your lowest supported API level.