解决 android-apt plugin is incompatible with the Android(安卓)Gradle plugin. Please use 'annotation
16lz
2021-01-25
在使用EventBus 3.0的时候,在Android Studio 执行Sync Project的时候出现了这个错误
android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProcessor'
由于网上很多介绍EventBus 3.0 是基于gradle 2.2.0之前的的版本,而Gradle 3.0
之后,Android官方提供了annotationProcessor
来代替android-apt
,annotationProcessor
同时支持 javac 和 jack 编译方式,而android-apt
只支持 javac 方式。同时android-apt
作者宣布不在维护,当然目前android-apt
仍然可以正常运行,如果你没有想支持 jack 编译方式的话,可以继续使用 android-apt
。
解决方案
- 先把项目根目录下的
build.gradle
中的classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
删除,下面是完整的代码
buildscript { repositories { mavenCentral() jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' //step 1.需要把这段代码注释或者删除 //classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' }}allprojects { repositories { mavenCentral() jcenter() google() }}
- 把module下面的
build.gradle
最上面引入的apply plugin: 'com.neenbedankt.android-apt'
删除,下面是完整的代码。
apply plugin: 'com.android.application'step 2.将这行代码注释或者删除//apply plugin: 'com.neenbedankt.android-apt
- 在module的
build.gradle
中的依赖修改为
//step 3.将apt 修改为annotationProcessor//apt 'org.greenrobot:eventbus-annotation-processor:3.1.1'annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
如果你是引用其他框架的注解,到这里就可以使用了。如果是引用了EventBus3.0的话还需要进行下面的一步。
- 需要将
apt{...}
代码移动到defaultConfig{...}
中,下面的完整的代码
apply plugin: 'com.android.application'android { signingConfigs { com { } } compileSdkVersion 26 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.knight.common" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" // step 5.将apt移动到这里,'com.knight.common.MyEventBusIndex'只是生成的目录,'knight.common'可以自定义。 javaCompileOptions { annotationProcessorOptions { arguments = [ eventBusIndex : 'com.knight.common.MyEventBusIndex' ] } } } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'org.greenrobot:eventbus:3.1.1' testCompile 'junit:junit:4.12' annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'}// step 4.将这还代码注释或者删除// apt {// arguments {// eventBusIndex "com.knight.commonp.MyEventBusIndex"// }// }
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
- Android初学者—listView用法
- Android(安卓)7.0 隐式广播-监听网络变化
- Android中在XML文件里@+id/和@id/的区别
- Android退出应用最优雅的方式
- 使用Android-PullToRefresh实现下拉刷新功能
- Android在线程中创建一个POST请求
- Android(安卓)如何实现ios中的UIPageController组件(就是那个带小