在使用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-aptannotationProcessor同时支持 javacjack 编译方式,而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
  • modulebuild.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"//     }// }

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
  3. Android初学者—listView用法
  4. Android(安卓)7.0 隐式广播-监听网络变化
  5. Android中在XML文件里@+id/和@id/的区别
  6. Android退出应用最优雅的方式
  7. 使用Android-PullToRefresh实现下拉刷新功能
  8. Android在线程中创建一个POST请求
  9. Android(安卓)如何实现ios中的UIPageController组件(就是那个带小

随机推荐

  1. Layout属性介绍
  2. 在android中实现动态跑动的图表实现方法
  3. 理解Android的手势识别
  4. android之wifi体系架构源码流程分析
  5. Android 混淆打包
  6. android中获取当前程序路径
  7. Android中ListView中显示图片和文本
  8. android doc 国内镜像
  9. [每天学点Android开发]Building Web Apps
  10. Android P SystemUI之StatusBar加载流程