KNet

License: MIT

Android network client based on Cronet. This library let you easily use QUIC protocol in your Android projects.
You can see more about QUIC here – mobile or product or backend

Cronet wrapper improvements:

  • Memory Optimizations
  • Multithreading Optimizations
  • Redirects Settings
  • Quic Configurations
  • Timeouts Settings
  • Broken Hosts Clearing
  • Documentation
  • Interceptor System
  • Debug System
  • Metrics
  • Error Handling
  • (soon) Fallback System
  • (soon) Encodings
  • (soon) Tests
  • (soon) Integrations(ExoPlayer/Ktor/Okhttp/Flipper/Stetho)
  • (soon) Backoff
  • (soon) DNS Prefetch

Gradle

You can try this library using Gradle:

dependencies {
 implementation 'com.vk.knet:core:1.0-alpha02'
 implementation 'com.vk.knet:cronet:1.0-alpha02'
 implementation 'com.vk.knet:okcronet:1.0-alpha02'
}

Initialization

Short version:

val cronet = CronetKnetEngine.Build(App.context) {
    client {
        enableQuic(
            CronetQuic(hints = listOf(Host("drive.google.com", 443)))
        )
    }
}

val knetCronet = Knet.Build(cronet)

Full version:

CronetLogger.global(
    object : CronetHttpLogger {
        override fun error(vararg obj: Any) {
            Log.e("Logos", obj.toList().toString())
        }

        override fun debug(type: CronetHttpLogger.DebugType, vararg obj: Any) {
            Log.d("[${type.name}]", obj.toList().toString())
        }

        override fun info(vararg obj: Any) {
            Log.i("Logos", obj.toList().toString())
        }
    }
)

private val cronet = CronetKnetEngine.Build(App.context) {
    client {
        setCache(CronetCache.Disk(App.context.filesDir, 1024 * 1024 * 10))

        enableHttp2(true)
        enableQuic(
            CronetQuic(
                hints = listOf(
                    Host("drive.google.com", 443)
                ),
            )
        )

        useBrotli(true)
        isClearBrokenHosts(true)

        netlog(CronetLog.Config(netDir, 1024 * 1024 * 100))

        connectTimeout(15, TimeUnit.SECONDS)
        writeTimeout(15, TimeUnit.SECONDS)
        readTimeout(15, TimeUnit.SECONDS)

        nativePool(CronetNativeByteBufferPool.DEFAULT)
        arrayPool(ByteArrayPool.DEFAULT)

        maxConcurrentRequests(50)
        maxConcurrentRequestsPerHost(10)

        followRedirects(true)
        followSslRedirects(true)

        addMetricListener { metric: HttpMetrics, _, _ ->
            Metrics.addMetric(metric)
        }
    }

    addInterceptor { p ->
        try {
            p.proceed(p.request)
        } catch (e: Exception) {
            try {
                println("[KNet] [RETRY ${if (e is ConnectException) e.message else e.localizedMessage}]")
                p.proceed(p.request)
            } catch (e: Exception) {
                e.printStackTrace()
                throw e
            }
        }
    }
}

val knetCronet = Knet.Build(cronet) {
    bufferPool(ByteArrayPool.DEFAULT)
}

OkHTTP

If you’re now using OkHttp, you can simply wrap KNet into OkHttp Interceptor… Easy, right?

val knetCronet = Knet.Build(cronet)
val okhttpInterceptor = KnetToOkHttpInterceptor(knetCronet)

val builder = OkHttpClient.Builder()
builder.addInterceptor(okhttpInterceptor)
val client = builder.build()

Comming Soon

  • Tests
  • Exo
  • Ktor
  • OkHttp
  • Flipper
  • Stetho

Licence

The MIT License (MIT)

Copyright (c) 2019 vk.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

GitHub

https://github.com/VKCOM/KNet