Jared Burrows
Jared Burrows

Reputation: 55527

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

How can I run gradlew lintDebug on both the Android app and the Java module?

Here is an example project setup:

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

Here is what I have done so far:

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

Adding this code snippet into the android-app module allows you to run gradlew :android-app:lintDebug in order to run lint on both modules source files.

Upvotes: 4

Views: 1140

Answers (1)

Jared Burrows
Jared Burrows

Reputation: 55527

In order to run lint on non-android based modules, I had to run it a second time:

Explanation:

  1. Run normal lintDebug
  2. Once the task finishes, add all over modules to the source set
  3. Run lint again under another task name

    gradlew lintDebug lintDebug2

Code:

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

Upvotes: 1

Related Questions