Last active
August 29, 2024 09:09
-
-
Save alexvanyo/9a3b38ef6c07907cfa7f9a4310c628bd to your computer and use it in GitHub Desktop.
Kotlin DSL JaCoCo configuration for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@DenisShov other solution would be to revert jacoco.gradle.kts to groovy right ?