How to create an aggregated Jacoco report for a multi-module project?

Viewed 2963

I have been looking for a solution for days for merging multiple Jacoco reports for a multi-module Android project in order to send them off to Sonarcloud at once. I have already checked tons of Stackoverflow posts and other things such as blogs, Github repositories, Gradle forum, etc but unfortunately none of solutions works for me.

I would really appreciate if anybody here shares a sample project or code piece with me.

Gradle version: 7.0.2
Kotlin version: 1.5.21
JDK: 11

Below code piece also doesn't work for me

/**
 *  Root task that generates an aggregated Jacoco test coverage report for all sub-projects
 */
task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') {
    group = 'Reporting'
    description = 'Generates an aggregate report from all subprojects'

    tasks.withType(Test) {
        ignoreFailures true
    }

    def projects = subprojects

    //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() })
    }
}
2 Answers

Here working example how to create aggregated jacoco report: https://github.com/SurpSG/jacoco-gradle-plugin-merge-coverage

task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
    description = 'Generates an aggregate report from all subprojects'
    dependsOn(subprojects.test)

    additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
    sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories.from = files(subprojects.sourceSets.main.output)
    executionData.from = files(subprojects.jacocoTestReport.executionData)

    reports {
        html.enabled true
    }
}

I just created this demo project illustrating how to archive this with a buildSrc ad-hoc plugin, that relies on Gradle's jacoco-report-aggregation and Android Gradle Plugin's native jacoco support.

Later you can easily link it with sonarqube by adding this to the root project:

plugins {
    coverage // comes from `buildSrc/src/main/kotlin/coverage.gradle.kts`
    id("org.sonarqube") version "3.4.0.2513"
}

sonarqube {
    properties {
        tasks.jacocoTestReport.configure {
            property("sonar.coverage.jacoco.xmlReportPaths", reports.xml.outputLocation.get().asFile)
        }
    }
}

tasks.sonarqube.configure {
    dependsOn(tasks.jacocoTestReport)
}
Related