Skip to content

Most visited

Recently visited

navigation

Java 8 Language Features

Android N introduces support for Java 8 language features that you can use when developing apps that target Android N. This page describes the new language features supported in the Android N Preview, how to properly set up your project to use them, and any known issues you may encounter.

To start using these features, you need to download and set up Android Studio 2.1 and the Android N Preview SDK, which includes the required Jack toolchain and updated Android Plugin for Gradle. If you haven't yet installed the Android N Preview SDK, see Set Up to Develop for Android N.

Note: Using the new Java 8 language features is not a requirement for developing apps that target the Android N platform. If you don't want to write code with Java 8 language features, you can keep your project's source and target compatibility values set to Java 7, but you still must compile with JDK 8 to build against the Android N platform.

Supported Java 8 Language Features and APIs

Android does not currently support all Java 8 language features. However, the following features are now available when developing apps targeting the Android N Preview:

Note: Type annotation information is only available at compile time, and not during runtime.

To test lambda expressions, method references, and type annotations on earlier versions of Android, go to your build.gradle file, and set compileSdkVersion and targetSdkVersion to 23 or lower. You will still need to enable the Jack toolchain to use these Java 8 features.

Additionally, the following Java 8 language feature APIs are now available:

Enabling Java 8 Features and the Jack Toolchain

In order to use the new Java 8 language features, you need to also use the new Jack toolchain. This new Android toolchain compiles Java language source into Android-readable dex bytecode, has its own .jack library format, and provides most toolchain features as part of a single tool: repackaging, shrinking, obfuscation and multidex.

Here is a comparison of the two toolchains used to build Android DEX files:

  • Legacy javac toolchain:
    javac (.java --> .class) --> dx (.class --> .dex)
  • New Jack toolchain:
    Jack (.java --> .jack --> .dex)

Configuring Gradle

To enable the Java 8 language features and Jack for your project, enter the following in your module-level build.gradle file:

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Known Issues

Instant Run does not currently work with Jack and will be disabled while using the new toolchain.

Because Jack does not generate intermediate class files when compiling an app, tools that depend on these files do not currently work with Jack. Some examples of these tools are:

  • Lint detectors that operate on class files
  • Tools and libraries that require the app’s class files (such as instrumentation tests with JaCoCo)

If you find other problems while using Jack, 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.