build: count Robolectric-run code in unit-test coverage
- Commit
9b6647a31deb0cf8ccd05fa0941d936ff0cabd24- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-06 17:05 UTC (29 days ago)
- Parents
4cf0d1a
Robolectric's sandbox loader defines classes without a code-source location and JaCoCo skipped them (inclnolocationclasses=false), so code exercised only under Robolectric reported 0%. Enable it on AGP's unit-test JaCoCo agent, keeping jdk.internal.* excluded. emergency EmergencyInfo: 0% -> 85%.
Diff
build-logic/src/main/kotlin/TestCoverage.kt
diff --git a/build-logic/src/main/kotlin/TestCoverage.kt b/build-logic/src/main/kotlin/TestCoverage.ktindex 39723c249f..16e7dcd40f 100644--- a/build-logic/src/main/kotlin/TestCoverage.kt+++ b/build-logic/src/main/kotlin/TestCoverage.kt@@ -99,3 +99,20 @@ fun org.gradle.api.Project.mergedCoverageClassDirs(variant: String): org.gradle. MERGED_COVERAGE_EXCLUDES.forEach { exclude(it) } } }++/**+ * Robolectric defines classes in its sandbox loader without a code-source location, and+ * JaCoCo skips such classes by default, so Robolectric-run code reported 0% coverage. AGP+ * attaches Gradle's JacocoTaskExtension to unit Test tasks; flip it on there.+ * `jdk.internal.*` must stay excluded or JaCoCo instruments JDK internals and the JVM crashes.+ */+fun org.gradle.api.Project.includeRobolectricClassesInCoverage() {+ afterEvaluate {+ tasks.withType(org.gradle.api.tasks.testing.Test::class.java).configureEach {+ extensions.findByType(org.gradle.testing.jacoco.plugins.JacocoTaskExtension::class.java)?.apply {+ isIncludeNoLocationClasses = true+ excludes = (excludes.orEmpty() + "jdk.internal.*").distinct()+ }+ }+ }+}
build-logic/src/main/kotlin/common-conventions-app.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-app.gradle.kts b/build-logic/src/main/kotlin/common-conventions-app.gradle.ktsindex 8359eb0bc7..2b6b431c6c 100644--- a/build-logic/src/main/kotlin/common-conventions-app.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-app.gradle.kts@@ -22,6 +22,7 @@ val libs = the<org.gradle.accessors.dm.LibrariesForLibs>() // every Test task builds + links the host libs of this module and each project // dep that registers one. Lazy — assemble/install never build them. wireRustHostLibsForUnitTests()+includeRobolectricClassesInCoverage() /** * version.txt is read once per build (not once per module): the first
build-logic/src/main/kotlin/common-conventions-library.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-library.gradle.kts b/build-logic/src/main/kotlin/common-conventions-library.gradle.ktsindex 666e76ac96..e175d7e192 100644--- a/build-logic/src/main/kotlin/common-conventions-library.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-library.gradle.kts@@ -21,6 +21,7 @@ val libs = the<org.gradle.accessors.dm.LibrariesForLibs>() // every Test task builds + links the host libs of this module and each project // dep that registers one. Lazy — assemble/install never build them. wireRustHostLibsForUnitTests()+includeRobolectricClassesInCoverage() configure<com.android.build.api.dsl.LibraryExtension> { buildFeatures {