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

3

我该如何在Android应用程序Java模块上同时运行gradlew lintDebug

以下是示例项目设置:

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。
1个回答

1
为了在非基于Android的模块上运行lint,我不得不再次运行它:

解释:

  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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接