Android Studio 3.0中解决apt报错,android-apt切换为官方annotationProcessor

Android Studio 3.0中解决apt报错,android-apt切换为官方annotationProcessor

最近从GitHub上Fork了一个项目任阅,用Android Studio3 打开后

报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Error:Cannot choose between the following configurations of project :EasyAdapterLibrary:
- debugApiElements
- debugRuntimeElements
- releaseApiElements
- releaseRuntimeElements
All of them match the consumer attributes:
- Configuration 'debugApiElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
- Found org.gradle.api.attributes.Usage 'java-api' but wasn't required.
- Configuration 'debugRuntimeElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
- Found org.gradle.api.attributes.Usage 'java-runtime' but wasn't required.
- Configuration 'releaseApiElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
- Found org.gradle.api.attributes.Usage 'java-api' but wasn't required.
- Configuration 'releaseRuntimeElements':
- Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required.
- Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
- Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
- Found org.gradle.api.attributes.Usage 'java-runtime' but wasn't required.

原因

从Messages中并没有查看出是什么原因,但是此项目之前使用的AS2.1,并且gradle中使用了apt,所以我怀疑是Studio升级到3.0之后原来的配置方式apt与最新版本Gradle已经不兼容。

APT
APT(Annotation Processing Tool)是一种处理注释的工具,它对源代码文件进行检测找出其中的Annotation,使用Annotation进行额外的处理。
Annotation处理器在处理Annotation时可以根据源文件中的Annotation生成额外的源文件和其它的文件(文件具体内容由Annotation处理器的编写者决定),APT还会编译生成的源文件和原来的源文件,将它们一起生成class文件。


Android Gradle插件2.2版本发布后,Android 官方提供了annotationProcessor来代替android-aptannotationProcessor同时支持 javac 和 jack 编译方式,而android-apt只支持 javac 方式。同时android-apt作者宣布不在维护,当然目前android-apt仍然可以正常运行,如果你没有想支持 jack 编译方式的话,可以继续使用 android-apt。Android Studio 3.0执行android-apt报错:

Android Studio 3.0执行android-apt一般会报错:

1
Error:android-apt plugin is incompatible with the Android Gradle plugin.  Please use 'annotationProcessor' configuration instead.

解决

android-apt切换为官方annotationProcessor的方法

1、在build.gradle修改为Android Gradle 3.0 插件

1
2
3
4
5
6
7
8
9
10
11
buildscript {
    repositories {
        ...
        google()
    }

- classpath 'com.android.tools.build:gradle:2.1.0'
+ classpath 'com.android.tools.build:gradle:3.0.1'

    }
}

2、删除build.gradle里android-apt 的相关配置

项目目录下

1
2
3
4
5
6
7
8
9
10
11
12
 buildscript {
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.1.0'
- classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
+ classpath 'com.android.tools.build:gradle:3.0.1'
+ // classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

app目录下

plugin: 'com.android.application'
1
2
3
4
5
6
- apply plugin: 'android-apt'
+ //apply plugin: 'android-apt'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

3、替换build.gradle里旧的配置为新的配置

做以下替换

  • compile 改为 implementation,
  • apt 改为 annotationProcessor
  • testCompile 改为 androidTestImplementation

app目录下

'com.squareup.okhttp3:okhttp:3.2.0'
1
2
3
4
5
6
7
8
     // Annotation
- compile 'com.google.dagger:dagger:2.0.2'
- apt 'com.google.dagger:dagger-compiler:2.0.2'
+// compile 'com.google.dagger:dagger:2.0.2'
+// apt 'com.google.dagger:dagger-compiler:2.0.2'
+ implementation 'com.google.dagger:dagger:2.9'
+ annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
+

4、清理缓存并重启Android Studio 3.0

参考:
https://www.jianshu.com/p/c1666a947692
https://majing.io/posts/10000003001155

Fork me on GitHub