evant / gradle-retrolambda

A gradle plugin for getting java lambda support in java 6, 7 and android
Apache License 2.0
5.3k stars 449 forks source link

Apply retrolambda to custom JavaCompile tasks #267

Closed Flowdalic closed 6 years ago

Flowdalic commented 6 years ago

I've a custom compileAndroid task which compiles my library project having the boot classpath set to android.jar of the corresponding minimum required Android API level, this ensures that only available methods are used.

task compileAndroid(type: JavaCompile) {
    source = compileJava.source
    classpath = compileJava.classpath
    destinationDir = new File(buildDir, 'android')
    options.bootClasspath = androidBootClasspath
}

But the compileAndroid task failes

> Task :smack-core:compileAndroid FAILED
/home/flo/data/code/smack/smack-core/src/main/java/org/jivesoftware/smack/SynchronizationPointWithSmackException.java:130: error: cannot find symbol
        reportFailureSetException(() -> {
                                  ^
  symbol:   method metafactory(Lookup,String,MethodType,MethodType,MethodHandle,MethodType)
  location: interface LambdaMetafactory
1 error

it appears that retrolambda is not applied to the compileAndroid task.

How do I manually apply it?

evant commented 6 years ago

It's not well documented but to you can create a retrolamda task. Check out the source for the java plugin https://github.com/evant/gradle-retrolambda/blob/master/gradle-retrolambda/src/main/groovy/me/tatarka/RetrolambdaPluginJava.groovy

Flowdalic commented 6 years ago

Thanks for your quick answer. Right now I wonder if I can even achieve what I want to achieve using Retrolambda: Writing Java 8 source code and linking that code against an android.jar of the corresponding Android API level. For this it seems to the required that retrolambda compiles the Java 8 source to Java 7 class files with simultaneously using the android.jar as boot classpath. But Retrolambda is only able to desugar Java 8 class files to Java 7 class files. Correct?

evant commented 6 years ago

The major problem you'll run into is you need certain java 8 classes on the classpath for retrolambda to work. Have you considered using https://github.com/xvik/gradle-animalsniffer-plugin?

Flowdalic commented 6 years ago

AnimalSniffer perfectly solves my use case, Thank you very much for pointing this out.