Android studio 3.1 version and ButterKnife 8.8.1 are incompatible and conflicting, the pro-test is effective

       Due to importing other people's projects, the version number of gradle was changed, which caused the project to report errors all the time. Finally, I checked the information and found that the problem was on ButterKnife. Some people had NullPointerException. The errors I encountered were as follows:

gradle version number

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

The solution is as follows:

  • Add the following code in the project's build.gradle:
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 Module's build.gradle:
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"
        
        //这是要添加的
        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 app's build.gradle:

Add in dependencies:

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

Note: add Modle to Module and app, especially if there is an inheritance relationship, complie and implementation cannot be mixed

Guess you like

Origin blog.csdn.net/qq_19822039/article/details/83000932