NOTE: You can open this sample inside an IDE using the https://www.jetbrains.com/help/idea/gradle.html#gradle_import_project_start[IntelliJ native importer] or https://projects.eclipse.org/projects/tools.buildship[Eclipse Buildship].

This sample shows how to aggregate code coverage across multiple Java subprojects using link:https://www.jacoco.org/jacoco/[JaCoCo].  The link:{userManualPath}/jacoco_report_aggregation_plugin.html[jacoco-report-aggregation plugin] provides this ability via a standalone project used to specify _which_ subprojects to include for aggregation.

The project in this example contains three "code" subprojects: `application`, `list` and `utilities`.  All three projects apply the `jacoco` plugin, and `application` consumes both `list` and `utilities` via its implementation configuration.  A fourth subproject `code-coverage-report` is the standalone utility project used to collect the coverage results.

[WARNING]
====
The Test Report Aggregation plugin does not currently work with the ``com.android.application`` plugin.
====

====
include::sample[dir="groovy",files="code-coverage-report/build.gradle[tag=**]"]
include::sample[dir="kotlin",files="code-coverage-report/build.gradle.kts[tag=**]"]
====

The standalone project applies `jacoco-report-aggregation`, but requires additional configuration if the `jvm-test-suite` plugin is not also present.

In this scenario, two additional pieces of setup are necessary:

<1> Declare dependencies using the `jacocoAggregation` configuration
<2> Define a report of type `JacocoCoverageReport` which collects coverage data from unit test suites
<3> Optional: make JaCoCo aggregate report generation part of the 'check' lifecycle phase

The report aggregation logic does not automatically inspect all subprojects for coverage data to aggregate.  Instead, the direct and transitive <<declaring_dependencies.adoc#sub:project_dependencies,project dependencies>> of the `jacocoAggregation` configuration are selected for potential aggregation.

The user must also declare one or more reports of type `JacocoCoverageReport`.  Each report instance specifies a `testType` property, used to match the test suite producing the coverage data.  A `JacocoReport` task is synthesized for each user-defined report and performs the aggregation.  Invoking this task will cause tests to be executed in the dependent projects of the `jacocoAggregation` configuration.

Running the tests and generate the report:

[listing.terminal.sample-command]
----
$ ./gradlew testCodeCoverageReport

BUILD SUCCESSFUL
25 actionable tasks: 25 executed
----

XML and HTML reports can now be found under `code-coverage-report/build/reports/jacoco/testCodeCoverageReport`.

For more information, see link:{userManualPath}/java_testing.html[Testing in Java project chapter].
