You can open this sample inside an IDE using the IntelliJ native importer or Eclipse Buildship.

This sample shows how to aggregate code coverage across multiple Java subprojects using JaCoCo. The jacoco-report-aggregation plugin provides this ability when applied to the distribution project, such as an Android application or WAR subproject.

The project in this example contains three projects: application, list and utilities. All three projects apply the jacoco plugin, and application consumes both list and utilities on its implementation configuration. The application subproject is the final distribution of this software project, and applies jacoco-report-aggregation to perform the code coverage aggregation.

application/build.gradle
plugins {
    id 'myproject.java-conventions'
    id 'application'
    id 'jacoco-report-aggregation' (1)
}

dependencies {
    implementation project(':list')
    implementation project(':utilities')
}

application {
    mainClass = 'org.gradle.sample.Main'
}

tasks.named('check') {
    dependsOn tasks.named('testCodeCoverageReport', JacocoReport) (2)
}
application/build.gradle.kts
plugins {
    id("myproject.java-conventions")
    application
    id("jacoco-report-aggregation") (1)
}

dependencies {
    implementation(project(":list"))
    implementation(project(":utilities"))
}

application {
    mainClass.set("org.gradle.sample.Main")
}

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

The application project requires no additional configuration if the jvm-test-suite plugin is present.

1 Apply the jacoco-report-aggregation plugin
2 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 distribution project’s runtime classpath are selected for potential aggregation.

For the default test suite named 'test', no additional configuration is necessary. The aggregated reports and their backing tasks of type JacocoReport are synthesized based on the test suites existing in the application project. Test suites having identical testType properties will be aggregated.

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 application/build/reports/jacoco/testCodeCoverageReport.

For more information, see Testing in Java project chapter.