Search code examples
androidandroid-studiogradlelambdabutterknife

Use Lambda expressions and Butterknife


I tried to use Lambda expressions in my code and I got this error : lambda expressions are not supported at this language level

I just search for it on SO and found a solution adding this to gradle file :

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

defaultConfig {
        ...
        jackOptions {
            enabled true
        }
    }

Then I got a new error : Error:Could not get unknown property 'classpath' for task ':app:transformJackWithJackForDebug' of type com.android.build.gradle.internal.pipeline.TransformTask.

Searched on SO again and found this is because I can't use jack and apt at the same time... so I remove apt deleting those lines :

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
        ...
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        ...
    }

And got a new error because ButterKnife needs apt...

So how use Lambda and Butterknife in the same project ?


Solution

  • You should use annotation processor for Butter-knife library in the build.gradle

     compile 'com.jakewharton:butterknife:8.4.0'
     annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    

    Full Gradle Looks like:

    buildscript {
    repositories {
       ....
    }
    
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'me.tatarka:gradle-retrolambda:3.4.0'
       .....
    }}
    apply plugin: 'me.tatarka.retrolambda'
    ......
    
    android{
    .....
     compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    repositories {
    }
    dependencies {
        ..........
        compile 'com.jakewharton:butterknife:8.4.0'
        annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
     }
    }
    

    NOTE: Don't use jackOption = Enabled