Last active
December 20, 2022 13:48
-
-
Save huuphuoc1396/0c77662f2b3aaf5ddea99c142af73387 to your computer and use it in GitHub Desktop.
JaCoCo Setup for Android Multi Module Project (refs: https://nolambda.stream/posts/jacoco-setup-for-multi-module-project)
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
apply plugin: 'jacoco' | |
// Filter out modules | |
def jacocoCoveredProject = subprojects.findAll { project -> | |
project.name == "app" || project.name == "data" || project.name == "domain" | |
} | |
def coveredProject = subprojects | |
println("All subprojects:") | |
println(subprojects.name) | |
println("") | |
println("Jacoco covered subprojects:") | |
println(jacocoCoveredProject.name) | |
configure(coveredProject) { project -> | |
apply plugin: 'jacoco' | |
jacoco { | |
toolVersion = "0.8.5" | |
} | |
tasks.withType(Test) { jacoco.includeNoLocationClasses = true } | |
task jacocoReport(type: JacocoReport, dependsOn: 'test') { | |
reports { | |
csv.enabled = true | |
xml.enabled = false | |
html.enabled = true | |
} | |
final fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', | |
'**/*Test*.*', 'android/**/*.*', '**/androidx/databinding/*'] | |
final androidKotlinTree = fileTree(dir: "${project.buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter) | |
final kotlinTree = fileTree(dir: "${project.buildDir}/classes/kotlin/main", excludes: fileFilter) | |
final javacTree = fileTree(dir: "${project.buildDir}/intermediates/javac/debug", excludes: fileFilter) | |
final mainSrc = "${project.projectDir}/src/main/java" | |
sourceDirectories.setFrom files([mainSrc]) | |
classDirectories.setFrom files([androidKotlinTree, kotlinTree, javacTree]) | |
executionData.setFrom fileTree(dir: project.buildDir, includes: [ | |
'jacoco/test.exec', | |
'jacoco/testDebugUnitTest.exec', | |
'outputs/code-coverage/connected/*coverage.ec' | |
]) | |
} | |
} | |
/** | |
* Root task that generates an aggregated Jacoco test coverage report for all sub-projects | |
* See the <a href="https://bit.ly/2FSWDtQ">JaCoCo Setup for Android Multi Module Project</a> | |
*/ | |
task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') { | |
tasks.withType(Test) { | |
ignoreFailures true | |
} | |
def projects = jacocoCoveredProject | |
// noinspection GrUnresolvedAccess | |
dependsOn(projects.jacocoReport) | |
final source = files(projects.jacocoReport.sourceDirectories) | |
additionalSourceDirs.setFrom source | |
sourceDirectories.setFrom source | |
classDirectories.setFrom files(projects.jacocoReport.classDirectories) | |
executionData.setFrom files(projects.jacocoReport.executionData) | |
reports { | |
html { | |
enabled true | |
destination file('build/reports/jacoco/html') | |
} | |
csv { | |
enabled true | |
destination file('build/reports/jacoco/jacocoFullReport.csv') | |
} | |
} | |
doFirst { | |
// noinspection GroovyAssignabilityCheck | |
executionData.setFrom files(executionData.findAll { it.exists() }) | |
} | |
} | |
/** | |
* Show the reports generated in the web browser | |
*/ | |
task showJacocoFullReport(type: Exec) { | |
dependsOn jacocoFullReport | |
workingDir "${rootProject.buildDir}/reports/jacoco/html/" | |
// Windows | |
commandLine 'cmd', '/c', 'start index.html' | |
// macOS | |
// commandLine 'open', 'index.html | |
// Ubuntu | |
// commandLine 'firefox', 'index.html' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment