Hot questions for Using Butter Knife in android studio

Question:

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 ?


Answer:

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

Question:

i am trying to use butterknife . but i got error "Cannot resolve symbol '**ButterKnife'" . i have tried to import butterknife.ButterKnife manually but i got the same error and i have modified gradle and added those lines : at project level ...

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

at the module level ...

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

dependencies { 

   compile 'com.jakewharton:butterknife-compiler:8.5.1'
   apt 'com.jakewharton:butterknife-compiler:8.5.1' 

}

what shall i do ? thanks in advance . here is a screenshot of my code where i got the error link for the screenshot


Answer:

apply plugin: 'com.android.application'
android {
    compileSdkVersion XX
    buildToolsVersion "XX.0.0"
    defaultConfig {
        applicationId "com.xxxxx.xxx"
        minSdkVersion xx
        targetSdkVersion xx
        versionCode x
        versionName "x.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}


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


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.code.findbugs:jsr305:2.0.1'
    compile 'com.android.support:design:25.1.1'
    compile 'com.jakewharton:butterknife:8.0.1'

    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    testCompile 'junit:junit:4.12'
}