Android studio 引用 butterKnife包错误解决

 错误提示:

Annotation processors must be explicitly declared now.  The following dependencies on the compile classpath are found to contain annotation processor.  Please add them to the annotationProcessor configuration.
    - butterknife-compiler-8.8.1.jar (com.jakewharton:butterknife-compiler:8.8.1)
  Alternatively, set 

提示:工程里的module依赖了butterknife的jar包,而jar里面又用到了注解。在新的gradle版本里,需要写新的groovy代码来对每个引用注解的地方单独配置。 
而在以往的版本中,gradle会默认给每个module都依赖一个annotationProcess,导致很多多余的对annotationProcess的依赖。
 

方案(一):在相应的module的gradle文件中,使用javaCompileOptions

defaultConfig {
        applicationId "com.breakloop.httpurlconnectiondemo"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //添加了javaCompileOptions 
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }
    }

方案(二):在相应的module的gradle文件中,使用annotationProcessor

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'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'com.jakewharton:butterknife-compiler:8.8.1'
    //添加了annotationProcessor
    annotationProcessor "com.jakewharton:butterknife-compiler-8.8.1"
}

然后项目就构建成功了。 

发布了40 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42650988/article/details/88802636