Skip to content

Share Robolectric JVM setup in a convention plugin#9598

Closed
yschimke wants to merge 1 commit into
lysine-dev:masterfrom
yschimke:agent/robolectric-jdk-conventions
Closed

Share Robolectric JVM setup in a convention plugin#9598
yschimke wants to merge 1 commit into
lysine-dev:masterfrom
yschimke:agent/robolectric-jdk-conventions

Conversation

@yschimke

Copy link
Copy Markdown
Collaborator

okhttp and android-test both configure their test JVMs for Robolectric, in different ways:

  • android-test opened nine JDK packages through AGP's unitTests DSL.
  • okhttp stripped the --add-opens flags AGP adds, for the Java 8 build.

This moves both into a new okhttp.robolectric-conventions plugin, so there's one place describing what Robolectric needs from the JVM.

It also adds a guard for Android SDKs the test JVM can't sandbox. Robolectric needs Java 21 to create an SDK 37 sandbox and fails the test class outright on Java 17:

java.lang.UnsupportedOperationException: Failed to create a Robolectric sandbox:
Android SDK 37 requires Java 21 (have Java 17)

Setting robolectric.enabledSdks below Java 21 leaves those tests unselected instead. DefaultSdkPicker.selectSdks intersects a test's configured SDKs with that set, and an empty intersection yields no children rather than an error. Preferred over excluding test classes by name in the build, since it needs no bookkeeping as more SDK 37 tests arrive.

No test currently requests SDK 37, so this is a no-op on master today — it's groundwork for #9596, which adds the first one.

Test plan

  • :okhttp:testAndroidHostTest and :android-test:test on Java 21 and with -Ptest.java.version=17; 81 host tests either way, unchanged from master.
  • spotlessCheck.

Both okhttp and android-test configure their test JVMs for Robolectric, in
different ways: android-test opened nine JDK packages via the Android Gradle
Plugin's unitTests DSL, while okhttp stripped the --add-opens flags the plugin
adds when running on Java 8. Move both into okhttp.robolectric-conventions so
there is one place describing what Robolectric needs from the JVM.

Also skip tests that can only run on an Android SDK the test JVM can't sandbox.
Robolectric needs Java 21 to build an SDK 37 sandbox, and fails the test class
outright on Java 17. Setting robolectric.enabledSdks below Java 21 leaves those
tests unselected instead, which scales as more of them arrive without naming
each one in the build.
@yschimke yschimke closed this Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant