Replacing Retrofit with Ktor Client and Kotlin Serialization for Androidđ±âđ
I have been using Retrofit from the time I have started Android Development. It is great and has been the first choice for most developers for nearly a decade.
If you donât know about Ktor
Ktor is an asynchronous framework for creating microservices, web applications, client and more with Kotlin.
To know more about Ktor
The introduction of Kotlin and then Ktor has a total game-changer. I have been trying out Ktor Client for some mini projects for now. Ktor Client is highly configurable and very easy to set up. You can make the Client up and running in no time. If Ktor feels like a more Kotlin friendly solution than Retrofit.
If you have tried the Ktor backend it becomes really easy to take up the Ktor Client it has a similar approach to the introduction of Ktor 2.0 it is a perfect lightweight solution for your next project.
if you like Jetpack Compose you should surely check out this:
Letâs build a simple dummy application with Mock API, Kotlin Flow & Jetpack Compose. You will see how with just some minimal lines of code it becomes really read, letâs get started.
Adding Dependencies
Adding dependencies for Ktor Client, Koltin Serialization in the app
Making Data Model
We have made a simple data model with the help of responses from this Dummy API
Making Ktor Client
It is really easy to set up Ktor clients, with plugins and install them with the Ktor Client configurations. Here are some of the basic configurations that I have added here you can add much more and change them according to your requirement.
JSON : Creates an instance of Json configured from the optionally given Json instance and adjusted with builderAction. Here we have chnaged two configurations only encodeDefaults=true and ignoreUnknownKeys=trueHttpClient: Constructs an asynchronous HttpClient using optional block for configuring this client. Now we will install some of the plugins:* JsonFeature: Here we configure the KotlinxSerializer and the JSON instance.* Logging: We are adding the logger object with setting the logger to ALL there are many options you can change accordingly.* HttpTimeout: Seting the timeout as the name suggests* defaultRequest: Here we set the default configuration for setting the contentType and content acceptType, ie Json here.
Adding Repository Layer
We made a simple suspending function, which returns of Response type(Custom model according to API response). From this function, we directly return the get request. as you can see it is really easy to implement.
Adding ViewModel Layer
We have added a simple ViewModel model layer, with KotlinFlow, handling the success and error accordingly.
Adding Basic Compose UI
I didnât do much on the UI side because the blog is focused on Ktor Client. So just slowing the basic response text.
That is with the basic setup and implementation of the Ktor Client. It is really easy and highly configurable as you can see from above. I am also adding some references in case you want to review them.
For any doubts and suggestions, you can reach out on my Instagram, or LinkedIn. Follow me for Kotlin content and more. Happy Coding!
I will well appreciate one of these đ