Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions android-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import okhttp3.buildsupport.androidBuild

plugins {
id("okhttp.base-conventions")
id("okhttp.robolectric-conventions")
id("com.android.library")
id("de.mannodermaus.android-junit5")
}
Expand Down Expand Up @@ -43,22 +44,6 @@ android {
testOptions {
targetSdk = 37
unitTests.isIncludeAndroidResources = true

// Robolectric 4.17 reflects into JDK internals, which JDK 17+ blocks by default.
// https://robolectric.org/getting-started/
unitTests.all {
it.jvmArgs(
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.util=ALL-UNNAMED",
"--add-opens=java.base/java.io=ALL-UNNAMED",
"--add-opens=java.base/java.net=ALL-UNNAMED",
"--add-opens=java.base/java.security=ALL-UNNAMED",
"--add-opens=java.base/java.text=ALL-UNNAMED",
"--add-opens=java.base/jdk.internal.access=ALL-UNNAMED",
"--add-opens=java.desktop/java.awt.font=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
)
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import okhttp3.buildsupport.testJavaVersion

/**
* Shared configuration for the modules that run Robolectric tests.
*
* Robolectric reflects into JDK internals, which JDK 17+ blocks by default, and its newer Android
* images need a newer JVM. https://robolectric.org/getting-started/
*/

/** Opened so Robolectric can reflect into the JDK internals its shadows and interceptors use. */
val robolectricJvmArgs =
listOf(
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.util=ALL-UNNAMED",
"--add-opens=java.base/java.io=ALL-UNNAMED",
"--add-opens=java.base/java.net=ALL-UNNAMED",
"--add-opens=java.base/java.security=ALL-UNNAMED",
"--add-opens=java.base/java.text=ALL-UNNAMED",
"--add-opens=java.base/jdk.internal.access=ALL-UNNAMED",
"--add-opens=java.desktop/java.awt.font=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
)

tasks.withType<Test>().configureEach {
if (testJavaVersion >= 9) {
jvmArgs(robolectricJvmArgs)
}

if (testJavaVersion < 21) {
// Robolectric needs Java 21 to sandbox Android SDK 37. Tests configured only for SDKs outside
// this set are skipped rather than failing to create a sandbox.
systemProperty("robolectric.enabledSdks", (21..36).joinToString(","))
}
}

if (testJavaVersion < 9) {
afterEvaluate {
tasks.withType<Test> {
// Work around robolectric requirements and limitations. The Android Gradle Plugin adds
// --add-opens itself, which Java 8 doesn't understand.
// https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java;l=339
allJvmArgs = allJvmArgs.filter { !it.startsWith("--add-opens") }
}
}
}
15 changes: 1 addition & 14 deletions okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ plugins {
id("okhttp.jvm-conventions")
id("okhttp.quality-conventions")
id("okhttp.testing-conventions")
id("okhttp.robolectric-conventions")
id("app.cash.burst")
alias(libs.plugins.maven.sympathy)
}
Expand Down Expand Up @@ -324,20 +325,6 @@ project.tasks.withType<AnimalSniffer> {
}
}

afterEvaluate {
tasks.withType<Test> {
if (javaLauncher
.get()
.metadata.languageVersion
.asInt() < 9
) {
// Work around robolectric requirements and limitations
// https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java;l=339
allJvmArgs = allJvmArgs.filter { !it.startsWith("--add-opens") }
}
}
}

// Work around issue 8826, where the Sentry SDK assumes that OkHttp's internal-visibility symbols
// will be suffixed '$okhttp' in deployable artifacts. This isn't intended to be a published API,
// but it's easy enough for us to keep it working. https://github.com/lysine-dev/okhttp/issues/8826
Expand Down
Loading