Skip to content

Commit

Permalink
Use Assets or Resources based on platform. (#8636)
Browse files Browse the repository at this point in the history
* Use Assets and Resources based on platform.

Assets uncompressed on Android. Assuming that the AAR file will compress.

Use Resources, compressed on JVM.
  • Loading branch information
yschimke authored Dec 31, 2024
1 parent 1786dd7 commit dd5a002
Show file tree
Hide file tree
Showing 25 changed files with 512 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package okhttp.android.test

import android.content.Context
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.gms.common.GooglePlayServicesNotAvailableException
import com.google.android.gms.security.ProviderInstaller
Expand Down Expand Up @@ -63,6 +65,7 @@ import okhttp3.internal.http2.Http2
import okhttp3.internal.platform.Android10Platform
import okhttp3.internal.platform.AndroidPlatform
import okhttp3.internal.platform.Platform
import okhttp3.internal.platform.PlatformRegistry
import okhttp3.logging.LoggingEventListener
import okhttp3.testing.PlatformRule
import okhttp3.tls.HandshakeCertificates
Expand Down Expand Up @@ -117,6 +120,9 @@ class OkHttpTest {

@BeforeEach
fun setup(server: MockWebServer) {
// Needed because of Platform.resetForTests
PlatformRegistry.applicationContext = ApplicationProvider.getApplicationContext<Context>()

this.server = server
}

Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ org-jetbrains-coroutines = "1.10.1"
org-jetbrains-kotlin = "2.1.0"
org-junit-jupiter = "5.11.4"
retrofit = "2.11.0"
startupRuntime = "1.2.0"
testcontainers = "1.20.4"

[libraries]
Expand All @@ -25,6 +26,7 @@ androidx-annotation = "androidx.annotation:annotation:1.9.1"
androidx-espresso-core = "androidx.test.espresso:espresso-core:3.6.1"
androidx-junit = "androidx.test.ext:junit:1.2.1"
androidx-lint-gradle = { module = "androidx.lint:lint-gradle", version.ref = "lintGradle" }
androidx-startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "startupRuntime" }
androidx-test-runner = "androidx.test:runner:1.6.2"
animalsniffer-annotations = "org.codehaus.mojo:animal-sniffer-annotations:1.24"
aqute-resolve = { module = "biz.aQute.bnd:biz.aQute.resolve", version.ref = "biz-aQute-bnd" }
Expand Down
58 changes: 42 additions & 16 deletions okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import aQute.bnd.gradle.BundleTaskExtension
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinMultiplatform
import org.gradle.kotlin.dsl.compileOnly
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import ru.vyarus.gradle.plugin.animalsniffer.AnimalSnifferExtension
import java.util.Base64

plugins {
kotlin("multiplatform")
Expand All @@ -15,35 +13,23 @@ plugins {
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish.base")
id("binary-compatibility-validator")
id("io.github.gmazzo.aar2jar") version "1.0.1"
}

val platform = System.getProperty("okhttp.platform", "jdk9")
val testJavaVersion = System.getProperty("test.java.version", "21").toInt()

fun ByteArray.toByteStringExpression(): String {
return "\"${Base64.getEncoder().encodeToString(this@toByteStringExpression)}\".decodeBase64()!!"
}

val copyKotlinTemplates = tasks.register<Copy>("copyKotlinTemplates") {
val kotlinTemplatesOutput = layout.buildDirectory.dir("generated/sources/kotlinTemplates")

from("src/commonJvmAndroid/kotlinTemplates")
into(kotlinTemplatesOutput)

// Tag as an input to regenerate after an update
inputs.file("src/jvmTest/resources/okhttp3/internal/publicsuffix/PublicSuffixDatabase.gz")

filteringCharset = Charsets.UTF_8.toString()

val databaseGz = project.file("src/jvmTest/resources/okhttp3/internal/publicsuffix/PublicSuffixDatabase.gz")
val listBytes = databaseGz.readBytes().toByteStringExpression()

expand(
// Build & use okhttp3/internal/-InternalVersion.kt
"projectVersion" to project.version,

// Build okhttp3/internal/publicsuffix/EmbeddedPublicSuffixList.kt
"publicSuffixListBytes" to listBytes
)
}

Expand Down Expand Up @@ -90,13 +76,27 @@ kotlin {
}
}

commonTest {
dependencies {
implementation(projects.okhttpTestingSupport)
implementation(libs.assertk)
implementation(libs.kotlin.test.annotations)
implementation(libs.kotlin.test.common)
implementation(libs.kotlin.test.junit)
implementation(libs.junit)
implementation(libs.junit.jupiter.api)
implementation(libs.junit.jupiter.params)
}
}

androidMain {
dependsOn(commonJvmAndroid)
dependencies {
compileOnly(libs.bouncycastle.bcprov)
compileOnly(libs.bouncycastle.bctls)
compileOnly(libs.conscrypt.openjdk)
implementation(libs.androidx.annotation)
implementation(libs.androidx.startup.runtime)
}
}

Expand Down Expand Up @@ -158,6 +158,20 @@ kotlin {
}
}
}

val androidUnitTest by getting {
dependencies {
implementation(libs.assertk)
implementation(libs.kotlin.test.annotations)
implementation(libs.kotlin.test.common)
implementation(libs.androidx.junit)

implementation(libs.junit.jupiter.engine)
implementation(libs.junit.vintage.engine)

implementation(libs.robolectric)
}
}
}
}

Expand All @@ -184,6 +198,19 @@ android {

consumerProguardFiles("okhttp3.pro")
}

testOptions {
unitTests {
isIncludeAndroidResources = true
}
}

sourceSets {
named("main") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
assets.srcDir("src/androidMain/assets")
}
}
}

// Hack to make BundleTaskExtension pass briefly
Expand Down Expand Up @@ -237,7 +264,6 @@ dependencies {
// Animal Sniffer confirms we generally don't use APIs not on Java 8.
configure<AnimalSnifferExtension> {
annotation = "okhttp3.internal.SuppressSignatureCheck"
sourceSets = listOf(project.sourceSets["main"])
}

configure<CheckstyleExtension> {
Expand Down
17 changes: 14 additions & 3 deletions okhttp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="okhttp.okhttp3">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="okhttp.okhttp3">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"/>

<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data android:name="okhttp3.internal.platform.PlatformInitializer"
android:value="androidx.startup"/>
</provider>
</application>

</manifest>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package okhttp3.internal.platform

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.security.NetworkSecurityPolicy
import android.util.CloseGuard
Expand All @@ -34,7 +35,9 @@ import okhttp3.internal.tls.CertificateChainCleaner

/** Android 10+ (API 29+). */
@SuppressSignatureCheck
class Android10Platform : Platform() {
class Android10Platform : Platform(), ContextAwarePlatform {
override var applicationContext: Context? = null

private val socketAdapters =
listOfNotNull(
Android10SocketAdapter.buildIfSupported(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package okhttp3.internal.platform

import android.content.Context
import android.os.Build
import android.security.NetworkSecurityPolicy
import android.util.Log
Expand Down Expand Up @@ -42,7 +43,9 @@ import okhttp3.internal.tls.TrustRootIndex

/** Android 5 to 9 (API 21 to 28). */
@SuppressSignatureCheck
class AndroidPlatform : Platform() {
class AndroidPlatform : Platform(), ContextAwarePlatform {
override var applicationContext: Context? = null

private val socketAdapters =
listOfNotNull(
StandardAndroidSocketAdapter.buildIfSupported(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.platform

import android.content.Context

interface ContextAwarePlatform {
var applicationContext: Context?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.platform

import android.content.Context
import androidx.startup.Initializer

/**
* Androidx Startup initializer to ensure that the AndroidPlatform has access to the application context.
*/
class PlatformInitializer : Initializer<Platform> {
override fun create(context: Context): Platform {
PlatformRegistry.applicationContext = context

return Platform.get()
}

override fun dependencies(): List<Class<Initializer<*>>> = listOf()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.platform

import android.content.Context
import okhttp3.internal.platform.android.AndroidLog

actual object PlatformRegistry {
Expand All @@ -10,4 +26,10 @@ actual object PlatformRegistry {

actual val isAndroid: Boolean
get() = true

var applicationContext: Context?
get() = (Platform.get() as? ContextAwarePlatform)?.applicationContext
set(value) {
(Platform.get() as? ContextAwarePlatform)?.applicationContext = value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.publicsuffix

import java.io.IOException
import okhttp3.internal.platform.PlatformRegistry
import okio.Source
import okio.source

internal class AssetPublicSuffixList(
override val path: String = PUBLIC_SUFFIX_RESOURCE,
) : BasePublicSuffixList() {
override fun listSource(): Source {
val assets =
PlatformRegistry.applicationContext?.assets ?: throw IOException("Platform applicationContext not initialized")

return assets.open(path).source()
}

companion object {
val PUBLIC_SUFFIX_RESOURCE = "PublicSuffixDatabase.list"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.publicsuffix

internal actual val PublicSuffixList.Companion.Default: PublicSuffixList
get() = AssetPublicSuffixList()
Loading

0 comments on commit dd5a002

Please sign in to comment.