Java on Android
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.
Java 8 features subset with Retrolambda
Section titled “Java 8 features subset with Retrolambda”Retrolambda lets you run Java 8 code with lambda expressions, method references and try-with-resources statements on Java 7, 6 or 5. It does this by transforming your Java 8 compiled bytecode so that it can run on an older Java runtime.
Backported Language Features:
- Default methods are backported by copying the default methods to a companion class (interface name + "$") as static methods, replacing the default methods in the interface with abstract methods, and by adding the necessary method implementations to all classes which implement that interface.
- Static methods on interfaces are backported by moving the static methods to a companion class (interface name + "$"), and by changing all methods calls to call the new method location.
Known Limitations:
Retrolambda gradle plugin will automatically build your android project with Retrolambda. The latest version can be found on the releases page.
Usage:
- Download and install jdk8
- Add the following to your
build.gradle
buildscript { repositories { mavenCentral() }
dependencies { classpath 'me.tatarka:gradle-retrolambda:<latest version>' }}
// Required because retrolambda is on maven centralrepositories { mavenCentral()}
apply plugin: 'com.android.application' //or apply plugin: 'java'apply plugin: 'me.tatarka.retrolambda'
android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}Known Issues:
retrolambda { jvmArgs '-noverify'}