将Android Studio升级到3.0.1,结果再次使用butterknife时,竟然报错了。
错误提示如下:
Error:Execution failed for task ':app:javaPreCompileDebug'.
> 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 android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
提示:工程里的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"
}
推荐使用方案(二),因为错误提示中,已经给出了说明。javaCompileOptions方法之后可能被删除,弃用!