# Aggregating code coverage with JaCoCo using a standalone utility project (Incubating) Sample

version 8.14

- Groovy DSL

- Kotlin DSL

| You can open this sample in an IDE that supports Gradle.

This sample shows how to aggregate code coverage across multiple Java subprojects using JaCoCo. The 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.

| The Test Report Aggregation plugin does not currently work with the `com.android.application` plugin.

code-coverage-report/build.gradle.kts

    plugins {
        base
        id("jacoco-report-aggregation")
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        jacocoAggregation(project(":application")) (1)
    }

    reporting {
        reports {
            val testCodeCoverageReport by creating(JacocoCoverageReport::class) { (2)
                testSuiteName = "test"
            }
        }
    }

    tasks.check {
        dependsOn(tasks.named<JacocoReport>("testCodeCoverageReport")) (3)
    }

code-coverage-report/build.gradle

    plugins {
        id 'base'
        id 'jacoco-report-aggregation'
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        jacocoAggregation project(':application') (1)
    }

    reporting {
        reports {
            testCodeCoverageReport(JacocoCoverageReport) { (2)
                testSuiteName = "test"
            }
        }
    }

    tasks.named('check') {
        dependsOn tasks.named('testCodeCoverageReport', JacocoReport) (3)
    }

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 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:

    $ ./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 Testing in Java project chapter.