apply plugin: 'jacoco' jacoco { toolVersion(versions.jacoco) } task jacocoMergeReports(type: JacocoMerge) { group = "Reporting" description = "Merge all jacoco reports from projects into one." List jacocoFiles = new ArrayList<>() project.subprojects { subproject -> File commonModuleReport = new File(subproject.buildDir, "/jacoco/test.exec") if (commonModuleReport.exists()) jacocoFiles.add(commonModuleReport) File androidModuleReport = new File(subproject.buildDir, "/jacoco/testDebugUnitTest.exec") if (androidModuleReport.exists()) jacocoFiles.add(androidModuleReport) } executionData(jacocoFiles.toArray()) destinationFile(file("${project.buildDir}/jacoco/jacoco.exec")) } def createJacocoFullReportTask() { tasks.create(name: 'jacocoFullReport', type: JacocoReport, dependsOn: 'jacocoMergeReports') { group = "Reporting" description = "Generate full Jacoco coverage report including all modules." List classFileTreeList = new ArrayList<>() List sourceFileTreeList = new ArrayList<>() project.subprojects { subproject -> if (subproject.plugins.hasPlugin("kotlin-android") && subproject.hasProperty("jacocoExclude")) { classFileTreeList.add( fileTree( dir: "${subproject.buildDir}/tmp/kotlin-classes/debug", excludes: subproject.jacocoExclude ) ) sourceFileTreeList.add( subproject.extensions.getByName('android').sourceSets.main.java.sourceFiles ) } else if (subproject.hasProperty("jacocoExclude")) { classFileTreeList.add( fileTree( dir: "${subproject.buildDir}/classes/kotlin/main", excludes: subproject.jacocoExclude ) ) sourceFileTreeList.add(subproject.sourceSets.main.getAllSource()) } } classDirectories = files(classFileTreeList.toArray()) sourceDirectories = files(sourceFileTreeList.toArray()) executionData = files("${buildDir}/jacoco/jacoco.exec") reports { xml.enabled = true html.enabled = true csv.enabled = false } } } // We need to wait to all subprojects configuration finish or we don't get sources and exclusions def subprojectsCount = allprojects.size() allprojects { afterEvaluate { subproject -> subprojectsCount-- if (subprojectsCount == 0) { apply { createJacocoFullReportTask() } } } }