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

时间:2017-04-30 00:27:33

标签: android gradle android-gradle android-lint

如何在 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 个答案:

答案 0 :(得分:1)

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

说明:

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

    下再次运行lint

    gradlew lintDebug lintDebug2

  4. 代码:

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