Skip to content

Instantly share code, notes, and snippets.

@sethrylan
Last active December 25, 2018 07:09
Show Gist options
  • Save sethrylan/6002657 to your computer and use it in GitHub Desktop.
Save sethrylan/6002657 to your computer and use it in GitHub Desktop.
Code Quality Tasks for Android
configurations {
codequality
}
repositories {
mavenCentral()
}
dependencies {
codequality 'com.puppycrawl.tools:checkstyle:5.6'
}
task checkstyle(type: AndroidCheckstyleTask) {
ignoreFailures true
showViolations false
}
check.dependsOn(checkstyle)
///////////////////////////////////////////////
////////////// Groovy Task Class //////////////
///////////////////////////////////////////////
import org.gradle.api.internal.project.IsolatedAntBuilder
/**
* See parameters at http://checkstyle.sourceforge.net/anttask.html
*/
class AndroidCheckstyleTask extends DefaultTask {
@InputFile @Optional File configFile = new File("$project.rootDir/config/checkstyle/checkstyle.xml")
@InputFile @Optional File xslFile = new File("$project.rootDir/config/checkstyle/checkstyle-noframes-sorted.xsl")
@OutputFile @Optional File outputFile = new File("$project.buildDir/reports/checkstyle/checkstyle-${project.name}.xml")
FileCollection checkstyleClasspath = project.configurations.codequality
Boolean ignoreFailures = false
Boolean showViolations = true
Project gradleProject = project
def AndroidCheckstyleTask() {
description = 'Runs checkstyle against Android sourcesets.'
group = 'Code Quality'
}
@TaskAction
def runCheckstyle() {
outputFile.parentFile.mkdirs()
def antBuilder = services.get(IsolatedAntBuilder)
antBuilder.withClasspath(checkstyleClasspath).execute {
ant.taskdef(name: 'checkstyle', classname: 'com.puppycrawl.tools.checkstyle.CheckStyleTask')
// see also, maxWarnings and failureProperty arguments
ant.checkstyle(config: configFile, failOnViolation: !ignoreFailures) {
fileset(dir: gradleProject.projectDir.getPath()) {
gradleProject.android.sourceSets.each { sourceSet ->
sourceSet.java.each { file ->
include(name: gradleProject.relativePath(file))
}
}
}
if (showViolations) {
formatter(type: 'plain', useFile: false)
}
formatter(type: 'xml', toFile: outputFile)
}
if (outputFile.exists()) {
ant.xslt(in: outputFile,
style: xslFile,
out: outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html")
)
}
}
}
}
configurations {
findbugs
findbugsPlugins
}
repositories {
mavenCentral()
}
dependencies {
compile "com.google.code.findbugs:annotations:$versions.findbugs"
findbugs "com.google.code.findbugs:findbugs-ant:$versions.findbugs"
}
task findbugs(type: AndroidFindBugsTask) {
ignoreFailures true
}
///////////////////////////////////////////////
////////////// Groovy Task Class //////////////
///////////////////////////////////////////////
import org.gradle.api.internal.project.IsolatedAntBuilder
/**
* See parameters at http://findbugs.sourceforge.net/manual/anttask.html
*/
class AndroidFindBugsTask extends DefaultTask {
@InputFile @Optional File excludeFile = new File("$project.rootDir/config/findbugs/exclude.xml")
@InputFile @Optional File xslFile = new File("$project.rootDir/config/findbugs/default.xsl")
@OutputFile File outputFile = new File("$project.buildDir/reports/findbugs/findbugs-${project.name}.xml")
FileCollection findbugsClasspath = project.configurations.findbugs
FileCollection pluginClasspath = project.configurations.findbugsPlugins
Boolean ignoreFailures = false
Project gradleProject = project
String errorProp = 'findbugsError'
String warningsProp = 'findbugsWarnings'
def AndroidFindBugsTask() {
description = 'Runs FindBugs against Android sourcesets.'
group = 'Code Quality'
dependsOn 'assemble'
dependsOn 'assembleTest'
dependsOn 'assembleDebug'
}
@TaskAction
def findBugs() {
outputFile.parentFile.mkdirs()
def antBuilder = services.get(IsolatedAntBuilder)
antBuilder.withClasspath(findbugsClasspath).execute {
ant.taskdef(name: 'findbugs', classname: 'edu.umd.cs.findbugs.anttask.FindBugsTask')
ant.findbugs(debug: 'true',
errorProperty: errorProp,
warningsProperty: warningsProp,
output: 'xml:withMessages',
outputFile: outputFile,
excludeFilter: excludeFile,
jvmargs: '-Xmx768M') {
findbugsClasspath.addToAntBuilder(ant, 'classpath')
pluginClasspath.addToAntBuilder(ant, 'pluginList')
auxclassPath(path: gradleProject.configurations.compile.asPath)
gradleProject.android.sourceSets*.java.srcDirs.each { srcDir ->
sourcePath(path: srcDir)
}
"class"(location: "$gradleProject.buildDir/classes")
}
if (ant.project.properties[errorProp]) {
throw new GradleException("FindBugs encountered an error. Run with --debug to get more information.")
}
if (ant.project.properties[warningsProp] && !ignoreFailures) {
if (outputFile) {
throw new GradleException("FindBugs rule violations were found. See the report at ${outputFile}.")
} else {
throw new GradleException("FindBugs rule violations were found.")
}
}
if (outputFile.exists()) {
ant.xslt(in: outputFile,
style: xslFile,
out: outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html")
)
}
}
}
}
configurations {
codequality
}
repositories {
mavenCentral()
}
dependencies {
codequality 'pmd:pmd:4.2.6'
}
task pmd(type: AndroidPmdTask) {
ignoreFailures true
showViolations false
}
check.dependsOn(pmd)
///////////////////////////////////////////////
////////////// Groovy Task Class //////////////
///////////////////////////////////////////////
import org.gradle.api.internal.project.IsolatedAntBuilder
/**
* See parameters at http://pmd.sourceforge.net/pmd-4.2.6/ant-task.html
*/
class AndroidPmdTask extends DefaultTask {
@InputFile @Optional File rulesetFile = new File("$project.rootDir/config/pmd/ruleset.xml")
@InputFile @Optional File xslFile = new File("$project.rootDir/config/pmd/pmd-nicerhtml.xsl")
@OutputFile @Optional File outputFile = new File("$project.buildDir/reports/pmd/pmd-${project.name}.xml")
FileCollection pmdClasspath = project.configurations.codequality
Boolean ignoreFailures = false
Boolean showViolations = true
Project gradleProject = project
def AndroidPmdTask() {
description = 'Runs PMD against Android sourcesets.'
group = 'Code Quality'
}
@TaskAction
def runPmd() {
outputFile.parentFile.mkdirs()
def antBuilder = services.get(IsolatedAntBuilder)
antBuilder.withClasspath(pmdClasspath).execute {
ant.taskdef(name: 'pmd', classname: 'net.sourceforge.pmd.ant.PMDTask')
ant.pmd(shortFilenames: 'true',
failonruleviolation: !ignoreFailures,
rulesetfiles: rulesetFile.toURI().toString()) {
formatter(type: 'xml', toFile: outputFile, toConsole: showViolations)
fileset(dir: gradleProject.projectDir.getPath()) {
gradleProject.android.sourceSets.each { sourceSet ->
sourceSet.java.each { file ->
include(name: gradleProject.relativePath(file))
}
}
}
}
ant.xslt(in: outputFile,
style: xslFile,
out: outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment