解决 android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProcesso

在使用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"
//     }
// }

猜你喜欢

转载自blog.csdn.net/a296777513/article/details/79196125
今日推荐