gradle-retrolambda

repository·main·Indexed 26 days ago

https://github.com/evant/gradle-retrolambda

A 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.

Tokens
1.3K
Snippets
8
Records
8
Agent score
40%

What's inside gradle-retrolambda

  1. Install the Gradle Retrolambda Plugin

    main

    To use Retrolambda in your Java or Android project, follow these steps:

    1. Download JDK 8 and set it as your default.
    2. Add the plugin to your build.gradle file.

    Requirements:

    • Minimum Android Gradle Plugin version: 1.5.0
    • Minimum Gradle version: 2.5

    Note: 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'
  2. Configure Android Studio for Java 8 compatibility

    main

    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
      }
    }
  3. Configure the retrolambda plugin

    main

    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
    }
  4. Use a custom retrolambda.jar version

    main

    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')
    }
  5. Configure JDK paths for Retrolambda

    main

    If you are running on Java 6 or 7, you may need to explicitly define the JDK paths for the plugin to function correctly.

    • Set the JDK for Retrolambda: Use the jdk property or set the JAVA8_HOME environment variable.
    • Set the JDK for Unit Tests: Use the 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")
    }
  6. Troubleshoot Google Play Services incompatibility

    main

    Certain versions of Google Play Services (specifically 5.0.77) contain bytecode incompatible with retrolambda.

    Solutions:

    1. Update to a newer version of Play Services if available.
    2. Downgrade to an earlier version (e.g., 4.4.52).
    3. Add -noverify to the jvmArgs in your retrolambda configuration block.
    retrolambda {
      jvmArgs '-noverify'
    }