Using Ktor on Android

Alex Queudot
L+R Engineers
Published in
4 min readJun 16, 2020

--

Ktor logo
https://github.com/ktorio/ktor

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:

  1. Engine: The HTTP client used to perform network requests. This article uses the Android/JVM OkHttp engine.
  2. 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.

--

--