Lambda causes compiler exception in Android Library Module
Asked Answered
L

2

8

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.

Lupe answered 25/5, 2016 at 23:28 Comment(1)
Ref this link. This may help you..Ible
L
7

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.

Lupe answered 8/6, 2016 at 0:40 Comment(2)
Is it possible now?Bifurcate
The issue I opened with Google is still in an "Assigned" state. So in other words, no. That same question just was asked on that thread. It wouldn't hurt for you to go add to the thread that it is an important issue to you too.Lupe
R
0

I solved the problem by including the JAR containing Java sources as a dependency, instead of the compiled JAR. This is how I configured my projects:

In library's build.gradle:

configurations {
    sources
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.java.srcDirs
}

artifacts {
    sources sourcesJar
}

In app's build.gradle:

dependencies {
    compile project(path: ":gdxstudio-core", configuration: "sources")

Of course the library is included as an external project in app's settings.gradle

Remediosremedy answered 19/2, 2017 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.