Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Maintenance build (#1)
Browse files Browse the repository at this point in the history
* update minSdk

* Kotlin 1.6.10

* migrate to Kotlin DSL

* Gradle 7.3.3

* AGP 7.1.1

* update Project name

* add dokka

* hide internal functions

* add README

* setup jitpack
  • Loading branch information
niusounds authored Feb 7, 2022
1 parent cac06c0 commit 43528f3
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 104 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ktor-client-cronet-engine

Ktor engine implementation using [Cronet](https://developer.android.com/guide/topics/connectivity/cronet) as backend which enables HTTP/3 with [ktor-client](https://ktor.io/docs/getting-started-ktor-client.html).

THIS IS **NOT** AN OFFICIAL IMPLEMENTATION.

Still very early stage. Currently simple `GET` request is only supported.

## Add dependencies

TODO

## Create the client

```kotlin
val client = HttpClient(
engine = Cronet.create {
context = applicationContext
config = { // this: CronetEngine.Builder
enableBrotli(true)
enableQuic(true)
}
}
)
```

You can pass existing CronetEngine instead.

```kotlin
val client = HttpClient(
engine = Cronet.create {
preconfigured = CronetEngine.Builder(applicationContext)
.enableBrotli(true)
.enableQuic(true)
.build()
}
)
```
52 changes: 0 additions & 52 deletions app/build.gradle

This file was deleted.

53 changes: 53 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
compileSdk = 31

defaultConfig {
applicationId = "com.niusounds.cronettest"
minSdk = 16
targetSdk = 31
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}

dependencies {
implementation(project(":cronet-engine"))
implementation("io.ktor:ktor-client-core:1.6.7")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")

implementation("com.google.android.gms:play-services-cronet:18.0.1")
implementation("androidx.core:core-ktx:1.7.0")
implementation("androidx.appcompat:appcompat:1.4.1")
implementation("com.google.android.material:material:1.5.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}
10 changes: 0 additions & 10 deletions build.gradle

This file was deleted.

11 changes: 11 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "7.1.1" apply false
id("com.android.library") version "7.1.1" apply false
id("org.jetbrains.kotlin.android") version "1.6.10" apply false
id("org.jetbrains.dokka") version "1.6.10" apply false
}

tasks.create<Delete>("clean") {
delete(rootProject.buildDir)
}
35 changes: 0 additions & 35 deletions cronet-engine/build.gradle

This file was deleted.

53 changes: 53 additions & 0 deletions cronet-engine/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("maven-publish")
id("org.jetbrains.dokka")
}

group = "com.github.niusounds"
version = "0.0.1"

android {
compileSdk = 31

defaultConfig {
minSdk = 16
targetSdk = 31

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation("io.ktor:ktor-client-core:1.6.7")
implementation("com.google.android.gms:play-services-cronet:18.0.1")
}

afterEvaluate {
publishing {
publications {
create<MavenPublication>("release") {
from(components.findByName("release"))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package com.niusounds.ktor.client.engine.cronet
import io.ktor.client.*
import io.ktor.client.engine.*

/**
* [HttpClientEngineFactory] using a [CronetEngine] based backend implementation
* with the associated configuration [CronetConfig].
*/
object Cronet : HttpClientEngineFactory<CronetConfig> {
override fun create(block: CronetConfig.() -> Unit): HttpClientEngine =
CronetEngine(CronetConfig().apply(block))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import android.content.Context
import io.ktor.client.engine.*
import org.chromium.net.CronetEngine

/**
* Configuration for [Cronet] client engine.
*/
class CronetConfig : HttpClientEngineConfig() {
/**
* Preconfigured CronetEngine instance instead of configuring one.
*/
var preconfigured: CronetEngine? = null

var context: Context? = getCurrentApplicationContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class CronetEngine(
}
}

fun UrlResponseInfo.toHttpResponseData(
private fun UrlResponseInfo.toHttpResponseData(
requestTime: GMTDate,
callContext: CoroutineContext,
responseBody: ByteArray? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import android.content.Context
* I strongly recommend to not to rely auto initialization and
* manually pass [CronetEngine] to HttpClient.
*/
fun getCurrentApplicationContext(): Context? =
internal fun getCurrentApplicationContext(): Context? =
runCatching {
Class.forName("android.app.ActivityThread")
.getMethod("currentApplication")
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.disableAutomaticComponentCreation=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Feb 04 22:07:56 JST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Empty file modified gradlew
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jdk:
- openjdk11
6 changes: 3 additions & 3 deletions settings.gradle → settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ dependencyResolutionManagement {
mavenCentral()
}
}
rootProject.name = "CronetTest"
include ':app'
include ':cronet-engine'
rootProject.name = "ktor-client-cronet-engine"
include(":app")
include(":cronet-engine")

0 comments on commit 43528f3

Please sign in to comment.