Android Studio :Failed to resolve : annotationProcessor

本文介绍了在Android Studio中遇到`Failed to resolve : annotationProcessor`的问题,详细描述了问题背景及解决过程。在Android Studio 2.3及以上版本,注解处理器的使用方式发生了变化,需要将`apply plugin: 'android-apt'`替换为`annotationProcessor`依赖。同时提供了官方博客链接以了解更多关于Android Studio版本更新的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

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

参考

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值