Adam Dye Adam Dye - 6 months ago 183
Android Question

Lambda causes complier exception in Android Library Module

When I use a lambda expression in an Android Library Module, I receive a compiler exception

com.sun.tools.javac.code.Symbol$CompletionFailure: class file for java.lang.invoke.MethodType not found
.

Lambda expressions are compiling without error when used in the Android application module.

A sample project to demonstrate the problem can be found here:

https://github.com/adamdye/AndroidLambdaIssue

The interesting classes in the example are MyLibrary.java and MainActivity.java. Each class contains the identical expressions.

Setup

Android Studio 2.2 preview 1
min/target SDK version = android-N
build tools version = 24.0.0.rc4
jack compiler enabled
source/target compatibility = 1.8
Gradle version = 2.10
Android Plugin Version = 2.2.0-alpha1
JDK = java 8


I am not interested in using retro-lambda. I'd like to get this working through proper configuration. I assume I'm missing a step somewhere.

Answer

Just to circle back, and add what I've learned.

I was able to get past the MethodType not found error by adding my JAVA HOME's runtime jar to gradle's bootclasspath in the project's build.gradle file.

allprojects {    
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xbootclasspath/a:" + System.properties.get("java.home") + "/lib/rt.jar"
        }
    }
}

However, the app would not deploy to a device or emulator because the app:transformClassesWithPreJackPackagedLibrariesForDebug gradle task fails. It requires that all interfaces that are used as a lamba are on the classpath. The bootclasspath fix did not set the classpath for the Jack compiler and libraries do not use the Jack compiler yet.

I opened a bug report on this issue: https://code.google.com/p/android/issues/detail?id=211386

They commented that this will be addressed in the near future.