Android Studio :Failed to resolve : annotationProcessor

时间:2025-04-10 15:31:18

问题描述

Android Studio :Failed to resolve : annotationProcessor

在编写Android Arch Component Demo时碰到这么一个问题,然后项目一直无法编译通过,通过字面意思可以推出是无法解析annotationProcessor

Project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        maven { url '' }
        jcenter()
    }
    dependencies {
        classpath ':gradle:2.3.3'
        // TODO 请关注下面这个依赖
        classpath ':android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module  files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url '' }
    }
}

task clean(type: Delete) {
    delete 
}

Module

apply plugin: ''
// TODO
apply plugin: '-apt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId ""
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(''), ''
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile(':espresso-core:2.2.2', {
        exclude group: '', module: 'support-annotations'
    })
    compile ':appcompat-v7:26.1.0'
    compile '.okhttp3:okhttp:3.8.1'
    testCompile 'junit:junit:4.12'
    compile ':constraint-layout:1.0.2'
    compile ':gson:2.8.0'
    compile ':design:26.1.0'

    // lifecycle
    compile ":runtime:1.0.0"
    compile ":extensions:1.0.0-alpha9-1"
    annotationProcessor ':compiler:1.0.0-alpha9-1'

    compile ":runtime:1.0.0-alpha9-1"
    annotationProcessor ":compiler:1.0.0-alpha9-1"
    compile ":rxjava2:1.0.0-alpha9-1"
}

通过google一堆的文章,大部分都是说什么VPN或者要添加各种仓库什么的,全部无效。最后又去请教度娘,然后找到了问题的解决方案。

问题解决

在处理这个问题时,一直以为时依赖有问题,然后对于annotationProcessor的了解并不多。所以现在要了解下相关知识。(问题出错的原因主要在上述的两个Gradle配置文件中的两个TODO)

annotationProcessor

/p/a6898939f260
上面这篇博客中提到,Android Studio 2.3之后,Android Annotation的使用方式发生了变化。
由原先的插件方式改成了现有的依赖方式。
apply plugin: ‘-apt’ —》
annotationProcessor ‘: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 ‘:butterknife:8.4.0’
annotationProcessor ‘:butterknife-compiler:8.4.0’*

也就是说如果当Android Studio升级成2.3后,注解处理编译的方式发生了改变。

具体可查看官方提供博客,了解Android Studio版本更新所影响的新特性。
GO-》/2017/03/

参考

Migrate to Android Studio 2.3
集成android-apt,报警告解决方法