【问题标题】:Gradle can not find 'lint' as dependency for custom taskGradle 找不到“lint”作为自定义任务的依赖项
【发布时间】:2015-04-15 18:45:15
【问题描述】:

我想创建一个简单易用的 Gradle 任务,它运行我希望持续集成服务器运行的所有内容。目前,我的 app/build.gradle 文件包含以下代码(其余部分省略):

task continuousIntegrationDebug(dependsOn: [assembleDebug, lint, runCheckstyle, runPmd, runFindbugs]) {
}

遗憾的是,Gradle 抱怨它找不到 lint(lintDebug 和 lintRelease 相同):

A problem occurred evaluating project ':app'.
> Could not find property 'lint' on project ':app'.

有趣的是,以下行在我运行检查时有效:

check.dependsOn 'lint'

以下外部调用也有效(来自项目根目录):

./gradlew app:lint

我忘记了什么?我正在使用版本com.android.tools.build:gradle:1.1.0 和最新版本的构建工具等。

另外,我必须先在外部运行 pmd、checkstyle 和 findbugs,然后才能运行任务本身(之前没有安装)。如何确保在 Android Studio 中首次同步 Gradle 文件时始终下载它们?

【问题讨论】:

  • 我的猜测是您定义的方式中的 dependsOn 不需要数组,因此试图将 lint 视为不存在的属性,而不是另一个任务。您可以尝试删除方括号,即将代码更改为dependsOn: assembleDebug, lint, runCheckstyle, runPmd, runFindbugs吗?
  • 我已经尝试在没有 lint 的情况下运行该命令 - 可以。另外,我用 lint 尝试了 task.dependsOn otherTask 语法,它也不起作用。我也尝试不使用数组 - 这会产生语法错误。

标签: android android-studio build gradle android-gradle-plugin


【解决方案1】:

这与其说是一个有效的解决方案,不如说是一个提示,但它可能会为您指明正确的方向。

我检查了添加 lint 任务 [1] 的内部 Groovy 代码。他们会这样做:

 lint = project.tasks.create("lint", Lint)

... 为了定义 Lint 任务。那么也许您需要先自己创建 lint 任务?

[2] 中指出了我偶然发现的另一个解决方案:

def compileLintTask = project.tasks.find {it.name == 'compileLint'}
    compileLintTask.dependsOn(copyLintJar)

tasks.find 用于查找 compileLint 任务。也许您可以通过同样的方式找到您正在寻找的任务?

  1. https://android.googlesource.com/platform/tools/build/+/master/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
  2. https://engineering.linkedin.com/android/writing-custom-lint-checks-gradle

【讨论】:

  • 我没有让它按照您描述的方式运行,但是当我查看android.googlesource.com/platform/tools/build/+/master/gradle/… 中的第 232 行时,我有了另一个想法 - 任务“检查”取决于“lint”并且是可从外部获得。因此,如果我将我的代码部分更改为dependsOn: [assembleDebug, check, runCheckstyle, runPmd, runFindbugs],我会在编译后得到一个功能性的 Lint 检查。 :)
  • 太棒了。但请记住,check 就像一个锚任务,其他工具(例如 findbugs)挂在 [1] 中。因此,根据check,还可以执行一些其他任务。 1.tools.android.com/tech-docs/new-build-system/…
  • 这绝对是正确的 - 这就是我写评论而不是答案的原因。 ;) 如果我能找到更好的解决方案,我会在这里发布。
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多