Android 数据绑定:如何避免 "cannot find the KaptTask" 警告
Android Data Binding: How to avoid "cannot find the KaptTask" warning
我有一个包含多个库模块的大型 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")
}
是什么导致了这个警告,它实际上是一个问题吗?如何让它消失?
此错误来自数据绑定注释处理器。
要禁用它,您应该只应用一次 kotlin-kapt
插件。
在主模块以外的任何模块中执行此操作:
plugins {
...
id("kotlin-kapt") apply false
...
}
此问题背后的真正原因是 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
就我而言,这是在迁移到约定插件之后发生的。我忘了添加 implementation kotlinPlugin
到 buildSrc/build.gradle
repositories { ... }
dependencies {
implementation gradlePlugins.android
implementation gradlePlugins.kotlin
}
添加之后问题就解决了
奇怪的是,我忽略了这个警告数周,因为一切都很好,直到数据绑定突然停止工作。我的猜测是存在某种竞争条件,我们在构建的其他点应用了 kotlin 插件,并且在进行一些更改后它停止工作。
项目应用了Kotlin插件:business_module:module_web但是找不到KaptTask。确保您应用了 kotlin-kapt 插件,因为必须将 kotlin 与数据绑定一起使用。在应用程序中应用插件 build.gradle:
apply plugin: 'kotlin-kapt'
如果你的项目有buildSrc,你可以删除build.gradle ->
implementation gradleApi()
implementation localGroovy()
replease remote url
如果您在 buildSrc/build.gradle[.kts]
中声明了 Android Gradle 插件依赖项,也会出现此问题:https://issuetracker.google.com/issues/123491449
解决此问题的方法是在 buildSrc/build.gradle[.kts]
中声明 ALL 插件依赖项而不是根项目的 build.gradle[.kts]
文件
假设您已经正确声明了 kotlin-kapt
插件,但它仍然无济于事。尝试将 kotlin-gradle-plugin
声明移动到 buildSrc/build.gradle.kts
.
即如果您使用 buildSrc/build.gradle.kts
,但您已声明
org.jetbrains.kotlin:kotlin-gradle-plugin
仅 build.gradle.kts
依赖项,例如
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")
...
}
我有一个包含多个库模块的大型 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")
}
是什么导致了这个警告,它实际上是一个问题吗?如何让它消失?
此错误来自数据绑定注释处理器。
要禁用它,您应该只应用一次 kotlin-kapt
插件。
在主模块以外的任何模块中执行此操作:
plugins {
...
id("kotlin-kapt") apply false
...
}
此问题背后的真正原因是 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
就我而言,这是在迁移到约定插件之后发生的。我忘了添加 implementation kotlinPlugin
到 buildSrc/build.gradle
repositories { ... }
dependencies {
implementation gradlePlugins.android
implementation gradlePlugins.kotlin
}
添加之后问题就解决了
奇怪的是,我忽略了这个警告数周,因为一切都很好,直到数据绑定突然停止工作。我的猜测是存在某种竞争条件,我们在构建的其他点应用了 kotlin 插件,并且在进行一些更改后它停止工作。
项目应用了Kotlin插件:business_module:module_web但是找不到KaptTask。确保您应用了 kotlin-kapt 插件,因为必须将 kotlin 与数据绑定一起使用。在应用程序中应用插件 build.gradle:
apply plugin: 'kotlin-kapt'
如果你的项目有buildSrc,你可以删除build.gradle ->
implementation gradleApi()
implementation localGroovy()
replease remote url
如果您在 buildSrc/build.gradle[.kts]
中声明了 Android Gradle 插件依赖项,也会出现此问题:https://issuetracker.google.com/issues/123491449
解决此问题的方法是在 buildSrc/build.gradle[.kts]
中声明 ALL 插件依赖项而不是根项目的 build.gradle[.kts]
文件
假设您已经正确声明了 kotlin-kapt
插件,但它仍然无济于事。尝试将 kotlin-gradle-plugin
声明移动到 buildSrc/build.gradle.kts
.
即如果您使用 buildSrc/build.gradle.kts
,但您已声明
org.jetbrains.kotlin:kotlin-gradle-plugin
仅 build.gradle.kts
依赖项,例如
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")
...
}