Using Ktor on Android
Published in
4 min readJun 16, 2020
Ktor is a coroutine-based networking framework by Jetbrains written in Kotlin. It works with Kotlin Multiplatform, allowing you to share networking code between different platforms.
This article explores the framework setup and the connection with a REST API in your Android project.
Adding Ktor
The Ktor client consists of two main parts:
- Engine: The HTTP client used to perform network requests. This article uses the Android/JVM OkHttp engine.
- Serialization: Processing request and response payloads as JSON and serializing them from/to your data models, using kotlinx Serialization.
Add the dependencies to your project’s Gradle with the following artifacts:
implementation "io.ktor:ktor-client-okhttp:$ktorVersion"
implementation "io.ktor:ktor-client-json:$ktorVersion"
implementation "io.ktor:ktor-client-serialization-jvm:$ktorVersion"
implementation "io.ktor:ktor-client-logging-jvm:$ktorVersion"
This article has been updated for Kotlin 1.5, ktor 1.6.0, and serialization 1.2.1.
The HTTP Client
The start point for every network request is the Ktor HTTP Client.