【问题标题】:Annotation processors must be explicitly declared now. The following dependencies on the compile classpath?注释处理器现在必须显式声明。编译类路径的以下依赖项?
【发布时间】:2019-08-18 05:08:25
【问题描述】:

我正在使用 android 架构组件开发新应用程序,但我从 gradle 收到以下错误

注解处理器现在必须显式声明。发现编译类路径的以下依赖项包含注释处理器。请将它们添加到 annotationProcessor 配置中。 - 生命周期编译器 2.0.0.jar (androidx.lifecycle:lifecycle-compiler:2.0.0) 或者,设置 android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true 以继续以前的行为。请注意,此选项已弃用,将来将被删除。 请参阅https://developer.android.com/r/tools/annotation-processor-error-message.html

下面是我的 app.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.lifecycleawaredemo"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.lifecycle:lifecycle-compiler:2.0.0'

    def lifecyle_version = "1.1.1"
    implementation "android.arch.lifecycle:extensions:$lifecyle_version"
    implementation "android.arch.lifecycle:compiler:$lifecyle_version"
}

在我的 build.gradle 下

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

【问题讨论】:

    标签: android android-gradle-plugin android-room androidx


    【解决方案1】:

    首先,您似乎混淆了您的 androidx 生命周期依赖项。您正在使用 androidx 工件,因此您可以删除此部分:

    def lifecyle_version = "1.1.1"
    implementation "android.arch.lifecycle:extensions:$lifecyle_version"
    implementation "android.arch.lifecycle:compiler:$lifecyle_version"
    

    你应该只需要:

    implementation 'androidx.lifecycle:lifecycle-compiler:2.0.0'
    

    现在,关于错误,消息说生命周期编译器包含注释处理器,因此您应该将它们添加到 annotationProcessor 配置中。为此,请在 app.gradle 的应用插件部分添加以下行:

    apply plugin: 'kotlin-kapt'
    

    这将启用 kotlin 注释处理器支持。接下来,更改:

    implementation 'androidx.lifecycle:lifecycle-compiler:2.0.0'
    

    允许编译器使用包含的注解处理器:

    kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0'
    

    应该可以正常编译。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多