wyzard
wyzard

Reputation: 553

Run android checkstyle on all modules

I have an android project with several libraries. I want to run a checkstyle task on all of the source code. The project's structure:

app (com.android.application),
lib1 (com.android.library),
lib2 (com.android.library),
... 

I followed this config tutorial:

https://github.com/Piasy/AndroidCodeQualityConfig

Project's build.gradle:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

subprojects {
    apply from: "$rootProject.projectDir/quality.gradle"

    afterEvaluate {
        check.dependsOn 'checkstyle'
    }
}

quality.gradle:

apply plugin: 'checkstyle'

checkstyle {
    toolVersion '7.4'

    configFile file("${project.rootDir}/checkstyle/checkstyle.xml")
    configProperties.checkstyleSuppressionFilterPath = file(
            "${project.rootDir}/checkstyle/suppressions.xml")
            .absolutePath
}

task checkstyle(type: Checkstyle, group: 'verification') {
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    exclude '**/test/**'
    exclude '**/androidTest/**'
    exclude '**/R.java'
    exclude '**/BuildConfig.java'
    classpath = files()
}

If i run a gradle check on the root project it runs only on the :app modul, not the entirely project.

What am I missing? Thank you.

Upvotes: 3

Views: 1672

Answers (1)

aaronmarino
aaronmarino

Reputation: 3992

This might not be exactly the answer you're looking for, but my solution was to add

apply from: rootProject.file("gradle/quality.gradle")

to the build.gradle of each module that I wanted to run checkstyle on. In my case there was one or two modules that I didn't want it run on.

Here's my quality.gradle file

apply plugin: "checkstyle"

checkstyle {
    configFile rootProject.file('checkstyle.xml')
    ignoreFailures false
    showViolations true
    toolVersion = "8.15"
}

/** Checkstyle task for new files (not in exclude list). Fail build if a check fails **/
task checkstyle(type: Checkstyle) {
    configFile rootProject.file('checkstyle/checkstyle.xml')

    //fail early
    ignoreFailures false
    showViolations true

    source 'src'
    include '**/*.java'
    exclude rootProject.file('checkstyle/checkstyle-exclude-list.txt') as String[]
    classpath = files()
}

/** Checkstyle task for legacy files. Don't fail build on errors **/
task checkstyleLegacy(type: Checkstyle) {
    configFile rootProject.file('checkstyle.xml')

    ignoreFailures true
    showViolations true

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    classpath = files()
}

afterEvaluate {
    preBuild.dependsOn 'checkstyle'
}

Upvotes: 2

Related Questions