Skip to content

Latest commit

 

History

History
114 lines (90 loc) · 3.96 KB

File metadata and controls

114 lines (90 loc) · 3.96 KB

okcronet

A network request library similar to OKHTTP, implemented using Cronet

中文

Why

Google provides a bridge implementation from okhttp to Cronet cronet-transport-for-okhttp, but as in README As described in the Incompatibilities content, you cannot use the source code to improve its functionality.

Therefore, the goal of the okcronet project is to solve the problems of HTTP3/QUIC support provided by interceptors implemented by Cronet.

For answers about okhttp's support for HTTP3/QUIC, you can view this issues.

Advantages of okcronet

  • Easy to use, consistent with OkHttp usage
  • Supports HTTP3/QUIC, which can provide better network performance
  • Supports all features of Cronet, such as caching, thread pools, proxies, etc.

Import

You need to reference both the OkCronet library and the Cronet library.

okcronet Library:

implementation("io.github.limuyang2:okcronet:1.0.11")

Cronet Library:

Google Official Cronet:

Way 1

Google Latest Version

implementation("org.chromium.net:cronet-api:141.7340.3")
implementation("org.chromium.net:cronet-common:141.7340.3")
implementation("org.chromium.net:cronet-embedded:141.7340.3")

Way 2

The packages officially provided by Google are not the latest. If you want to use the latest version synchronized with chromium, you can visit the official storage bucket to obtain it.google cloud

Google Play Cronet:

Reference link - android develop

implementation("com.google.android.gms:play-services-cronet:18.1.1")

Using

The usage is consistent with the usage of okhttp. There is just one more CronetEngine creation work.

Build a globally unique CronetEngine

    // cronetEngine needs to maintain global uniqueness and cannot be created repeatedly
    private val cronetEngine: CronetEngine by lazy(LazyThreadSafetyMode.NONE) {
        val httpCacheDir =
            File(
                this.applicationContext.externalCacheDir ?: this.applicationContext.cacheDir,
                "http"
            )

        if (!httpCacheDir.exists()) {
            httpCacheDir.mkdir()
        }

        CronetEngine.Builder(
            NativeCronetEngineBuilderImpl(this.applicationContext)
        )
            .setStoragePath(httpCacheDir.absolutePath)
            .enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1048576)
            .enableHttp2(true)
            .enableQuic(true)
            .setThreadPriority(-1)
            .enableBrotli(true)
            .build()
    }

GET

    // Create CronetClient
    val cronetClient = CronetClient.Builder(cronetEngine).build()

    // Build Request
    val request = Request.Builder()
        .url("https://www.fastly.com/quic-http-3")
        .get()
        .build()

    // Initiate network requests synchronously
    cronetClient.newCall(request).execute()

POST

    // Create CronetClient
    val cronetClient = CronetClient.Builder(cronetEngine).build()

    // Build RequestBody
    val body: RequestBody = "jsonString".toRequestBody()

    // Build Request
    val request = Request.Builder()
        .url("https://www.fastly.com/quic-http-3")
        .post(body)
        .build()

    // Initiate network requests synchronously
    cronetClient.newCall(request).execute()

Proguard

You need to add obfuscation rules for cronet. If you are using the version officially provided by Google, it will be included automatically.

Thanks

okhttp

cronet-transport-for-okhttp