Skip to content

Commit

Permalink
Split okhttp core into Android and JVM (#8600)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke authored Dec 27, 2024
1 parent 6947be1 commit 5b2a1e1
Show file tree
Hide file tree
Showing 322 changed files with 1,824 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.internal.platform.AndroidPlatform
import okio.IOException

class MainActivity : ComponentActivity() {
Expand All @@ -31,6 +32,9 @@ class MainActivity : ComponentActivity() {

val client = OkHttpClient()

// Ensure we are compiling against the right variant
println(AndroidPlatform.isSupported)

val url = "https://github.com/square/okhttp".toHttpUrl()
println(url.topPrivateDomain())

Expand Down
116 changes: 76 additions & 40 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.dokka.gradle.DokkaTaskPartial
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import ru.vyarus.gradle.plugin.animalsniffer.AnimalSnifferExtension
import java.net.URI
Expand Down Expand Up @@ -93,11 +94,16 @@ subprojects {

apply(plugin = "checkstyle")
apply(plugin = "ru.vyarus.animalsniffer")
apply(plugin = "biz.aQute.bnd.builder")
apply(plugin = "io.github.usefulness.maven-sympathy")

// The 'java' plugin has been applied, but it is not compatible with the Android plugins.
// These are applied inside the okhttp module for that case specifically
if (project.name != "okhttp") {
apply(plugin = "biz.aQute.bnd.builder")
apply(plugin = "io.github.usefulness.maven-sympathy")
}

// Skip samples parent
if (project.buildFile.exists()) {
if (project.buildFile.exists() && project.name != "okhttp") {
apply(plugin = "com.android.lint")

dependencies {
Expand All @@ -109,9 +115,11 @@ subprojects {
options.encoding = Charsets.UTF_8.toString()
}

configure<JavaPluginExtension> {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
if (plugins.hasPlugin(JavaBasePlugin::class.java)) {
extensions.configure<JavaPluginExtension> {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
}

Expand All @@ -126,33 +134,38 @@ subprojects {
}
}

configure<CheckstyleExtension> {
config = resources.text.fromArchiveEntry(checkstyleConfig, "google_checks.xml")
toolVersion = rootProject.libs.versions.checkStyle.get()
sourceSets = listOf(project.sourceSets["main"])
}
// Handled in :okhttp directly
if (project.name != "okhttp") {
configure<CheckstyleExtension> {
config = resources.text.fromArchiveEntry(checkstyleConfig, "google_checks.xml")
toolVersion = rootProject.libs.versions.checkStyle.get()
sourceSets = listOf(project.sourceSets["main"])
}

// Animal Sniffer confirms we generally don't use APIs not on Java 8.
configure<AnimalSnifferExtension> {
annotation = "okhttp3.internal.SuppressSignatureCheck"
sourceSets = listOf(project.sourceSets["main"])
// Animal Sniffer confirms we generally don't use APIs not on Java 8.
configure<AnimalSnifferExtension> {
annotation = "okhttp3.internal.SuppressSignatureCheck"
sourceSets = listOf(project.sourceSets["main"])
}
}

val signature: Configuration by configurations.getting
dependencies {
// No dependency requirements for testing-support.
if (project.name == "okhttp-testing-support") return@dependencies

// okhttp configured specifically.
if (project.name == "okhttp") return@dependencies

if (project.name == "mockwebserver3-junit5") {
// JUnit 5's APIs need java.util.function.Function and java.util.Optional from API 24.
signature(rootProject.libs.signature.android.apilevel24) { artifact { type = "signature" } }
"signature"(rootProject.libs.signature.android.apilevel24) { artifact { type = "signature" } }
} else {
// Everything else requires Android API 21+.
signature(rootProject.libs.signature.android.apilevel21) { artifact { type = "signature" } }
"signature"(rootProject.libs.signature.android.apilevel21) { artifact { type = "signature" } }
}

// OkHttp requires Java 8+.
signature(rootProject.libs.codehaus.signature.java18) { artifact { type = "signature" } }
"signature"(rootProject.libs.codehaus.signature.java18) { artifact { type = "signature" } }
}

val javaVersionSetting =
Expand All @@ -175,10 +188,15 @@ subprojects {
}
}

val testRuntimeOnly: Configuration by configurations.getting
dependencies {
testRuntimeOnly(rootProject.libs.junit.jupiter.engine)
testRuntimeOnly(rootProject.libs.junit.vintage.engine)
val platform = System.getProperty("okhttp.platform", "jdk9")
val testJavaVersion = System.getProperty("test.java.version", "21").toInt()

if (project.name != "okhttp") {
val testRuntimeOnly: Configuration by configurations.getting
dependencies {
testRuntimeOnly(rootProject.libs.junit.jupiter.engine)
testRuntimeOnly(rootProject.libs.junit.vintage.engine)
}
}

tasks.withType<Test> {
Expand Down Expand Up @@ -211,25 +229,30 @@ subprojects {
tasks.withType<Test>().configureEach {
environment("OKHTTP_ROOT", rootDir)
}
tasks.withType<KotlinJvmTest>().configureEach {
environment("OKHTTP_ROOT", rootDir)
}

if (platform == "jdk8alpn") {
// Add alpn-boot on Java 8 so we can use HTTP/2 without a stable API.
val alpnBootVersion = alpnBootVersion()
if (alpnBootVersion != null) {
val alpnBootJar = configurations.detachedConfiguration(
dependencies.create("org.mortbay.jetty.alpn:alpn-boot:$alpnBootVersion")
).singleFile
tasks.withType<Test> {
jvmArgs("-Xbootclasspath/p:${alpnBootJar}")
if (project.name != "okhttp") {
if (platform == "jdk8alpn") {
// Add alpn-boot on Java 8 so we can use HTTP/2 without a stable API.
val alpnBootVersion = alpnBootVersion()
if (alpnBootVersion != null) {
val alpnBootJar = configurations.detachedConfiguration(
dependencies.create("org.mortbay.jetty.alpn:alpn-boot:$alpnBootVersion")
).singleFile
tasks.withType<Test> {
jvmArgs("-Xbootclasspath/p:${alpnBootJar}")
}
}
} else if (platform == "conscrypt") {
dependencies {
// testRuntimeOnly(rootProject.libs.conscrypt.openjdk)
}
} else if (platform == "openjsse") {
dependencies {
// testRuntimeOnly(rootProject.libs.openjsse)
}
}
} else if (platform == "conscrypt") {
dependencies {
testRuntimeOnly(rootProject.libs.conscrypt.openjdk)
}
} else if (platform == "openjsse") {
dependencies {
testRuntimeOnly(rootProject.libs.openjsse)
}
}

Expand All @@ -246,6 +269,11 @@ subprojects {
languageSettings.optIn("okhttp3.ExperimentalOkHttpApi")
}
}
plugins.withId("org.jetbrains.kotlin.multiplatform") {
kotlinExtension.sourceSets.configureEach {
languageSettings.optIn("okhttp3.ExperimentalOkHttpApi")
}
}
plugins.withId("org.jetbrains.kotlin.android") {
kotlinExtension.sourceSets.configureEach {
languageSettings.optIn("okhttp3.ExperimentalOkHttpApi")
Expand Down Expand Up @@ -316,6 +344,14 @@ subprojects {
}
}

plugins.withId("org.jetbrains.kotlin.jvm") {
val test = tasks.named("test")
tasks.register("jvmTest") {
description = "Get 'gradlew jvmTest' to run the tests of JVM-only modules"
dependsOn(test)
}
}

tasks.wrapper {
distributionType = Wrapper.DistributionType.ALL
}
1 change: 1 addition & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "okhttp-buildSrc"
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ org.gradle.caching=true
org.gradle.parallel=true

android.useAndroidX=true
kotlin.mpp.applyDefaultHierarchyTemplate=false

androidBuild=false
graalBuild=false
Expand Down
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ gradlePlugin-androidJunit5 = "de.mannodermaus.gradle.plugins:android-junit5:1.11
gradlePlugin-animalsniffer = "ru.vyarus:gradle-animalsniffer-plugin:1.7.2"
gradlePlugin-binaryCompatibilityValidator = "org.jetbrains.kotlinx.binary-compatibility-validator:org.jetbrains.kotlinx.binary-compatibility-validator.gradle.plugin:0.17.0"
gradlePlugin-bnd = { module = "biz.aQute.bnd:biz.aQute.bnd.gradle", version.ref = "biz-aQute-bnd" }
gradlePlugin-dokka = "org.jetbrains.dokka:dokka-gradle-plugin:1.9.20"
gradlePlugin-dokka = "org.jetbrains.dokka:dokka-gradle-plugin:2.0.0"
gradlePlugin-errorprone = "net.ltgt.gradle:gradle-errorprone-plugin:4.1.0"
gradlePlugin-graalvmBuildTools = "org.graalvm.buildtools.native:org.graalvm.buildtools.native.gradle.plugin:0.10.4"
gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "org-jetbrains-kotlin" }
Expand Down Expand Up @@ -78,7 +78,6 @@ kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "
kotlin-stdlib-osgi = { module = "org.jetbrains.kotlin:kotlin-osgi-bundle", version.ref = "org-jetbrains-kotlin" }
kotlin-test-annotations = { module = "org.jetbrains.kotlin:kotlin-test-annotations-common", version.ref = "org-jetbrains-kotlin" }
kotlin-test-common = { module = "org.jetbrains.kotlin:kotlin-test-common", version.ref = "org-jetbrains-kotlin" }
kotlin-test-js = { module = "org.jetbrains.kotlin:kotlin-test-js", version.ref = "org-jetbrains-kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "org-jetbrains-kotlin" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "org-jetbrains-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "org-jetbrains-coroutines" }
Expand Down
1 change: 1 addition & 0 deletions mockwebserver-junit4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
api(libs.junit)

testImplementation(libs.assertk)
testImplementation(libs.junit.vintage.engine)
}

mavenPublishing {
Expand Down
1 change: 0 additions & 1 deletion okcurl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ tasks.shadowJar {
mergeServiceFiles()
}


if (testJavaVersion >= 11) {
apply(plugin = "org.graalvm.buildtools.native")

Expand Down
13 changes: 9 additions & 4 deletions okhttp-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ dependencies {
testImplementation(libs.robolectric)
testImplementation(libs.androidx.espresso.core)
testImplementation(libs.squareup.okio.fakefilesystem)
testImplementation(projects.okhttpTestingSupport)
testImplementation(rootProject.libs.conscrypt.openjdk)

androidTestImplementation(projects.okhttpTls)
androidTestImplementation(libs.assertk)
androidTestImplementation(projects.mockwebserver3Junit4)
androidTestImplementation(libs.androidx.test.runner)
}

mavenPublishing {
// AGP 7.2 embeds Dokka 4, which breaks publishing. Android modules are hardcoded to generate Javadoc instead of Gfm.
configure(com.vanniktech.maven.publish.AndroidSingleVariantLibrary(publishJavadocJar=false))
}
// TODO remove this whole module after merging with okhttp
// Conflicts with KMP :okhttp outputs

//mavenPublishing {
// // AGP 7.2 embeds Dokka 4, which breaks publishing. Android modules are hardcoded to generate Javadoc instead of Gfm.
// configure(com.vanniktech.maven.publish.AndroidSingleVariantLibrary(publishJavadocJar=false))
//}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.platform.android
package okhttp3.android

import java.security.Provider
import javax.net.ssl.SSLContext
Expand All @@ -22,28 +22,25 @@ import okhttp3.DelegatingSSLSocket
import okhttp3.DelegatingSSLSocketFactory
import okhttp3.Protocol.HTTP_1_1
import okhttp3.Protocol.HTTP_2
import okhttp3.testing.PlatformRule
import okhttp3.internal.platform.android.AndroidSocketAdapter
import okhttp3.internal.platform.android.ConscryptSocketAdapter
import okhttp3.internal.platform.android.DeferredSocketAdapter
import okhttp3.internal.platform.android.SocketAdapter
import okhttp3.internal.platform.android.StandardAndroidSocketAdapter
import org.conscrypt.Conscrypt
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeFalse
import org.junit.Assume.assumeTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

class AndroidSocketAdapterTest {
@RegisterExtension @JvmField
val platform = PlatformRule()

@BeforeEach
fun setUp() {
platform.assumeConscrypt()
}
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.ParameterizedRobolectricTestRunner.Parameters

@RunWith(ParameterizedRobolectricTestRunner::class)
class AndroidSocketAdapterTest(val adapter: SocketAdapter) {
val context: SSLContext by lazy {
val provider: Provider = Conscrypt.newProviderBuilder().provideTrustManager(true).build()

Expand All @@ -52,9 +49,8 @@ class AndroidSocketAdapterTest {
}
}

@ParameterizedTest
@MethodSource("data")
fun testMatchesSupportedSocket(adapter: SocketAdapter) {
@Test
fun testMatchesSupportedSocket() {
val socketFactory = context.socketFactory

val sslSocket = socketFactory.createSocket() as SSLSocket
Expand All @@ -65,27 +61,24 @@ class AndroidSocketAdapterTest {
assertNull(adapter.getSelectedProtocol(sslSocket))
}

@ParameterizedTest
@MethodSource("data")
fun testMatchesSupportedAndroidSocketFactory(adapter: SocketAdapter) {
@Test
fun testMatchesSupportedAndroidSocketFactory() {
assumeTrue(adapter is StandardAndroidSocketAdapter)

assertTrue(adapter.matchesSocketFactory(context.socketFactory))
assertNotNull(adapter.trustManager(context.socketFactory))
}

@ParameterizedTest
@MethodSource("data")
fun testDoesntMatchSupportedCustomSocketFactory(adapter: SocketAdapter) {
@Test
fun testDoesntMatchSupportedCustomSocketFactory() {
assumeFalse(adapter is StandardAndroidSocketAdapter)

assertFalse(adapter.matchesSocketFactory(context.socketFactory))
assertNull(adapter.trustManager(context.socketFactory))
}

@ParameterizedTest
@MethodSource("data")
fun testCustomSocket(adapter: SocketAdapter) {
@Test
fun testCustomSocket() {
val socketFactory = DelegatingSSLSocketFactory(context.socketFactory)

assertFalse(adapter.matchesSocketFactory(socketFactory))
Expand All @@ -101,6 +94,7 @@ class AndroidSocketAdapterTest {

companion object {
@JvmStatic
@Parameters(name = "{0}")
fun data(): Collection<SocketAdapter> {
return listOfNotNull(
DeferredSocketAdapter(ConscryptSocketAdapter.factory),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(
sdk = [30],
sdk = [21, 26, 30, 33, 35],
)
class RobolectricOkHttpClientTest {
private lateinit var context: Context
Expand Down
2 changes: 2 additions & 0 deletions okhttp-idna-mapping-table/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ dependencies {
testImplementation(libs.assertk)
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.junit.jupiter.params)

testImplementation(rootProject.libs.junit.jupiter.engine)
}

animalsniffer {
Expand Down
Loading

0 comments on commit 5b2a1e1

Please sign in to comment.