Dan Shookowsky
Dan Shookowsky

Reputation: 221

How can I execute checkstyle whenever I build an Android app

I'm trying to integrate checkstyle with an Android project - my build.gradle is below. I'd like to basically see warnings identifying code that's missing documentation on build. With this config, I see a gradle task named checkstyle that I can execute manually, but it's not called when I rebuild the project (even if I right-click the task and say execute on rebuild')

I must be missing something, because it seems like others are having the exact opposite problem and are trying to prevent it from running on build. What am I doing wrong?

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
    apply plugin: 'checkstyle'

    task checkstyle(type: Checkstyle) {
        configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
        source 'src'
        include '**/*.java'
        exclude '**/gen/**'

        reports {
            xml.enabled = true
        }

        classpath = files()
    }
}

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

Upvotes: 3

Views: 5743

Answers (2)

Dan Shookowsky
Dan Shookowsky

Reputation: 221

It appears I've found the answer to my own question - here

First create the task at the root level

allprojects {
   repositories {
       jcenter()
   }

   task checkstyle(type: Checkstyle) {
       showViolations = true
       configFile file("../settings/checkstyle.xml")

       source 'src/main/java'
       include '**/*.java'
       exclude '**/gen/**'
       exclude '**/R.java'
       exclude '**/BuildConfig.java'

       // empty classpath
       classpath = files()
   }
}

Then add the dependencies at the module level. These were just appended at the end of the existing build.gradle file for the project.

apply plugin: 'checkstyle'

preBuild.dependsOn('checkstyle')
assemble.dependsOn('lint')
check.dependsOn('checkstyle')

Upvotes: 6

STARGATEBG
STARGATEBG

Reputation: 251

I had to do this a couple of months ago ... after a lot of research and looking.

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
    // Cleaning the old log because of the creation of the new ones (not sure if totaly needed)
    delete fileTree(dir: "${project.rootDir}/app/build/reports")
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    // empty classpath
    classpath = files()
    //Failing the build
    ignoreFailures = false
}

checkstyle {
    toolVersion = '6.18'
}

project.afterEvaluate {
    preBuild.dependsOn 'checkstyle'
}

This wast part is important. preBuild is the first task that get's executed every time there is build, but it's not visible until gradle is running so you need .afterEvaluate. With this checkstyle is the first thing to run. In the code above you can set ignorefailures to true and it will fail the build if severity of checks is set to Error and will not if there are only warrnings.

BTW this needs to be in the module gradle file for example build.gradle(Module:app)

Upvotes: 4

Related Questions