Android studio version 3.1 and butterknife version 8.8.1 are incompatible. The pro test is effective

Posted by seanstuart on Tue, 17 Dec 2019 17:25:45 +0100

Due to importing other people's projects and changing the version number of gradle, the project always reported an error. Finally, it was found that the problem was in ButterKnife when I looked up the data. Some people had NullPointerException, and my error was as follows:

gradle version number

classpath  'com.android.tools.build:gradle:3.1.0'

The solution is as follows:

  • Add the following code to build.gradle of Project:
buildscript {
    repositories {
        jcenter()
        google()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
    }
   
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
    }
}
  • Add the following code to build.gradle of Module:
apply plugin: 'com.android.library'
apply plugin:'com.jakewharton.butterknife'

android {

    compileSdkVersion 25
    buildToolsVersion '27.0.3'
    defaultConfig {
        
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        
        //This is to be added
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }

    }
    
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    
}
  • Add the following code to build.gradle of app:

In dependencies, add:

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Note: Module and app can be added, especially those with inheritance relationship. Complex and implementation cannot be mixed

Topics: Gradle ButterKnife Android Google