我如何在非 android 模块上 运行 Android Lint?

How can I run Android Lint on non-android modules?

我如何 运行 gradlew lintDebug Android appJava模块?

这是一个示例项目设置:

root-project
  |- android-app
  |- java-library

这是我到目前为止所做的:

gradle.taskGraph.whenReady { taskGraph ->
    if (taskGraph.hasTask(":$project.name:lintDebug")) {
        rootProject.subprojects.each { subprojects ->
            // Do not add the same sources twice for this project
            if (subprojects.name == project.name) {
                return
            }

            // Append other modules source directories to this one
            android.sourceSets.main.java.srcDirs += subprojects.files("./src/main/java")
        }
    }
}

将此代码片段添加到 android-app 模块允许您 运行 gradlew :android-app:lintDebug 以便 运行 lint 两个模块源文件。

为了 运行 lint 在非 android 基础模块上,我不得不 运行 第二次:

解释:

  1. 运行正常lintDebug
  2. 任务完成后,将所有模块添加到源集中
  3. 运行 再次在另一个任务名称下进行 lint

    gradlew lintDebug lintDebug2

代码:

// This is a work around to get our lint rules to run on our entire project
// When running lint, add other sources sets to android app source set to run on all modules
gradle.taskGraph.afterTask { task ->
    if (task.name == "lintDebug") {
        // Remove current source sets
        android.sourceSets.main.java.srcDirs = []

        // Append other modules source directories to this one
        rootProject.subprojects.each { subprojects ->
            android.sourceSets.main.java.srcDirs += subprojects.files("./src/main/java")
        }
    }
}

// Once we ran "lintDebug" on the current module, let's run it on all the others
// lintDebug -> afterTask -> add all source sets -> run again
task lintDebug2(dependsOn: "lintDebug")