Android数据绑定:如何避免“无法找到KaptTask”警告

4
我有一个大型的Android项目,包含多个库模块。它们都使用Kotlin,并且许多已启用数据绑定。该项目和所有模块的构建和运行都没有错误。
然而,我一直在Gradle同步日志中收到每个模块的警告,我认为这是一个错误的正面反应:
> Configure project :feature-a
Kotlin plugin is applied to the project :feature-a but we cannot find the KaptTask. Make sure you apply the kotlin-kapt plugin because it is necessary to use kotlin with data binding.

> Configure project :feature-b
Kotlin plugin is applied to the project :feature-b but we cannot find the KaptTask. Make sure you apply the kotlin-kapt plugin because it is necessary to use kotlin with data binding.

> Configure project :feature-c
Kotlin plugin is applied to the project :feature-c but we cannot find the KaptTask. Make sure you apply the kotlin-kapt plugin because it is necessary to use kotlin with data binding.

[... etc. for dozens of modules ...]

我已经检查过确保 "kotlin-kapt" 插件正确应用。我正在为所有的模块使用 Kotlin Gradle DSL,并且像这样应用插件:

plugins {
    id("com.android.library")
    id("kotlin-android")
    id("kotlin-android-extensions")
    id("kotlin-kapt")
    id("androidx.navigation.safeargs.kotlin")
}

这个警告是什么原因引起的?它确实是问题吗?如何让它消失?
7个回答

2
这个问题也可能出现在您的buildSrc/build.gradle[.kts]中声明了Android Gradle插件依赖项的情况下: https://issuetracker.google.com/issues/123491449 解决方法是将所有插件依赖项都声明在buildSrc/build.gradle[.kts]中,而不是根项目的build.gradle[.kts]文件中。

你能否提供一个如何做这件事的示例? - Aldrin Joe Mathew

1
这个问题的真正原因是没有应用 kotlin gradle 插件。
您可以在 TaskManager 中找到打印错误的代码。
try {
   //noinspection unchecked
   kaptTaskClass = (Class<? extends Task>) Class.forName("org.jetbrains.kotlin.gradle.internal.KaptTask");
} catch (ClassNotFoundException e) {
            logger.error(
                    "Kotlin plugin is applied to the project "
                            + project.getPath()
                            + " but we cannot find the KaptTask. Make sure you apply the"
                            + " kotlin-kapt plugin because it is necessary to use kotlin"
                            + " with data binding.");
        }

正如您所看到的,它无法解决org.jetbrains.kotlin.gradle.internal.KaptTask

在我的情况下,这是在迁移到约定插件后发生的。我忘记在buildSrc/build.gradle中添加implementation kotlinPlugin

repositories { ... }

dependencies {
    implementation gradlePlugins.android
    implementation gradlePlugins.kotlin
}

一旦我添加了那个,问题就解决了

奇怪的是,我忽略了这个警告几周,因为一切都构建得很好,直到数据绑定突然停止工作。我猜测可能存在某种竞态条件,我们在构建的某个其他点应用了 Kotlin 插件,然后在某些更改后它停止工作了。


0
假设您已经正确声明了kotlin-kapt插件,但仍然无法解决问题。请尝试将kotlin-gradle-plugin声明移动到buildSrc/build.gradle.kts中。
例如,如果您正在使用buildSrc/build.gradle.kts,但是您只在build.gradle.kts依赖项中声明了org.jetbrains.kotlin:kotlin-gradle-plugin,则需要进行上述操作。
 dependencies {
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
    ...
 }

kotlin-gradle-plugin 的声明移动到 buildSrc/build.gradle.kts 的依赖项部分,并使用 implementation 关键字代替 classpath,即在 buildSrc/build.gradle.kts 中应该有以下内容:
dependencies {
   implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
   implementation("com.android.tools.build:gradle:7.1.3")
   ...
}

0
应用了 Kotlin 插件至项目 :business_module:module_web,但是我们找不到 KaptTask。请确保你已应用 kotlin-kapt 插件,因为它是必要的以使用数据绑定的 Kotlin。在 app 的 build.gradle 中应用插件:
apply plugin: 'kotlin-kapt'

0
如果您的项目有 buildSrc,您可以删除 build.gradle 文件。
implementation gradleApi()
implementation localGroovy()

replease remote url 

0

这个错误来自于数据绑定注解处理器。 要禁用它,您只需要在一个模块中应用 kotlin-kapt 插件一次即可。 在除主模块之外的任何模块中执行以下操作:

plugins {
    ...
    id("kotlin-kapt") apply false
    ...
}

0
一个解决方法是在buildSrc/build.gradle.kts中依赖于AGP api。
// Wrong
com.android.tools.build:gradle:*
// Right
com.android.tools.build:gradle-api:*

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接