WIP: Fixes code coverage

Change-Id: I04b4fcdd11824a72124da7122c799732a039ded9
ja/coverage
Jose Alcérreca 10 months ago
parent 3e4aad3210
commit 8bf51cfae3

@ -43,6 +43,7 @@ android {
buildTypes { buildTypes {
debug { debug {
enableAndroidTestCoverage = true
applicationIdSuffix = NiaBuildType.DEBUG.applicationIdSuffix applicationIdSuffix = NiaBuildType.DEBUG.applicationIdSuffix
} }
release { release {

@ -27,7 +27,8 @@ class AndroidLibraryJacocoConventionPlugin : Plugin<Project> {
apply("org.gradle.jacoco") apply("org.gradle.jacoco")
apply("com.android.library") apply("com.android.library")
} }
val extension = extensions.getByType<LibraryAndroidComponentsExtension>() val extension: LibraryAndroidComponentsExtension = extensions.getByType<LibraryAndroidComponentsExtension>()
configureJacoco(extension) configureJacoco(extension)
} }
} }

@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid
import com.android.build.api.variant.AndroidComponentsExtension import com.android.build.api.variant.AndroidComponentsExtension
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.register import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType import org.gradle.kotlin.dsl.withType
@ -46,30 +47,52 @@ internal fun Project.configureJacoco(
toolVersion = libs.findVersion("jacoco").get().toString() toolVersion = libs.findVersion("jacoco").get().toString()
} }
val jacocoTestReport = tasks.create("jacocoTestReport") val jacocoTestReport = tasks.create("jacocoTestReport")
androidComponentsExtension.onVariants { variant -> androidComponentsExtension.onVariants { variant ->
val testTaskName = "test${variant.name.capitalize()}UnitTest" val jvmTestTaskName = "test${variant.name.capitalize()}UnitTest"
val androidTestTaskName = "connected${variant.name.capitalize()}AndroidTest"
val buildDir = layout.buildDirectory.get().asFile val buildDir = layout.buildDirectory.get().asFile
val reportTask = tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport::class) { listOf(jvmTestTaskName, androidTestTaskName).forEach { testTaskName ->
dependsOn(testTaskName) val reportTask =
tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport::class) {
dependsOn(testTaskName)
reports {
xml.required.set(true)
html.required.set(true)
}
classDirectories.setFrom(
// TODO: Hack suggested in https://issuetracker.google.com/161300933
// fileTree("$buildDir/tmp/kotlin-classes/${variant.name}") {
// exclude(coverageExclusions)
// }
reports { fileTree("$buildDir/intermediates/classes/${variant.name}/transform${variant.name.capitalize()}ClassesWithAsm/dirs") {
xml.required.set(true) exclude(coverageExclusions)
html.required.set(true) }
} )
classDirectories.setFrom( sourceDirectories.setFrom(
fileTree("$buildDir/tmp/kotlin-classes/${variant.name}") { files(
exclude(coverageExclusions) "$projectDir/src/main/java",
"$projectDir/src/main/kotlin"
)
)
executionData.setFrom(
file("$buildDir/jacoco/$testTaskName.exec"),
file("$buildDir/**/coverage.ec"),
// Beware of using '**' with directories that contain spaces!
project.fileTree("$buildDir/outputs/code_coverage")
.matching { include("**/*.ec") }
)
} }
)
sourceDirectories.setFrom(files("$projectDir/src/main/java", "$projectDir/src/main/kotlin")) jacocoTestReport.dependsOn(reportTask)
executionData.setFrom(file("$buildDir/jacoco/$testTaskName.exec"))
} }
jacocoTestReport.dependsOn(reportTask)
} }
tasks.withType<Test>().configureEach { tasks.withType<Test>().configureEach {

@ -2,7 +2,7 @@
accompanist = "0.32.0" accompanist = "0.32.0"
androidDesugarJdkLibs = "2.0.4" androidDesugarJdkLibs = "2.0.4"
# AGP and tools should be updated together # AGP and tools should be updated together
androidGradlePlugin = "8.3.0" androidGradlePlugin = "8.4.0-alpha13"
androidTools = "31.3.0" androidTools = "31.3.0"
androidxActivity = "1.8.0" androidxActivity = "1.8.0"
androidxAppCompat = "1.6.1" androidxAppCompat = "1.6.1"
@ -17,7 +17,7 @@ androidxDataStore = "1.0.0"
androidxEspresso = "3.5.1" androidxEspresso = "3.5.1"
androidxHiltNavigationCompose = "1.2.0" androidxHiltNavigationCompose = "1.2.0"
androidxLifecycle = "2.7.0" androidxLifecycle = "2.7.0"
androidxMacroBenchmark = "1.2.2" androidxMacroBenchmark = "1.2.3"
androidxMetrics = "1.0.0-alpha04" androidxMetrics = "1.0.0-alpha04"
androidxNavigation = "2.7.4" androidxNavigation = "2.7.4"
androidxProfileinstaller = "1.3.1" androidxProfileinstaller = "1.3.1"

@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000 networkTimeout=10000
validateDistributionUrl=true validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME

Loading…
Cancel
Save