buildSrc/build.gradle.kts:
Код: Выделить всё
plugins {
`kotlin-dsl`
}
repositories {
google()
mavenCentral()
}
dependencies {
compileOnly(libs.androidtools.build.gradle)
compileOnly(libs.kotlin.gradle.plugin)
}
// Isolate buildSrc dependencies from main project
configurations.all {
resolutionStrategy {
// Prevent buildSrc dependencies from affecting main project
preferProjectModules()
}
}
Код: Выделить всё
class JacocoCoveragePlugin : Plugin
{
override fun apply(project: Project) {
// Apply jacoco plugin locally so tasks/types are available
project.plugins.apply("jacoco")
// Wait until the Android plugin is applied to the project
project.pluginManager.withPlugin("com.android.application") {
configureForAndroid(project)
}
project.pluginManager.withPlugin("com.android.library") {
configureForAndroid(project)
}
}
private fun configureForAndroid(project: Project) {
val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
androidComponents.onVariants { variant ->
val variantName = variant.name
if (!variantName.contains("myVariant", ignoreCase = true)) return@onVariants
val capitalizedVariant = variantName.replaceFirstChar { it.uppercaseChar() }
val testTaskName = "test${capitalizedVariant}UnitTest"
val reportTaskName = "jacoco${capitalizedVariant}Report"
// Register the Jacoco report task
project.tasks.register(reportTaskName) {
group = "Reporting"
description = "Generate Jacoco coverage report for $variantName"
mustRunAfter(testTaskName)
reports {
xml.required.set(true)
html.required.set(true)
xml.outputLocation.set(project.layout.buildDirectory.file("reports/jacoco/${variantName}/coverage.xml"))
html.outputLocation.set(project.layout.buildDirectory.dir("reports/jacoco/${variantName}/html"))
}
val kotlinClasses = project.fileTree(project.layout.buildDirectory.dir("tmp/kotlin-classes/$variantName")) {
include(PACKAGE_INCLUDE_WILDCARD)
}
val javaClasses = project.fileTree(project.layout.buildDirectory.dir("intermediates/javac/$variantName/classes")) {
include(PACKAGE_INCLUDE_WILDCARD)
}
val mergedClasses = project.fileTree(project.layout.buildDirectory.dir("intermediates/packaged-classes/$variantName")) {
include(PACKAGE_INCLUDE_WILDCARD)
}
classDirectories.setFrom(
listOf(kotlinClasses, javaClasses, mergedClasses)
.filter { it.dir.exists() }
)
sourceDirectories.setFrom(
listOf(
"${project.projectDir}/src/main/java",
"${project.projectDir}/src/main/kotlin"
).map { project.file(it) }
)
executionData.setFrom(
project.fileTree(project.layout.buildDirectory) {
include(
"jacoco/test${capitalizedVariant}UnitTest.exec",
"outputs/unit_test_code_coverage/${variantName}UnitTest/test${capitalizedVariant}UnitTest.exec"
)
}
)
}
// Wire up test task after all tasks are created
project.afterEvaluate {
project.tasks.withType(Test::class.java).matching { it.name == testTaskName }.configureEach {
finalizedBy(reportTaskName)
ignoreFailures = project.gradle.startParameter.taskNames.any {
it.contains(reportTaskName)
}
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... tom-plugin
Мобильная версия