Change-Id: Ic0d68ac1822045a8561828d357614cf246ab6622ja/coverage_agp
parent
3e4aad3210
commit
f003321220
@ -1,87 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.google.samples.apps.nowinandroid
|
|
||||||
|
|
||||||
import com.android.build.api.variant.AndroidComponentsExtension
|
|
||||||
import org.gradle.api.Project
|
|
||||||
import org.gradle.api.tasks.testing.Test
|
|
||||||
import org.gradle.kotlin.dsl.configure
|
|
||||||
import org.gradle.kotlin.dsl.register
|
|
||||||
import org.gradle.kotlin.dsl.withType
|
|
||||||
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
|
|
||||||
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
|
|
||||||
import org.gradle.testing.jacoco.tasks.JacocoReport
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
private val coverageExclusions = listOf(
|
|
||||||
// Android
|
|
||||||
"**/R.class",
|
|
||||||
"**/R\$*.class",
|
|
||||||
"**/BuildConfig.*",
|
|
||||||
"**/Manifest*.*"
|
|
||||||
)
|
|
||||||
|
|
||||||
private fun String.capitalize() = replaceFirstChar {
|
|
||||||
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun Project.configureJacoco(
|
|
||||||
androidComponentsExtension: AndroidComponentsExtension<*, *, *>,
|
|
||||||
) {
|
|
||||||
configure<JacocoPluginExtension> {
|
|
||||||
toolVersion = libs.findVersion("jacoco").get().toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
val jacocoTestReport = tasks.create("jacocoTestReport")
|
|
||||||
|
|
||||||
androidComponentsExtension.onVariants { variant ->
|
|
||||||
val testTaskName = "test${variant.name.capitalize()}UnitTest"
|
|
||||||
val buildDir = layout.buildDirectory.get().asFile
|
|
||||||
val reportTask = tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport::class) {
|
|
||||||
dependsOn(testTaskName)
|
|
||||||
|
|
||||||
reports {
|
|
||||||
xml.required.set(true)
|
|
||||||
html.required.set(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
classDirectories.setFrom(
|
|
||||||
fileTree("$buildDir/tmp/kotlin-classes/${variant.name}") {
|
|
||||||
exclude(coverageExclusions)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
sourceDirectories.setFrom(files("$projectDir/src/main/java", "$projectDir/src/main/kotlin"))
|
|
||||||
executionData.setFrom(file("$buildDir/jacoco/$testTaskName.exec"))
|
|
||||||
}
|
|
||||||
|
|
||||||
jacocoTestReport.dependsOn(reportTask)
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<Test>().configureEach {
|
|
||||||
configure<JacocoTaskExtension> {
|
|
||||||
// Required for JaCoCo + Robolectric
|
|
||||||
// https://github.com/robolectric/robolectric/issues/2230
|
|
||||||
// TODO: Consider removing if not we don't add Robolectric
|
|
||||||
isIncludeNoLocationClasses = true
|
|
||||||
|
|
||||||
// Required for JDK 11 with the above
|
|
||||||
// https://github.com/gradle/gradle/issues/5184#issuecomment-391982009
|
|
||||||
excludes = listOf("jdk.internal.*")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue