Skip to content

Instantly share code, notes, and snippets.

@truongngoclinh
Last active April 20, 2021 11:19
Show Gist options
  • Save truongngoclinh/31512800e21ce6806735fa1290a4338e to your computer and use it in GitHub Desktop.
Save truongngoclinh/31512800e21ce6806735fa1290a4338e to your computer and use it in GitHub Desktop.
Jacoco test coverage report for package, application level
apply plugin: 'jacoco'
android {
testOptions.unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
task jacoco(group: 'verification', description: 'Grenerate Jacoco Report for all android variants')
tasks.check.dependsOn tasks.jacoco
project.afterEvaluate {
def variants = android.hasProperty('libraryVariants') ? android.libraryVariants : android.applicationVariants
variants.forEach {
tasks.jacoco.dependsOn(addJacocoTask(it))
}
}
def addJacocoTask(variant) {
logger.info("adding jacoco task for variant $variant.name")
// see https://docs.gradle.org/current/dsl/org.gradle.testing.jacoco.tasks.JacocoReport.html
def jacocoTask = project.tasks.create("jacoco${variant.name.capitalize()}Report", JacocoReport)
jacocoTask.dependsOn("test${variant.name.capitalize()}UnitTest")
jacocoTask.group = 'verification'
jacocoTask.description = "Generate Jacoco Report for variant $variant.name"
jacocoTask.reports {
csv.enabled false
html.enabled true
xml.enabled true
}
def excludedFiles = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/AutoValue_*.*',
'**/AutoParcel_*.class',
'**/*JavascriptBridge.class'
]
def sourceDirectories = files(variant.sourceSets.java.srcDirs.flatten())
def classDirectories = fileTree(dir: variant.javaCompile.destinationDir, excludes: excludedFiles)
if(project.plugins.hasPlugin("kotlin-android")) {
sourceDirectories.from(files(variant.sourceSets.kotlin.srcDirs.flatten()))
def kotlinTask = tasks.getByName("compile${variant.name.capitalize()}Kotlin")
if(kotlinTask) {
classDirectories += fileTree(dir: kotlinTask.destinationDir, excludes: excludedFiles)
}
}
jacocoTask.sourceDirectories = sourceDirectories
jacocoTask.classDirectories = classDirectories
jacocoTask.executionData = files("${buildDir}/jacoco/test${variant.name}UnitTest.exec")
jacocoTask
}
import org.gradle.internal.logging.text.StyledTextOutputFactory
import static org.gradle.internal.logging.text.StyledTextOutput.Style
apply plugin: 'jacoco'
task jcDebug(type: JacocoReport, dependsOn: ":app:testDebugUnitTest") {
def out = services.get(StyledTextOutputFactory).create("")
def pckName = rootProject.hasProperty('package') ? rootProject['package'] : ''
out.withStyle(Style.Info).println("\nCreating Test Coverage Report for Package ${pckName.capitalize()}\n")
if (pckName != '') {
pckName += "/"
}
group = "Reporting"
description = "Generate Debug Variant Jacoco Report for Specific Package"
reports {
csv.enabled false
html.enabled true
xml.enabled true
}
def excludedFiles = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/AutoValue_*.*',
'**/AutoParcel_*.class',
'**/*JavascriptBridge.class',
'**/*Activity*.*',
'**/*Adapter*.*',
'**/*ViewPager*.*',
'**/*ViewHolder*.*'
]
def srcDirPath = "${project.projectDir}/src/main/java/.../$pckName"
def classDirPath = "${buildDir}/tmp/kotlin-classes/debug/.../$pckName"
def execDirPath = "${buildDir}/jacoco/testDebugUnitTest.exec"
sourceDirectories.from(files(srcDirPath))
classDirectories.from(fileTree(dir: classDirPath, excludes: excludedFiles))
executionData.from(files(execDirPath))
doLast {
exec {
def file = project.file("${buildDir}/reports/jacoco/jcDebug/html/index.html")
out.withStyle(Style.Success).println("\nOpening the Report \n")
commandLine 'open', file
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment