-
-
Save alexvanyo/9a3b38ef6c07907cfa7f9a4310c628bd to your computer and use it in GitHub Desktop.
| import com.android.build.gradle.internal.tasks.factory.dependsOn | |
| plugins { | |
| id("com.android.application") | |
| jacoco | |
| } | |
| // Register the main JaCoCo task to later depend on the per-variant tasks | |
| val jacocoTestReport = tasks.register("jacocoTestReport") | |
| tasks.withType<Test> { | |
| configure<JacocoTaskExtension> { | |
| isIncludeNoLocationClasses = true | |
| } | |
| } | |
| android { | |
| applicationVariants.all(closureOf<com.android.build.gradle.api.ApplicationVariant> { | |
| val testTaskName = "test${this@closureOf.name.capitalize()}UnitTest" | |
| val excludes = listOf( | |
| // Android | |
| "**/R.class", | |
| "**/R\$*.class", | |
| "**/BuildConfig.*", | |
| "**/Manifest*.*" | |
| ) | |
| val reportTask = tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport::class) { | |
| dependsOn(testTaskName) | |
| reports { | |
| xml.isEnabled = true | |
| html.isEnabled = true | |
| } | |
| classDirectories.setFrom( | |
| files( | |
| fileTree(this@closureOf.javaCompileProvider.get().destinationDir) { | |
| exclude(excludes) | |
| }, | |
| fileTree("$buildDir/tmp/kotlin-classes/${this@closureOf.name}") { | |
| exclude(excludes) | |
| } | |
| ) | |
| ) | |
| // Code underneath /src/{variant}/kotlin will also be picked up here | |
| sourceDirectories.setFrom(this@closureOf.sourceSets.flatMap { it.javaDirectories }) | |
| executionData.setFrom(file("$buildDir/jacoco/$testTaskName.exec")) | |
| } | |
| jacocoTestReport.dependsOn(reportTask) | |
| }) | |
| } |
Hi @agustinsivoplas , have you added this code in build.gradle.kts directly?
In my case i am creating new file called jacoco.gradel.kts and applying that file in my all modules build.gradle.kts like this
apply(from - "../jacoco.gradle.kts")
so it gives me error saying unresolved reference android
here is the main error issue

@akki2298 Not sure if it was the same issue, but adding the code directly to build.gradle.kts instead of applying from jacoco.gradle.kts resolved an error for me. Definitely worth a try I would say.
@ThomasGoehringer @agustinsivoplas yes it is working then but since i have multiple modules there will be lot of duplicate code so i want to have it common place
For other people who wonder why apply(from - "../jacoco.gradle.kts") doesn't work, check this article - https://medium.com/@kurtlemond/migrating-jacoco-reports-gradle-tasks-to-kotlin-dsl-7b566d89ea92
TLDR: Dynamic resolution, which Groovy DSL provides, does not exists in Kotlin DSL.
I ended up using "Precompiled Script Plugin".
I just added kotlin-dsl-precompiled-script-plugins to buildSrc/build.gradle.kts and created jacoco-reports.gradle.kts under buildSrc/src/main/kotlin folder. Then you can add id("jacoco-reports") plugin to your modules.
@DenisShov other solution would be to revert jacoco.gradle.kts to groovy right ?
@akki2298 sorry for the delay, can you post an image of the issue? This is my version (works fine)