'Android Data Binding: How to avoid "cannot find the KaptTask" warning
I have a large Android project with multiple library modules. They all use Kotlin, and many have data binding enabled. The project and all modules build and run just fine with no errors.
However, I have been getting a warning in the Gradle sync logs for each module that I think is a false positive:
> 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 ...]
I have checked to make sure that the "kotlin-kapt" plugin is properly applied. I'm using the Kotlin Gradle DSL for all modules and am applying the plugin like this:
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-android-extensions")
id("kotlin-kapt")
id("androidx.navigation.safeargs.kotlin")
}
What is causing this warning, is it actually a problem, and how do I get it to disappear?
Solution 1:[1]
This problem can also occur if you have Android Gradle plugin dependency declared in your buildSrc/build.gradle[.kts]
: https://issuetracker.google.com/issues/123491449
A work-around for this is to declare ALL of your plugin dependencies in buildSrc/build.gradle[.kts]
instead of your root project's build.gradle[.kts]
file
Solution 2:[2]
The real reason behind this issue is that kotlin
gradle plugin wasn't applied
You can find the code that prints the error in 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.");
}
As you can see, it can't resolve org.jetbrains.kotlin.gradle.internal.KaptTask
In my case, this happened after migrating to convention plugins. I forgot to add implementation kotlinPlugin
to buildSrc/build.gradle
repositories { ... }
dependencies {
implementation gradlePlugins.android
implementation gradlePlugins.kotlin
}
Once I added that, the issue was solved
The weird thing is, I ignored this warning for weeks because everything built fine, until suddenly databinding stopped working. My guess is that there was some kind of race condition where we did apply kotlin plugin at some other point of our build, and that it stopped working after some change.
Solution 3:[3]
This error is coming from the databinding annotation processor.
To disable it you should only apply the kotlin-kapt
plugin once.
In any module other than the main one do this:
plugins {
...
id("kotlin-kapt") apply false
...
}
Solution 4:[4]
Kotlin plugin is applied to the project :business_module:module_web but we cannot find the KaptTask. Make sure you apply the kotlin-kapt plugin because it is necessary to use kotlin with data binding. Apply plugin in app's build.gradle:
apply plugin: 'kotlin-kapt'
Solution 5:[5]
if you project have buildSrc, you can delete build.gradle ->
implementation gradleApi()
implementation localGroovy()
replease remote url
Solution 6:[6]
Assuming that you have already properly declared kotlin-kapt
plugin and it still does not help. Try moving kotlin-gradle-plugin
declaration to buildSrc/build.gradle.kts
.
I.e. if you are using buildSrc/build.gradle.kts
, but you have declared
org.jetbrains.kotlin:kotlin-gradle-plugin
in just build.gradle.kts
dependencies as e.g.
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
...
}
move kotlin-gradle-plugin
declaration to buildSrc/build.gradle.kts
dependencies section and use implementation
keyword instead of classpath
, i.e. in buildSrc/build.gradle.kts
you should have:
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
implementation("com.android.tools.build:gradle:7.1.3")
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jonas Masalskis |
Solution 2 | Maragues |
Solution 3 | Sir Codesalot |
Solution 4 | Amin |
Solution 5 | Time Relocation |
Solution 6 | Tomasz |