Get Android gradle plugin & checkstyle working together / command line usage

久未见 提交于 2019-11-29 20:19:59

I got pmd, findbugs, and checkstyle working with Gradle 1.12 android plugin 0.12.+ using the following script:

apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'

check.dependsOn 'checkstyle', 'findbugs', 'pmd'

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

    classpath = files()
}

task findbugs(type: FindBugs) {
    ignoreFailures = true
    effort = "max"
    reportLevel = "high"
    excludeFilter = new File("${project.rootDir}/config/quality/findbugs/findbugs-filter.xml")
    classes = files("$project.buildDir/intermediates/classes/")

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml {
            destination "$project.buildDir/reports/findbugs/findbugs.xml"
            xml.withMessages true
        }
    }

    classpath = files()
}

task pmd(type: Pmd) {
    ruleSetFiles = files("${project.rootDir}/config/quality/pmd/pmd-ruleset.xml")
    ignoreFailures = true
    ruleSets = ["basic", "braces", "strings"]

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml.enabled = true
        html.enabled = false
    }
}

Running gradle build in command line will run all code quality plugins and generate xml reports in app/build/reports/ which are then ready to be viewed or parsed by CI tools.

Someone has a great answer to solve integrating PMD, findbugs and checkstyle with Gradle for Android. Unfortunately, the only solution for now is based on ant : http://sethrylan.org/2013/07/14/gradle-android-findbugs.html

I wish gradle will one day allow to do as much as maven for Android.

--- Update as of October 2013

With Gradle 1.8 and Android plugin for Gradle 0.6.+, you don't need this anymore. Android sourcesets and configurations are now compatible with the java plugin and all quality plugin work out of the box.

This includes pmd, findbugs, checkstyle and classycle.

--- Update A configuration, largely inspired from the project mentioned above, is proposed in this open source project as well, plus other tools.

To get this to work with my Android project, I had to declare the task explicitly.

Here's what worked for me:

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    // empty classpath
    classpath = files()
}

Be mindful that the Android plugin may choose to create a task of the same name in the future or work in conjunction with the checkstyle plugin in different ways.

You can try Android Check plugin (https://github.com/noveogroup/android-check):

  • Checkstyle
  • PMD

Configuration:

buildscript {
    repositories { jcenter() }
    dependencies {
        ...
        classpath 'com.noveogroup.android:check:+'
        ...
    }
}

apply plugin: 'com.noveogroup.android.check'

You can use hardcore configuration:

check {
    abortOnError true
    checkstyle { config hard() }
    pmd { config hard() }
}

I found by digging on the web that the Android plugin depends on java-base and not java (i.e. the sourceSets management is specific) and the checkstyle plugin rely on java. As a consequence, some gradle upstream modification are needed to get the thing done. The gradle team is working on that, as seen on twitter:

@anzix Android source sets will be soon understood by the generic code quality plugins. Work on that has already started.

— Gradle Build System (@Gradleware) May 26, 2013

Take a look at the Soter Gradle plugin to semlessly add support for Findbugs, Checkstyle and PMD to Android projects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!