Skip to content

Most visited

Recently visited

navigation

Use Java 8 language features

Android Studio 3.0 Preview 1 and later supports all Java 7 language features and a subset of Java 8 language features that vary by platform version. This page describes the Java 8 language features you can use, how to properly configure your project to use them, and any known issues you may encounter.

Note: When developing apps for Android, using Java 8 language features is optional. You can keep your project's source and target compatibility values set to Java 7, but you still need to compile using JDK 8.

Android Studio now provides built-in support for using certain Java 8 language features and third-party libraries that use them. As shown in figure 1, the default toolchain implements the new language features by performing bytecode transformations, called desugar, on the output of the javac compiler. Jack is no longer supported, and you should first disable Jack to use the improved Java 8 support built into the default toolchain.

Figure 1. Java 8 language feature support using desugar bytecode transformations.

To start using supported Java 8 language features, update the Android plugin to 3.0.0-alpha1 (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
  }
}

Note: If Android Studio detects that your project is using Jack, Retrolambda, or DexGuard, the IDE uses Java 8 support provided by those tools instead. However, consider migrating to the default toolchain.

Supported Java 8 Language Features and APIs

Android Studio does not support all Java 8 language features, but more are being added in future releases of the IDE. Depending on which minSdkVersion you’re using, certain features and APIs are available now, as described in the table below.

Java 8 Language Feature Compatible minSdkVersion
Lambda expressions Any. However, lambda serialization is supported only when all values captured by the lambda are serializable.
Method References Any.
Type Annotations Any. However, type annotation information is available at compile time, but not at runtime. Also, the platform supports TYPE in API level 24 and below, but not ElementType.TYPE_USE or ElementType.TYPE_PARAMETER.
Default and static interface methods Any.
Repeating annotations Any.
Java 8 Language API Compatible minSdkVersion
java.lang.annotation.Repeatable API level 24 or higher.
AnnotatedElement.getAnnotationsByType(Class) API level 24 or higher.
java.util.stream API level 24 or higher.
java.lang.FunctionalInterface API level 24 or higher.
java.lang.reflect.Method.isDefault() API level 24 or higher.
java.util.function API level 24 or higher.

In addition to the Java 8 language features and APIs above, Android Studio 3.0 Preview 1 and later extends support for try-with-resources to all Android API levels.

Migrate to the default toolchain

If Android Studio detects that your project is using Jack, Retrolambda, or DexGuard, the IDE uses Java 8 support provided by those tools instead. However, compared to the default toolchain, those tools lack some functionality and support. So follow the instructions in this section to migrate to Android Studio's default toolchain.

Migrate from Jack

The Jack toolchain is deprecated, as per this announcement. If your project depends on Jack, you should migrate to using Java 8 support built into Android Studio’s default toolchain. Using the default toolchain also includes support for third-party libraries that use Java 8 language features, Instant Run, and tools that depend on intermediate .class files.

To disable Jack and switch to the default toolchain, simply remove the jackOptions block from your module’s build.gradle file:

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

    // Keep the following configuration in order to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Migrate from Retrolambda

Compared to Android Studio's default toolchain, Retrolambda lacks support for third party libraries that use Java 8 language features. To migrate to the default toolchain, remove the Retrolambda dependency from your project-level build.gradle file:

buildscript {
  ...
   dependencies {
      // Remove the following dependency.
      classpath 'me.tatarka:gradle-retrolambda:<version_number>'
   }
}

And remove the Retrolambda plugin and retrolambda block from each module's build.gradle file:

// Remove the following plugin.
apply plugin: 'me.tatarka.retrolambda'
...
// Remove this block after migrating useful configurations.
retrolambda {
    ...
    // If you have arguments for the Java VM you want to keep,
    // move them to your project's gradle.properties file.
    jvmArgs '-Xmx2048m'
}

Disable Support for Java 8 Language Features

If you’re experiencing issues related to the new support for Java 8 language features, you can disable it by adding the following to your gradle.properties file:

android.enableDesugar=false

To help us improve support for Java 8, please file a bug.

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.