Skip to content

Instantly share code, notes, and snippets.

@felipefpx
Created September 1, 2019 10:29
Show Gist options
  • Save felipefpx/392b6eb602999c5d244c89aa89468356 to your computer and use it in GitHub Desktop.
Save felipefpx/392b6eb602999c5d244c89aa89468356 to your computer and use it in GitHub Desktop.
Jacoco Merged Report for Multimodule Android with Kotlin DSL
#!/bin/bash
clear
./gradlew clean mergedJacocoReport
./gradlew jacocoFullReport
REPORT_PATH="file://$(pwd)/build/reports/jacoco/jacocoFullReport/html/index.html"
echo ${REPORT_PATH} | pbcopy
echo "Report available at:"
echo ${REPORT_PATH}
echo "Report file path copied to clipboard. You can paste it in your favorite browser. :)"
#
# Before use it, in the first time, you must guarantee some running permissions:
# chmod +x coverage.sh
#
# After that, you just need to run:
# ./coverage.sh
#
# It will generate coverage report and copy the html report path to your clipboard.
// Put it at root/buildSrc/src/main/kotlin
@file:Suppress("UnstableApiUsage")
plugins {
jacoco
}
tasks.withType<Test> {
configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
}
}
task<JacocoReport>("mergedJacocoReport") {
dependsOn("testDebugUnitTest", "createDebugCoverageReport")
reports {
xml.isEnabled = false
html.isEnabled = false
}
val fileFilter = mutableSetOf(
"**/R.class",
"**/R\$*.class",
"**/BuildConfig.*",
"**/Manifest*.*",
"**/*Test*.*",
"android/**/*.*",
"**/*\$Lambda$*.*", // Jacoco can not handle several "$" in class name.
"**/*\$inlined$*.*" // Kotlin specific, Jacoco can not handle several "$" in class name.
)
classDirectories.setFrom(
fileTree(project.buildDir) {
include(
"**/classes/**/main/**",
"**/intermediates/classes/debug/**",
"**/intermediates/javac/debug/*/classes/**", // Android Gradle Plugin 3.2.x support.
"**/tmp/kotlin-classes/debug/**"
)
exclude(fileFilter)
}
)
sourceDirectories.setFrom(
fileTree("${project.buildDir}") {
include(
"src/main/java/**",
"src/main/kotlin/**",
"src/debug/java/**",
"src/debug/kotlin/**"
)
}
)
executionData.setFrom(
fileTree(project.buildDir) {
include(
"outputs/code_coverage/**/*.ec",
"jacoco/jacocoTestReportDebug.exec",
"jacoco/testDebugUnitTest.exec",
"jacoco/test.exec"
)
}
)
}
// root/build.gradle.kts
buildScript {
// [...]
dependencies {
// [...]
classpath("com.palantir:jacoco-coverage:0.4.0")
}
}
// [...]
plugins {
id("com.palantir.jacoco-full-report") version "0.4.0"
}
// optional:
jacocoFull {
excludeProject(":excluded_module1", ":excluded_module2")
}
// [...]
// submodule/build.gradle.kts
plugins {
// [...]
id(mergedJacocoReport)
}
jacoco {
toolVersion = "0.8.2"
}
android {
defaultConfig {
// [...]
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
testOptions {
execution = "ANDROIDX_TEST_ORCHESTRATOR"
animationsDisabled = true
unitTests.apply {
isReturnDefaultValues = true
isIncludeAndroidResources = true
}
}
}
// [...]
@mochadwi
Copy link

mochadwi commented Dec 4, 2021

Thanks for this!

@mochadwi
Copy link

mochadwi commented Dec 5, 2021

Did you also use SonarQube to view the coverage reports?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment