Gradle 当存在 lintCheck 依赖项时,Android Studio 中的构建工具 4.1.0 同步失败

Gradle build tools 4.1.0 failing sync in Android Studio when there is a lintCheck dependency

我们有一个带有自定义 lint 检查的 Android 多模块项目,我们一直在尝试迁移到 Android gradle 构建工具 4.1.0,但是 Android Studio 4.1.0 gradle 同步一直失败。

假设我们有两个模块(加上一些不相关的“库”模块):

app 使用自定义 lintchecks 模块,在 app/build.gradle:

  lintChecks project(":lintchecks")

现在,假设有一些自定义插件(例如在 buildSrc 中)或使用 subprojectstasks.whenTaskAdded 组合的配置。 例如,在 ./build.gradle:

subprojects {
   tasks.whenTaskAdded { 
     // content not important
   }
}

lintcheck 配置没有什么特别之处。例如 lintchecks/build.gradle:

apply plugin: 'kotlin'

dependencies {
    compileOnly "com.android.tools.lint:lint-api:27.1.0"
    compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation "com.android.tools.lint:lint:27.1.0"
    testImplementation "com.android.tools.lint:lint-tests:27.1.0"
    testImplementation "com.android.tools:testutils:27.1.0"
}

jar {
    manifest {
        attributes("Lint-Registry-v2": "com.company.lintrules.IssueRegistry")
    }
}

Android Studio 同步一直失败,错误类似于:

A problem occurred evaluating project ':lintrules'.
> Failed to apply plugin 'kotlin'.
   > Gradle#projectsEvaluated(Action) on build 'MyCompanyBuild' cannot be executed in the current context.

总结

看来问题是 Android Studio 4.1.0 + 构建 gradle 工具 4.1.0 + lintCheck() + tasks.whenTaskAdded {}.

我试过的

问题

还有其他人遇到同样的问题吗?

此问题已在工单下报告 https://issuetracker.google.com/issues/170656529

目前解决此问题的方法是跳过 idea.active 系统 IDE 测试中导致问题的逻辑 属性:

if(System.getProperty("idea.active") != "true"){
  tasks.whenTaskAdded {
    (...)
  }
}

(感谢Jerry找票)