Android supports all Java 7 language features and a subset of Java 8 language features that vary by platform version. This page describes the new 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.
Support for Java 8 language features requires a new compiler called Jack. Jack is supported only on Android Studio 2.1 and higher. So if you want to use Java 8 language features, you need to use Android Studio 2.1 to build your app.
If you already have Android Studio installed, make sure you update to the latest version by clicking Help > Check for Update (on Mac, Android Studio > Check for Updates). If you don't already have the IDE installed on your workstation, download Android Studio here.
Supported Java 8 Language Features and APIs
Android does not support all Java 8 language features. However, the following features are available when developing apps targeting Android 7.0 (API level 24):
- Default and static interface methods
- Lambda expressions (also available on API level 23 and lower)
- Repeatable annotations
- Method References (also available on API level 23 and lower)
- Type Annotations (also available on API level 23 and lower)
Note: Note: Type annotation information is available at compile time, but not
at runtime. Also, the platform supports TYPE
in API 24 and below, but not ElementType.TYPE_USE or
ElementType.TYPE_PARAMETER..
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 APIs are also available:
- Reflection and language-related APIs:
-
java.lang.FunctionalInterface -
java.lang.annotation.Repeatable -
java.lang.reflect.Method.isDefault() - and Reflection APIs associated with repeatable annotations, such as
AnnotatedElement.getAnnotationsByType(Class)
-
- Utility APIs:
Enable Java 8 Features and the Jack Toolchain
In order to use the new Java 8 language features, you need to also use the
Jack toolchain.
This new Android toolchain compiles Java language sources 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)
Configure Gradle
To enable 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 issues while using Jack, please file a bug.