Install Gradle Retrolambda using the plugins DSL
mainFor Gradle 2.1+, you can use the modern plugins syntax to apply the Retrolambda plugin:
plugins {
id "me.tatarka.retrolambda" version "3.7.1"
}repository·main·Indexed 26 days ago
https://github.com/evant/gradle-retrolambdaA Gradle plugin that enables Java 8 lambda expressions in Java 6 or 7 projects, specifically targeting Java and Android environments by leveraging the retrolambda tool. It supports configuration for target Java versions, JVM arguments, and incremental processing, and provides integration guidance for Android Studio and Proguard.
For Gradle 2.1+, you can use the modern plugins syntax to apply the Retrolambda plugin:
plugins {
id "me.tatarka.retrolambda" version "3.7.1"
}To use Retrolambda in your Java or Android project, follow these steps:
build.gradle file.Requirements:
1.5.02.5Note: You must ensure mavenCentral() is included in both your buildscript repositories and your project repositories because retrolambda is hosted on Maven Central.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.7.1'
}
}
// Required because retrolambda is on maven central
repositories {
mavenCentral()
}
apply plugin: 'com.android.application' //or apply plugin: 'java'
apply plugin: 'me.tatarka.retrolambda'To ensure Android Studio correctly recognizes the language level when using Retrolambda, add the following to your android block in build.gradle:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}Use the retrolambda configuration block in your build.gradle to customize behavior.
Available Options:
javaVersion: The target Java version to compile to (accepted values: 5, 6, or 7). Default is 6.include: A list of build variants/sets to run through retrolambda (e.g., ['Debug', 'Release']). Default is all.exclude: A list of build variants/sets to skip. Use this instead of include.jvmArgs: A list of additional JVM arguments passed to the retrolambda process.defaultMethods: Enables support for default and static methods in interfaces. Note: Setting this to true automatically sets incremental to false due to retrolambda limitations. Default is false.incremental: If true, only changed class files are processed. If false, all class files are processed. Default is true.retrolambda {
javaVersion JavaVersion.VERSION_1_6
jvmArgs '-arg1', '-arg2'
defaultMethods false
incremental true
}By default, the plugin uses net.orfjackal.retrolambda:retrolambda:2.5.6. You can override this by using the retrolambdaConfig dependency configuration to specify a different version from Maven Central or a local file.
dependencies {
// Latest one on maven central
retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:+'
// Or a local version
// retrolambdaConfig files('libs/retrolambda.jar')
}If you are running on Java 6 or 7, you may need to explicitly define the JDK paths for the plugin to function correctly.
jdk property or set the JAVA8_HOME environment variable.oldJdk property or set JAVA5_HOME, JAVA6_HOME, or JAVA7_HOME environment variables to force tests to run on an older version.retrolambda {
// Set the JDK used by retrolambda
jdk System.getenv("JAVA8_HOME")
// Set the JDK used for unit tests
oldJdk System.getenv("JAVA6_HOME")
}Certain versions of Google Play Services (specifically 5.0.77) contain bytecode incompatible with retrolambda.
Solutions:
4.4.52).-noverify to the jvmArgs in your retrolambda configuration block.retrolambda {
jvmArgs '-noverify'
}The plugin is compatible with Proguard (v2.4.0+). To prevent warnings related to lambda bytecode, add these rules to your Proguard configuration file:
-dontwarn java.lang.invoke.*
-dontwarn **$$Lambda$*