This is an example project that illustrates how can the
JaCoCo Report Aggregation Plugin and
Test Report Aggregation Plugin
can be used to aggregate a complex Android project with JVM modules in a single :jacocoTestReport
and :testAggregateTestReport
tasks.
- An
app
android module (with Robolectric tests) - A
login
android library module (with JUnit4/JVM tests) - A
domain
jvm module (with tests) - A
coverage-plugin
included build that provides thecoverage
root plugin
The plugin fills the gaps between AGP and JaCoCo Report Aggregation Plugin by providing the necessary setup missing:
- It applies
jacoco-report-aggregation
andtest-report-aggregation
at root project - Creates
jacocoTestReport
andtestAggregateTestReport
forTestSuiteType.UNIT_TEST
- If a module applies
jacoco
plugin, it adds it to thejacocoAggregation
andtestReportAggregation
root configurations - If a module applies the
java
plugin, makes its childjacocoTestReport
task to depend ontest
- If a module applies the
android
plugin:- it enables by default
BuildType.enableUnitTestCoverage
ondebug
to produce jacoco exec files - adds the
codeCoverageExecutionData
,codeCoverageSources
,codeCoverageElements
(classes) andtestResultsElements
outgoing variants, to allowjacoco-report-aggregation
andtest-report-aggregation
to aggregate it
- it enables by default
Please note that JVM still need to manually apply jacoco
plugin (this is an intentional opt-in behavior)
build.gradle.kts
The task :jacocoTestReport
is added to the root project when applying this plugin and it can be
run to produce the report. All dependent test
tasks will be run too to produce the required execution data.
The same for :testAggregateTestReport
:
The easiest way to adopt this plugin, is to put the coverage.gradle.kts
on your buildSrc/src/main/kotlin
folder.
Create the buildSrc
project if missing and make sure to apply kotlin-dsl
plugin on it
Don't forget its companion CoveragePluginDSL.kt file
This is an opt-in/out switch meant to be used when having productFlavors
.
enableUnitTestCoverage
is a BuildType
setting (default on debug
). When having flavors, you'll
have many coverage reports to produce targeting debug
(one per flavor variant).
You can use enableUnitTestCoverage.set(false)
to turn aggregation off for an specific ProductFlavor
.
Basically, the variant won't be added to the codeCoverageExecutionData
configuration, so :jacocoTestReport
won't compute it
For instance, app
module has a environment
dimension with 2 flavors: stage
and prod
.
Without any extra settings, :jacocoTestReport
will depend on :app:testStageDebugUnitTest
and
:app:testProdDebugUnitTest
(running its src/test/
tests effectively twice).
You may choose which flavors participates in the aggregated report by doing:
productFlavors {
create("stage") {
dimension = "environment"
}
create("prod") {
dimension = "environment"
aggregateTestCoverage.set(false)
}
}
where it effectively only run :app:testStageDebugUnitTest