Move android code to KMM Shared Module| How we migrated our code to Kotlin Multiplatform Part-2

Move android code to KMM Shared Module| How we migrated our code to Kotlin Multiplatform Part-2

Anuj Sachan's photo
Anuj Sachan
·Jul 1, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Earlier, I wrote a blog about merging the existing iOS and Android App with KMM and share the code between iOS and Android natively.

If you have landed here directly and haven’t read the previous part, here is the link to it.

IS KMM production-ready? How we migrated our code to Kotlin Multiplatform Mobile! Part 1
I will discuss my thoughts about KMM as we have worked with the production-ready app in
mounty.co!medium.com

This blog will tell you how we migrated our existing Android code in a shared module. We were using the MVVM designing pattern in our Android app. Heres’s a diagram describing the existing android architecture (non-KMM).

source: developer.android.com/jetpack/guide

We have used retrofit for network calls, Jetpack rooms for even better database handling, and Shared Preferences for any kind of key-value storage. In the current architecture, we have multiple App Repositories that made several API calls to our backend using retrofit.

If you are thinking about KMM and haven’t started the android project yet, Here’s a tip: Make just one Repository and Service for the entire app with added comments and or properly documented! Check the above diagram

In the project, the View-model calls the Repository injected by the hilt module, after which the repository calls our Service. This was our previous architecture. The first thing that we had to move is DTO or data classes so that we could share networking /caching logic.

As we know in DTO we have several annotations, we had to change those in KMM. JetBrains created a plugin named KotlinX Serialization. Edit your shared module’s `build.gradle`, and add KotlinX Serialization plugin and dependencies as mentioned below:

In plugin Add —

kotlin(“plugin.serialization”)

And add below dependencies in `commonMain` block in build.gradle-

implementation("io.ktor:ktor-client-serialization:$ktorVers..")
implementation("org.jetbrains.kotlinx:kotlinx-serialization..")

After adding this plugin, move the DTO folder from android to the common main folder

After moving you will get several warnings! It’s time to change your android annotations to KMM annotation.

  1. convert @Parcelize ->@Serializable Parciable annotation will not work on KMM
  2. convert @SerializedName(“DATA”)->@SerialName(“DATA”)

The above changes have to be followed in all the existing DTO and one more thing if you have used date time conversation in DTO directly from the android library you have to use Kotlinx DateTime library for date-time conversion mostly developers use Joda-Time library for DateTime conversion that's all for DTO part you can move your Util folder for validation and all other folders directly which you want to share in your case folders may differ. You can put validation and other files or folders with common code.

Okey-Dokey! Now we have moved all the validation logic and DTO classes successfully.

In the next blog, I will tell how we migrated the whole service, API calls, Caching, and sessions and made it common in KMM.

Follow me:
Blog: http://www.maddeveloper.in
Twitter: https://twitter.com/AnujSachan72

About Mounty

Mounty is an online discovery and booking platform for camps, adventure activities & trips anchored by the largest network of privately owned campsites, adventures, and trips providers. We aim to make the travel experience fun and seamless. We do this through an amazing team of passionate travelers with years of experience and exceptional capabilities.

 
Share this