Skip to content

Instantly share code, notes, and snippets.

@budioktaviyan
Created June 20, 2021 04:35
Show Gist options
  • Save budioktaviyan/08d7802402f898ea63541df377af6ed6 to your computer and use it in GitHub Desktop.
Save budioktaviyan/08d7802402f898ea63541df377af6ed6 to your computer and use it in GitHub Desktop.
Generate Coverage Reports (Jacoco) Tasks
//
// Generate Coverage Reports (Jacoco) Tasks
//
project.afterEvaluate {
/* ktlint-disable max-line-length */
//
// Check if project contain Android Application plugin
//
project.plugins.firstOrNull { plugin -> plugin is AppPlugin } ?: return@afterEvaluate
project.plugins.apply(dep.plugin.jacoco)
project.configure<JacocoPluginExtension> {
toolVersion = ver.jacoco
}
project.tasks.whenTaskAdded {
with(extensions.findByType<JacocoTaskExtension>() ?: return@whenTaskAdded) {
isIncludeNoLocationClasses = true
}
}
with(project.extensions.getByType<AppExtension>()) {
applicationVariants.all {
val variantLabel = name
val isDebuggable = buildType.isDebuggable
if (!isDebuggable) {
project.logger.info("Skipping Jacoco for $variantLabel because it is not debuggable")
return@all
}
project.tasks.register("jacoco${variantLabel.capitalize()}Report", JacocoReport::class.java) {
group = "Coverage Report"
dependsOn(project.tasks["test${variantLabel.capitalize()}UnitTest"])
val coverageSourceDirs = "src/main/kotlin"
val javaClasses = project.fileTree("${project.buildDir}/intermediates/javac/$variantLabel")
val kotlinClasses = project.fileTree("${project.buildDir}/tmp/kotlin-classes/$variantLabel")
// Using the default Jacoco exec file output path.
val execFile = "jacoco/test${variantLabel.capitalize()}UnitTest.exec"
executionData.setFrom(
project.fileTree("${project.buildDir}") {
setIncludes(listOf(execFile))
}
)
// Do not run task if there's no execution data.
setOnlyIf { executionData.files.any { file -> file.exists() } }
classDirectories.setFrom(javaClasses, kotlinClasses)
sourceDirectories.setFrom(coverageSourceDirs)
additionalSourceDirs.setFrom(coverageSourceDirs)
reports {
html.isEnabled = true
csv.isEnabled = false
xml.isEnabled = true
}
}
}
}
/* ktlint-enable max-line-length */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment