问题描述
Android Studio :Failed to resolve : annotationProcessor
在编写Android Arch Component Demo时碰到这么一个问题,然后项目一直无法编译通过,通过字面意思可以推出是无法解析annotationProcessor
Project build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// TODO 请关注下面这个依赖
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module build.gradle
apply plugin: 'com.android.application'
// TODO
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.rzfsc.okhttpdemo"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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:26.1.0'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.android.support:design:26.1.0'
// lifecycle
compile "android.arch.lifecycle:runtime:1.0.0"
compile "android.arch.lifecycle:extensions:1.0.0-alpha9-1"
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha9-1'
compile "android.arch.persistence.room:runtime:1.0.0-alpha9-1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha9-1"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha9-1"
}
通过google一堆的文章,大部分都是说什么VPN或者要添加各种仓库什么的,全部无效。最后又去请教度娘,然后找到了问题的解决方案。
问题解决
在处理这个问题时,一直以为时依赖有问题,然后对于annotationProcessor的了解并不多。所以现在要了解下相关知识。(问题出错的原因主要在上述的两个Gradle配置文件中的两个TODO)
annotationProcessor
http://www.jianshu.com/p/a6898939f260
上面这篇博客中提到,Android Studio 2.3之后,Android Annotation的使用方式发生了变化。
由原先的插件方式改成了现有的依赖方式。
apply plugin: ‘com.neenbedankt.android-apt’ —》
annotationProcessor ‘android.arch.lifecycle:compiler:1.0.0-alpha9-
*If you use annotation processors (like ButterKnife for example):
remove the following line:
apply plugin: ‘android-apt’
change all occurrences of “apt” to “annotationProcessor”:
compile ‘com.jakewharton:butterknife:8.4.0’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.4.0’*
也就是说如果当Android Studio升级成2.3后,注解处理编译的方式发生了改变。
具体可查看官方提供博客,了解Android Studio版本更新所影响的新特性。
GO-》https://android-developers.googleblog.com/2017/03/android-studio-2-3.html