Skip to content

Instantly share code, notes, and snippets.

@kboyarshinov
Last active August 9, 2020 15:48
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save kboyarshinov/2857d726d23f8b6cbc19 to your computer and use it in GitHub Desktop.
Save kboyarshinov/2857d726d23f8b6cbc19 to your computer and use it in GitHub Desktop.
Code quality gradle scripts for Android
/**
* Checkstyle tasks
* Usage:
* - place this file under root dir of your project at /gradle directory
* - apply script from your gradle file:
* apply from : "{rootDir}/gradle/checkstyle.gradle"
*
* To configure checkstyle use configs at:
* "{rootDir}/config/checkstyle/checkstyle.xml" - for main projects
* "{rootDir}/config/checkstyle/checkstyle-test.xml" - for tests
* "{rootDir}/config/checkstyle/suppresions.xml" - for style suppressions
*
* Xml and HTML reports are stored:
* "{project.buildDir}/reports/checkstyle/"
* HTML styling is done by XSLT stylesheet:
* "{rootDir}/config/checkstyle/checkstyle-noframes-sorted.xsl"
*/
apply plugin: 'checkstyle'
task checkstyleMain (type: Checkstyle) {
ignoreFailures = false
showViolations = false
source 'src/main', 'src/release'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
reports {
xml.destination "$project.buildDir/reports/checkstyle/main.xml"
}
classpath = files()
configFile = file("${rootProject.rootDir}/config/checkstyle/checkstyle.xml")
}
task checkstyleTest (type: Checkstyle){
ignoreFailures = false
showViolations = false
source 'src/androidTest'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
reports {
xml.destination "$project.buildDir/reports/checkstyle/test.xml"
}
classpath = files()
configFile = file("${rootProject.rootDir}/config/checkstyle/checkstyle-test.xml")
}
task checkstyleReport << {
checkType = project.ext.get("checkType")
if (file("$buildDir/reports/checkstyle/${checkType}.xml").exists()) {
ant.xslt(in: "$project.buildDir/reports/checkstyle/${checkType}.xml",
style:"${rootProject.rootDir}/config/checkstyle/checkstyle-noframes-sorted.xsl",
out:"$project.buildDir/reports/checkstyle/checkstyle_${checkType}.html"
)
}
}
task checkstyle(dependsOn:['checkstyleMain', 'checkstyleTest']){
description 'Runs Checkstyle inspection against Android sourcesets.'
group = 'Code Quality'
}
gradle.taskGraph.afterTask {Task task, TaskState state ->
if(state.failure) {
if (task.name in ['checkstyleMain', 'checkstyleTest']) {
checkstyleReport {
def matcher = task.name =~ /^checkstyle(.*)$/
if (matcher.matches()) {
project.ext.set("checkType", matcher.group(1).toLowerCase())
}
}
checkstyleReport.execute()
}
}
}
/**
* FindBugs task
* Usage:
* - place this file under root dir of your project at /gradle directory
* - apply script from your gradle file:
* apply from : "{rootDir}/gradle/findbugs.gradle"
*
* To configure findbugs exclude filter use configs at:
* "{rootDir}/config/findbugs/exclude.xml"
*
* Xml and HTML reports are stored:
* "{project.buildDir}/reports/findbugs/"
* HTML styling is done by XSLT stylesheet:
* "{rootDir}/config/findbugs/findbugs-default.xsl"
*/
configurations {
findbugs
findbugsPlugins
}
repositories {
mavenCentral()
}
dependencies {
findbugs "com.google.code.findbugs:findbugs-ant:2.0.3"
}
task findbugs(type: AndroidFindBugsTask) {
}
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'
}
@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/intermediates/classes")
}
if (ant.project.properties[errorProp]) {
throw new GradleException("FindBugs encountered an error. Run with --debug to get more information.")
}
if (outputFile.exists()) {
ant.xslt(in: outputFile,
style: xslFile,
out: outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html")
)
}
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.")
}
}
}
}
}
/**
* PMD task
* Usage:
* - place this file under root dir of your project at /gradle directory
* - apply script from your gradle file:
* apply from : "{rootDir}/gradle/pmd.gradle"
*
* To configure pmd ruleset use configs at:
* "{rootDir}/config/pmd/pmd-ruleset.xml"
*
* Xml and HTML reports are stored:
* "{project.buildDir}/reports/pmd/"
*/
apply plugin: 'pmd'
task pmd(type: Pmd) {
description 'Runs PMD inspection against Android sourcesets.'
group = 'Code Quality'
ruleSetFiles = files("${rootProject.rootDir}/config/pmd/pmd-ruleset.xml")
source = fileTree('src/')
include '**/*.java'
exclude '**/gen/**'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment