【问题标题】:Android Data Binding: How to avoid "cannot find the KaptTask" warningAndroid 数据绑定:如何避免“找不到 KaptTask”警告
【发布时间】:2020-08-31 15:19:01
【问题描述】:

我有一个包含多个库模块的大型 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")
}

是什么导致了这个警告,它实际上是一个问题,我如何让它消失?

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    如果您在 buildSrc/build.gradle[.kts]:https://issuetracker.google.com/issues/123491449 中声明了 Android Gradle 插件依赖项,也会出现此问题

    解决此问题的方法是在 buildSrc/build.gradle[.kts] 而不是根项目的 build.gradle[.kts] 文件中声明 所有 插件依赖项

    【讨论】:

      【解决方案2】:

      这个问题背后的真正原因是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 插件,并且在进行了一些更改后它停止了工作。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          Kotlin 插件应用于项目 :business_module:module_web 但我们找不到 KaptTask。确保您应用了 kotlin-kapt 插件,因为有必要将 kotlin 与数据绑定一起使用。在应用的 build.gradle 中应用插件:

          apply plugin: 'kotlin-kapt'
          

          【讨论】:

            【解决方案5】:

            如果你的项目有 buildSrc,你可以删除 build.gradle ->

            implementation gradleApi()
            implementation localGroovy()
            
            replease remote url 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-12-25
              • 2021-02-04
              • 2015-05-30
              • 2020-09-24
              • 2019-02-21
              • 1970-01-01
              • 1970-01-01
              • 2017-08-17
              相关资源
              最近更新 更多