Skip to content

Commit b3888a8

Browse files
authored
fix(build): resolve Robolectric SDK jars through Gradle (#2040)
1 parent 0bf9e5e commit b3888a8

9 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎.github/dependabot.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ updates:
1313
- bmc08gt
1414
assignees:
1515
- bmc08gt
16+
ignore:
17+
# Each version is the exact android-all build a Robolectric release loads for one SDK level
18+
# (see the robolectric-sdk* entries in libs.versions.toml). They move with Robolectric, not on
19+
# their own; "newer" builds belong to other SDK levels.
20+
- dependency-name: "org.robolectric:android-all-instrumented"

‎apps/flipcash/app/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins {
1313
id("org.jetbrains.kotlin.plugin.serialization")
1414
id("dagger.hilt.android.plugin")
1515
alias(libs.plugins.google.services)
16+
alias(libs.plugins.flipcash.robolectric.sdks)
1617
alias(libs.plugins.firebase.perf)
1718
alias(libs.plugins.bugsnag.gradle)
1819
alias(libs.plugins.secrets)

‎build-logic/convention/build.gradle.kts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ gradlePlugin {
6565
id = "flipcash.kmp.test.fixtures"
6666
implementationClass = "KmpTestFixturesConventionPlugin"
6767
}
68+
register("robolectricSdks") {
69+
id = "flipcash.robolectric.sdks"
70+
implementationClass = "RobolectricSdksConventionPlugin"
71+
}
6872
register("serviceBoundary") {
6973
id = "flipcash.service.boundary"
7074
implementationClass = "ServiceBoundaryConventionPlugin"

‎build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.android.build.api.dsl.LibraryExtension
2+
import com.getcode.buildlogic.robolectric.useGradleResolvedRobolectricSdks
23
import org.gradle.api.JavaVersion
34
import org.gradle.api.Plugin
45
import org.gradle.api.tasks.testing.Test
@@ -71,6 +72,7 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
7172
failOnNoDiscoveredTests.set(false)
7273
maxHeapSize = "2g"
7374
}
75+
useGradleResolvedRobolectricSdks()
7476

7577
dependencies {
7678
"implementation"(libs.findLibrary("timber").get())

‎build-logic/convention/src/main/kotlin/KmpAppLibraryConventionPlugin.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget
2+
import com.getcode.buildlogic.robolectric.useGradleResolvedRobolectricSdks
23
import org.gradle.api.Plugin
34
import org.gradle.api.Project
45
import org.gradle.api.artifacts.VersionCatalogsExtension
@@ -153,6 +154,8 @@ class KmpAppLibraryConventionPlugin : Plugin<Project> {
153154
failOnNoDiscoveredTests.set(false)
154155
maxHeapSize = "2g"
155156
}
157+
// Only the Android host tests run under Robolectric; jvmTest has no use for the jars.
158+
useGradleResolvedRobolectricSdks { it.name.contains("AndroidHostTest") }
156159
}
157160
}
158161
}

‎build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget
2+
import com.getcode.buildlogic.robolectric.useGradleResolvedRobolectricSdks
23
import org.gradle.api.Plugin
34
import org.gradle.api.Project
45
import org.gradle.api.artifacts.VersionCatalogsExtension
@@ -91,6 +92,9 @@ class KmpLibraryConventionPlugin : Plugin<Project> {
9192
attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
9293
}
9394
}
95+
96+
// Only the Android host tests run under Robolectric; jvmTest has no use for the jars.
97+
useGradleResolvedRobolectricSdks { it.name.contains("AndroidHostTest") }
9498
}
9599
}
96100
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import com.getcode.buildlogic.robolectric.useGradleResolvedRobolectricSdks
2+
import org.gradle.api.Plugin
3+
import org.gradle.api.Project
4+
5+
/**
6+
* Applies [useGradleResolvedRobolectricSdks] on its own, for modules that run Robolectric tests but
7+
* apply none of the flipcash.* conventions that already call it (the app module).
8+
*/
9+
class RobolectricSdksConventionPlugin : Plugin<Project> {
10+
override fun apply(target: Project) = target.useGradleResolvedRobolectricSdks()
11+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.getcode.buildlogic.robolectric
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.artifacts.VersionCatalogsExtension
5+
import org.gradle.api.file.FileCollection
6+
import org.gradle.api.tasks.InputFiles
7+
import org.gradle.api.tasks.Internal
8+
import org.gradle.api.tasks.PathSensitive
9+
import org.gradle.api.tasks.PathSensitivity
10+
import org.gradle.api.tasks.testing.Test
11+
import org.gradle.kotlin.dsl.getByType
12+
import org.gradle.kotlin.dsl.withType
13+
import org.gradle.process.CommandLineArgumentProvider
14+
import java.io.File
15+
import java.util.Properties
16+
17+
private val SDK_ALIASES = listOf("robolectric-sdk24", "robolectric-sdk32", "robolectric-sdk34", "robolectric-sdk35", "robolectric-sdk36")
18+
19+
/**
20+
* Feeds Robolectric its android-all jars from Gradle instead of letting each test JVM fetch them.
21+
*
22+
* Left alone, Robolectric's MavenDependencyResolver downloads a 150-220 MB jar per SDK level into
23+
* `~/.m2` from inside the test JVM. Neither CI workflow caches `~/.m2`, so every run downloads
24+
* again, and a failed fetch fails the whole test class in `classMethod` with
25+
* `AssertionError at MavenArtifactFetcher.java:129`. Resolving the jars as Gradle dependencies puts
26+
* them in the Gradle dependency cache, which CI already restores, and moves the download out of the
27+
* tests.
28+
*
29+
* Robolectric reads the jars' locations from the file named by the `robolectric-deps.properties`
30+
* system property. An SDK missing from that file fails with "no artifacts found for
31+
* DependencyJar{...}" rather than falling back to a download, so a Robolectric bump that renames
32+
* its jars fails loudly.
33+
*/
34+
fun Project.useGradleResolvedRobolectricSdks(testTaskFilter: (Test) -> Boolean = { true }) {
35+
// A module can apply more than one convention that calls this.
36+
if (configurations.findByName("robolectricSdk36") != null) return
37+
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
38+
// One configuration per SDK: the jars are versions of one module, so a shared configuration
39+
// would resolve the conflict to the newest and drop the rest.
40+
val sdks = files(
41+
SDK_ALIASES.map { alias ->
42+
configurations.create(alias.replace("-s", "S")) {
43+
isCanBeConsumed = false
44+
isCanBeResolved = true
45+
isTransitive = false
46+
}.also { dependencies.add(it.name, libs.findLibrary(alias).get()) }
47+
},
48+
)
49+
50+
tasks.withType<Test>().configureEach {
51+
if (!testTaskFilter(this)) return@configureEach
52+
val depsFile = File(temporaryDir, "robolectric-deps.properties")
53+
val args = RobolectricSdkArgs(sdks, depsFile)
54+
jvmArgumentProviders.add(args)
55+
doFirst { args.writeDepsFile() }
56+
}
57+
}
58+
59+
internal class RobolectricSdkArgs(
60+
// The jar names carry the version and the content is fixed per version, so where the Gradle
61+
// cache lives on a machine must not affect the test task's cache key.
62+
@get:InputFiles
63+
@get:PathSensitive(PathSensitivity.NAME_ONLY)
64+
val jars: FileCollection,
65+
@get:Internal
66+
val depsFile: File,
67+
) : CommandLineArgumentProvider {
68+
69+
override fun asArguments(): Iterable<String> =
70+
listOf("-Drobolectric-deps.properties=${depsFile.absolutePath}")
71+
72+
fun writeDepsFile() {
73+
val props = Properties()
74+
jars.files.forEach { jar ->
75+
val version = jar.name.removePrefix("$ARTIFACT-").removeSuffix(".jar")
76+
props.setProperty("org.robolectric:$ARTIFACT:$version", jar.absolutePath)
77+
}
78+
depsFile.parentFile.mkdirs()
79+
// Properties.store escapes the ':' in the coordinates, which load() would otherwise read as
80+
// the key/value separator.
81+
depsFile.writer().use { props.store(it, null) }
82+
}
83+
84+
private companion object {
85+
const val ARTIFACT = "android-all-instrumented"
86+
}
87+
}

‎gradle/libs.versions.toml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ mockito-kotlin = "6.4.0"
108108
mockk = "1.14.12"
109109
turbine = "1.2.1"
110110
robolectric = "4.18"
111+
# The android-all jars Robolectric loads for each SDK level the tests run on (libs/test-utils'
112+
# robolectric.properties pins 36; @Config pins 32, 34 and 35; modules without test-utils on the
113+
# classpath fall back to Robolectric's default, 24). Gradle resolves them so tests don't download
114+
# them at runtime; see RobolectricSdks.kt. Each Robolectric release names its own
115+
# builds in DefaultSdkProvider: bump these with it, or every test on that SDK fails with
116+
# "no artifacts found for DependencyJar{...}".
117+
robolectric-sdk24 = "7.0.0_r1-robolectric-r1-i7"
118+
robolectric-sdk32 = "12.1-robolectric-8229987-i7"
119+
robolectric-sdk34 = "14-robolectric-10818077-i7"
120+
robolectric-sdk35 = "15-robolectric-14663667-2-i7"
121+
robolectric-sdk36 = "16-robolectric-13921718-2-i7"
111122
slf4j-simple = "2.0.20"
112123
androidxUiJUnit = "1.11.4"
113124
mixpanel = "8.12.0"
@@ -346,6 +357,11 @@ testing-androidx-arch-core = { group = "androidx.arch.core", name = "core-testin
346357
testing-androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "androidxTestRules" }
347358
turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" }
348359
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
360+
robolectric-sdk24 = { module = "org.robolectric:android-all-instrumented", version.ref = "robolectric-sdk24" }
361+
robolectric-sdk32 = { module = "org.robolectric:android-all-instrumented", version.ref = "robolectric-sdk32" }
362+
robolectric-sdk34 = { module = "org.robolectric:android-all-instrumented", version.ref = "robolectric-sdk34" }
363+
robolectric-sdk35 = { module = "org.robolectric:android-all-instrumented", version.ref = "robolectric-sdk35" }
364+
robolectric-sdk36 = { module = "org.robolectric:android-all-instrumented", version.ref = "robolectric-sdk36" }
349365
testing-slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j-simple" }
350366
testing-lifecycle = { group = "androidx.lifecycle", name = "lifecycle-runtime-testing", version.ref = "androidx-lifecycle" }
351367
testing-compose-uiTestManifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
@@ -394,6 +410,7 @@ grpc = ["grpc-okhttp", "grpc-kotlin", "grpc-protobuf-lite", "grpc-stub"]
394410

395411
[plugins]
396412
flipcash-android-library = { id = "flipcash.android.library" }
413+
flipcash-robolectric-sdks = { id = "flipcash.robolectric.sdks" }
397414
flipcash-android-library-compose = { id = "flipcash.android.library.compose" }
398415
flipcash-android-feature = { id = "flipcash.android.feature" }
399416
flipcash-android-ed25519-shadow = { id = "flipcash.android.ed25519.shadow" }

0 commit comments

Comments
 (0)