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 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${[email protected]()}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([email protected]().destinationDir) { | |
exclude(excludes) | |
}, | |
fileTree("$buildDir/tmp/kotlin-classes/${[email protected]}") { | |
exclude(excludes) | |
} | |
) | |
) | |
// Code underneath /src/{variant}/kotlin will also be picked up here | |
sourceDirectories.setFrom([email protected] { 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 ?