Ktor is separated into several libraries, but regardless of what you want to do, you will need to add these two dependencies (at the very least):
dependencies {
implementation("io.ktor:ktor-client-core:2.3.12")
implementation("io.ktor:ktor-client-android:2.3.12")
}
The core dependency, as the name implies, holds the main client functionality, while the second dependency is the ktor engine. An engine in ktor is responsible for handling the network requests and there are different variants of engines for different platforms. Since we are developing on Android, we need the Android engine.
Since we want to also work with JSON in our response and to deserialize it, we will add the following package:
dependencies {
implementation("io.ktor:ktor-client-core:2.3.12")
implementation("io.ktor:ktor-client-android:2.3.12")
implementation("io.ktor:ktor-client-content-negotiation:2.3.12") //}
The Content Negotiation dependency is responsible for serializing/deserializing the data into a specific format (I.E. JSON)
More on serialization in Kotlin and Jetpack Compose later in this article
Now we can start by creating our HttpClient.
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Androidval myHttpClient = HttpClient(Android)
Pretty simple, right? We basically instantiated a ktor HttpClient and passed as an argument the specific Engine type we want. Since we want to make our HttpClient also handle JSON, we need to install a configuration to this client. This object is known as the HttpClientConfig and allows us to configure how our HttpClient will work in many ways.
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.kotlinx.json.jsonval myHttpClient = HttpClient(Android) {
install(ContentNegotiation) {
json()
}
}
Here, we are telling ktor to install the ContentNegotiation plugin and we are configuring this plugin to work with the JSON serializer. There is a lot more that you can configure inside the HttpClientConfig. For example, you can set the request timeout in milliseconds by installing the HttpTimeout extension:
val myHttpClient = HttpClient(Android) {
install(HttpTimeout) {
requestTimeoutMillis = 10000
}
install(ContentNegotiation) {
json()
}
}
I have covered setting up serialization in Jetpack Compose in a previous article and you can read it here:
But if I would have to condense that article down into a few lines, it would be these:
- Understand which Jetpack Compose version you are working with
- This will tell you which Kotlin version you need to work with
- Then you must find a corresponding kotlin-serialization library version that matches the Kotlin version you found in step #2
Once you have all that figured out, go to your project level build.gradle.kts file you put the following in the plugins block:
plugins {
...
kotlin("jvm") version "1.5.0"
kotlin("plugin.serialization") version "1.5.0"
}
And in your application level build.gradle.kts file, modify the plugins block and the dependencies block:
plugins {
...
id("org.jetbrains.kotlin.plugin.serialization")
}...
dependencies {
...
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
}