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
(自定义 lint 规则)
app
使用自定义 lintchecks
模块,在 app/build.gradle
:
lintChecks project(":lintchecks")
现在,假设有一些自定义插件(例如在 buildSrc
中)或使用 subprojects
和 tasks.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 {}
.
我试过的
lintrules/build.xml
中的不同插件,例如maven-publish
, org.jetbrains.kotlin.jvm
, 同样的错误结果
- 声明插件的不同方式,例如
plugins { id (...) }
与 apply plugin: '...'
,相同的错误结果
- 旧版本的 lint 依赖项
- 删除
lintCheck
,或删除 tasks.whenTaskAdded
,或降级到 gradle 构建工具 4.0.1
,任何这些都会使其再次工作
- 运行 在 Android Studio 之外的终端上的任何命令,它工作正常,问题似乎只在 A.S 上。同步操作。
问题
还有其他人遇到同样的问题吗?
此问题已在工单下报告 https://issuetracker.google.com/issues/170656529
目前解决此问题的方法是跳过 idea.active
系统 IDE 测试中导致问题的逻辑 属性:
if(System.getProperty("idea.active") != "true"){
tasks.whenTaskAdded {
(...)
}
}
(感谢Jerry找票)
我们有一个带有自定义 lint 检查的 Android 多模块项目,我们一直在尝试迁移到 Android gradle 构建工具 4.1.0,但是 Android Studio 4.1.0 gradle 同步一直失败。
假设我们有两个模块(加上一些不相关的“库”模块):
app
(主应用模块)lintchecks
(自定义 lint 规则)
app
使用自定义 lintchecks
模块,在 app/build.gradle
:
lintChecks project(":lintchecks")
现在,假设有一些自定义插件(例如在 buildSrc
中)或使用 subprojects
和 tasks.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 {}
.
我试过的
lintrules/build.xml
中的不同插件,例如maven-publish
,org.jetbrains.kotlin.jvm
, 同样的错误结果- 声明插件的不同方式,例如
plugins { id (...) }
与apply plugin: '...'
,相同的错误结果 - 旧版本的 lint 依赖项
- 删除
lintCheck
,或删除tasks.whenTaskAdded
,或降级到 gradle 构建工具4.0.1
,任何这些都会使其再次工作 - 运行 在 Android Studio 之外的终端上的任何命令,它工作正常,问题似乎只在 A.S 上。同步操作。
问题
还有其他人遇到同样的问题吗?
此问题已在工单下报告 https://issuetracker.google.com/issues/170656529
目前解决此问题的方法是跳过 idea.active
系统 IDE 测试中导致问题的逻辑 属性:
if(System.getProperty("idea.active") != "true"){
tasks.whenTaskAdded {
(...)
}
}
(感谢Jerry找票)