Created
August 2, 2016 01:38
-
-
Save rcgonzalezf/37fca7a3224fef1fdc2b8f5f0294b55d to your computer and use it in GitHub Desktop.
Generate code coverage. If on a mac, the afterTask will open the results in the default browser when you use the openinbrowser=true param.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# source: https://www.reddit.com/r/androiddev/comments/3ig3gm/show_us_your_gradle_tasks/cugju7z | |
usage: | |
./gradlew jacocoTestReport -Popeninbrowser=true #opens in browser | |
./gradlew jacocoTestReport #does not open in browser | |
*/ | |
configurations { | |
jacocoReport | |
} | |
task jacocoTestReport(type: JacocoReport, dependsOn: ['test']) { | |
description = "Generates Jacoco coverage reports: XML and HTML" | |
group = "Reporting" | |
jacocoClasspath = project.configurations['androidJacocoAnt'] | |
// exclude auto-generated classes and tests | |
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', | |
'android/**/*.*'] | |
def debugTree = fileTree(dir: "${project.buildDir}/intermediates/classes/debug/<your package tld>/<your package company name>/“, excludes: fileFilter) | |
def mainSrc = "${project.projectDir}/src/main/java" | |
sourceDirectories = files([mainSrc]) | |
classDirectories = files([debugTree]) | |
executionData = fileTree(dir: project.projectDir, includes: ['**/*.exec', '**/*.ec']) | |
reports { | |
xml { | |
enabled = true | |
destination = "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml" | |
} | |
csv.enabled false | |
html { | |
enabled = true | |
destination = "${project.buildDir}/reports/jacoco" | |
} | |
} | |
} | |
dependencies { | |
jacocoReport 'org.jacoco:org.jacoco.ant:0.7.2.201409121644' | |
} | |
gradle.taskGraph.afterTask { Task task, TaskState state -> | |
if (state.failure) { | |
println "-- jacocoTestReport FAILED." | |
} else { | |
if (task.name == "jacocoTestReport") { | |
def jacocoHTMLIndex = "${project.buildDir}/reports/jacoco/index.html" | |
def openinbrowser = project.hasProperty('openinbrowser') ? project.getProperty('openinbrowser') : 'false' | |
if (openinbrowser == "true") { | |
exec { | |
commandLine 'open', jacocoHTMLIndex | |
} | |
}else{ | |
println "Generated jacocoTestReport html located at ${jacocoHTMLIndex}" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment