Ktor for Android development

Oğuzhan Aslan
4 min readJul 21, 2021
Photo by lilzidesigns on Unsplash

In Android development, we have multiple libraries to send/or get data from a server such as Retrofit, Volley, AndroidAsync. Since 19 November 2018 (date of 1.0.0 version release) , we have Ktor as well. Ktor is a library that allows us to make requests and get responses from the servers.

The good point about it is that Ktor is an native library which means we can use it whenever we can write code in Kotlin. This feature makes Ktor a good option for cross-platform projects.For instance, it is widely used in Kotlin Multiplatform Mobile(KMM) projects. We can write both server and client side codes by using Ktor. Due to the fact that we are mainly “clients” in Android development, I’m going to talk about client side of Ktor in this blog.

Before we get started , make sure that you are familiar with the Kotlin DSL since Ktor uses that feature of kotlin most of the time.

In order to use Ktor in your project add this dependency in your application level build.gradle file

implementation “io.ktor:ktor-client-core:<version>”

after we can create our client instance.

val client = HttpClient()

Now, we can make requests to the server we use. Familiarly requests can be GET, POST, PUT, DELETE, HEAD, OPTION, or PATCH .

If you want to use an easier way, Ktor has built-in extention functions for for basic HTTP methods .

However, if you try to use the client instance to get responses or make any request now , you probably face with an error. Since we do not have any engine yet. The Engines for Ktor are like somehow plugins for Ktor clients, the client uses the engine to execute the processes we define. There are plenty of engines for our client. So, it depends on you to change one of them by your use case.

CIO — (coroutine-based engine)

CIO is based on our lovely friend coroutines.This makes it good for JVM and Android Applications. Also, it is generally a good choise for KMM (and also other cross-platform) projects, since it is based on native kotlin.

dependency : implementation "io.ktor:ktor-client-cio:<version>"

Android Engine

In the official documentary of Ktor, it is stated that

HttpClientEngineFactory using a UrlConnection based backend implementation without additional dependencies with the the associated configuration AndroidEngineConfig.

You may choose this engine if you are developing application for Android.

dependency :implementation "io.ktor:ktor-client-android:<version>"

Java Engine

This engine can be used in java based applications and also java based Android applications. it uses the Java HTTP Client .

dependency :implementation "io.ktor:ktor-client-java:<version>"

These Engines ,so far, are should be enough to know for our case but as I said there are many of them. For instance, there are an engines for Javascript, IOS and there are also other JVM engines like Apache. If you want to learn more about them please read the official documentation.

So how do we pass the engine to Ktor client ? well that is so easy, we just pass them toHttpClient ‘s constructor.

Moreover, if you try to create HttpClient instance without any paramether, you see that compiler does not give any warnings. That is due to the fact that the client class uses default engine that is defined in the build.gradle file. This feature is used in KMM projects (and most probably can be used in other multiplatform appications). Both IOS and Android part of the projects can use their own engine. (Here a sample project about that ). But that is not a must to do. You may use another way.

Until here, we are able to make requests and get responses from the server in raw form but as you may know, most of the time dealing with raw data is so time consuming and complex, so we generaly use a seriliazer to do the convertions for us. The serializers can be used in Android Are Gson,Jackson and Kotlinx.

We need to use @Serializable annotation on the classes that is sended or received classes for kotlinx-serialization. By using that, the serialiazer knows how to convert the raw data into model class or vice versa.

Overall, Ktor is one of the good libraries when it comes to networking in Android. It uses Kotlin DSL most of the time and that makes it easy to use. You may feel a bit unfamiliar with the library but after a while you will surely get used to it. I believe you have enough information to user Ktor in your projects now. Depending on you use case, you may choose to use it.

Love you all.

Stay tune for upcoming blogs.

Take care.

--

--