如何摆脱增量注释处理请求的警告?

How to get rid of Incremental annotation processing requested warning?

我刚刚开始使用 android 开发并尝试使用 Room 库。从昨天开始,我就遇到了这条警告信息

w: [kapt] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.lifecycle.LifecycleProcessor (NON_INCREMENTAL), androidx.room.RoomProcessor (NON_INCREMENTAL).

我已尝试研究和修复但无法避免此错误这是我的 grale.build 文件。请suggest/advice我做错了什么。

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "ps.room.bookkeeper"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation":"$projectDir/schemas".toString()]
            }
        }    
    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'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // life cycle dependencies
    def lifecycle_version = "2.0.0"
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    kapt "android.arch.lifecycle:compiler:$lifecycle_version"

    //Room dependencies
    //def room_version = "2.1.0"
    implementation 'android.arch.persistence.room:runtime:2.1.0'
    kapt 'android.arch.persistence.room:compiler:2.1.0'
    //annotationProcessor 'android.arch.persistence.room:compiler:2.1.0'

//    implementation "android.arch.lifecycle:extensions:$room_version"
//    kapt "android.arch.persistence.room:compiler:$room_version"
//    androidTestImplementation "android.arch.persistence.room:testing:$room_version"

    //implementation 'androidx.room:room-runtime:2.1.0'
    //annotationProcessor 'androidx.room:room-compiler:2.1.0'
}

如@Necrontyr 所述,kotlin-gradle-plugin 版本 1.3.50 中存在一个错误。只需将 build.gradle(Project) 中的 kotlin_version 降级到 1.3.41.

给你加上这行就可以了 gradle.properties:

kapt.incremental.apt=true

来自Room documentation

"Room has the following annotation processor options...room.incremental: Enables Gradle incremental annotation proccesor."

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                    "room.schemaLocation":"$projectDir/schemas".toString(),
                    "room.incremental":"true",
                    "room.expandProjection":"true"]
            }
        }
    }
}

务必将房间版本更新到 2.2.x 或更高版本。

如果它抱怨 "Incremental annotation processing requested, but support is disabled because the following processors are not incremental",那么在 gradle.properties 中将 "kapt.incremental.apt" 设置为 "true"(在不同的答案中提到)是违反直觉的。您需要将其设置为 "false"。这对我有用。

我正在使用 AndroidX,但我猜 android.arch.lifecycle 也是一样的。对我来说,它只是帮助取代了这个:

kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

...有了这个:

implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

因此,如果您使用的是 android.arch.lifecycle,替换它可能具有相同的效果:

kapt "android.arch.lifecycle:compiler:$lifecycle_version"

...有了这个:

implementation "android.arch.lifecycle:common-java8:$lifecycle_version"

请注意,这仅在您使用 Java 8 时有效,并且您还应该删除 OnLifecycleEvent annotations for LifecycleObserver classes and let those observers implement DefaultLifecycleObserver

也建议在 here 显示的 build.gradle 依赖项中更改为此方法。

真正的问题是增量处理使事情变得更快,但如果任何注解处理器是非增量的,其中 none 将以这种方式实际处理。

增量处理的目的是什么?

version 1.3.30+ 开始,增量处理允许模块不会在每次发生更改时再次完全处理,从而为构建过程提供更好的性能:

The main areas of focus for this release have been around Kotlin/Native, KAPT performance, as well as improvements for IntelliJ IDEA.

来自 Kotlin documentation:

Annotation processors (see JSR 269) are supported in Kotlin with the kapt compiler plugin. In a nutshell, you can use libraries such as Dagger or Data Binding in your Kotlin projects.

如何修复房间增量处理?

房间增量注释处理器默认禁用。这是一个已知问题,描述为 here。他们打算在 2.2.0 版本上修复它。您可以等待更新,也可以通过设置启用它以消除警告:

gradle.properties文件中:

kapt.incremental.apt=true

(可选步骤)

允许数据绑定增量:

android.databinding.incremental=true

为了更快的构建:

kapt.use.worker.api=true

如果只进行少量更改,构建时间会大大减少:

kapt.include.compile.classpath=false

(回到主题)

在你的项目build.gradle中,添加必要的依赖项(Groovy):

dependencies {
    ...
    implementation "androidx.room:room-runtime:2.2.0-rc01"
    annotationProcessor "androidx.room:room-compiler:2.2.0-rc01"
}

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.incremental":"true"]
            }
        }
    }
}

Kotlin DSL 版本:

dependencies {
    ...
    implementation("androidx.room:room-runtime:2.2.0-rc01")
    kapt("androidx.room:room-compiler:2.2.0-rc01")
}

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = mapOf("room.incremental" to "true")
            }
        }
    } 
}

2019 年 10 月 9 日

androidx.room:room-*:2.2.0 is released.

Gradle Incremental Annotation Processor: Room is now a Gradle isolating annotation processor and incrementability can be enabled via the processor option room.incremental.

最新更新:

对于最新的 Kotlin DSL 版本,请使用

    javaCompileOptions {
        annotationProcessorOptions {
            arguments["room.incremental"] = "true"
        }
    }

这里的许多其他答案掩盖了错误或禁用了增量处理,而不是实际使其按您想要的方式工作。

您可以在 gradle.properties 文件中为您的特定库启用增量处理。只需添加这些设置,或者与引发错误的库相匹配的设置:

android.databinding.incremental=true
android.lifecycleProcessor.incremental=true

您真正应该做的是在 build.gradle 模块应用程序的 buildConfig 标记中实施这些代码行:

javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                        "room.schemaLocation"  : "$projectDir/schemas".toString(),
                        "room.incremental"     : "true",
                        "room.expandProjection": "true"]
            }
        }

这里列出了您可以解决此问题并显着减少构建时间的方法。

在您的 build.gradle(模块)文件中:

android {
    ...
    defaultConfig {
        ...
        kapt {
            arguments {
                 arg("room.schemaLocation", "$projectDir/schemas".toString())
                 arg("room.incremental", "true")
                 arg("room.expandProjection", "true")
            }
        }
    }
    ...
}

在您的 gradle.properties 文件中:

kapt.incremental.apt=true            // enabled by default on 1.3.50+
kapt.use.worker.api=true             // faster builds
kapt.include.compile.classpath=false // near instant builds when there are few changes

android.databinding.incremental=true
android.lifecycleProcessor.incremental=true
//add your specific library if it supports incremental kapt 

当系统语言为非英语。在这种情况下,使用英语的计算机系统语言就可以解决问题。

启用 Kapt 增量注释处理请求

使用 Kotlin 1.3.31 或更新版本 Kotlin 1.3.30 released

在您的 android kotlin 项目 gradle.properties 文件中

# Enable Kapt Incremental annotation processing requeste
kapt.incremental.apt=true

# Enable android.databinding.annotationprocessor.ProcessDataBinding (DYNAMIC)
android.databinding.incremental=true

# Decrease gradle builds time 
kapt.use.worker.api=true

# turn off AP discovery in compile path, and therefore turn on Compile Avoidance
kapt.include.compile.classpath=false

# Enable In Logcat to determine Kapt
kapt.verbose=true

以上答案可能有用,但帮助我的是将 build.gradle(Project) 中的 kotlin_version 减少到 1.3.41 并构建项目。这将允许您查看您的实体模型是否存在任何问题。

我的是,忘记注释了@PrimaryKey。你的可能有所不同。 Kotlin 1.3.41 允许查看这些问题。解决这些问题并将您的 kotlin_version 恢复到之前的状态。

从 1.3.30 版本开始,kapt 支持增量注释处理作为一项实验性功能。然而,从版本 1.3.50 开始,增量注释处理 默认启用

因此,如果您的 kapt 版本大于或等于 1.3,则必须将 kapt.incremental.apt=true 行添加到 gradle.properties 文件以启用增量注释处理.30 和低于 1.3.50。除此以外;您不必将 kapt.incremental.apt 设置为 true 即可启用它。不过,您可以根据需要将其设置为 false 以禁用它。

除此之外; 增量注释处理也需要启用增量编译。因此,您必须将 kotlin.incremental=true 行添加到 gradle.properties 文件才能从增量注释处理功能中受益。

请注意,从 2.3.0-alpha02 版本开始,Room 的增量注释处理选项默认开启。这意味着如果您的库版本大于或等于此版本,则不必将 room.incremental 参数设置为 true。要了解更多信息,请参阅 issue #112110217

另请注意,如果您使用的是 Android Gradle 插件 3.6.x 或更新版本,增量注释处理选项默认为 ON 用于数据绑定。因此,您不必将 android.databinding.incremental=true 行添加到 gradle.properties 文件中。要了解更多信息,请参阅 issue #110061530

对我来说,当我的实体具有主键和@NonNull 注释,但类型字段仍然使用可为空(?)时,就会发生这种情况。所以,只要删除这个?,问题就消失了

data class TvEntity(
        @PrimaryKey
        @NonNull
        @ColumnInfo(name = "tvId")
        var tvId: String?,