How to cleanly migrate the layer of an enterprise Android application to Kotlin Multiplatform

  • Implementing a shared module using automated templates in Android Studio for Android and iOS.
  • Linking dependencies in Gradle for Android and configuring binary frameworks using scripts in Xcode for iOS.
  • Upgrade to the Kotlin 2.0 compiler and transition to Compose Multiplatform to optimize interface development.

How to cleanly migrate the layer of an enterprise Android application to Kotlin Multiplatform

If you've spent years polishing an enterprise application for Android and now you've been asked to bring the project to Apple users as well, you're probably wondering if you have to throw all your work out the window. The good news is that Kotlin Multiplatform (KMP) is here to save the day, allowing us to reuse business logic without having to write the same code twice in different languages.

This isn't a magic tool that does all the work on its own, but rather a smart strategy for sharing the data and domain layer . Essentially, we move what's common to both platforms to a central location and let each system manage its own interface, allowing us to maintain the quality of a native app but with the efficiency of unified development.

How to set up the shared module from scratch

To get off to a good start, the easiest thing to do is take advantage of the official tools. If you use Android Studio Meerkat and have the Gradle plugin version 8.8.0 or higher, you're in luck thanks to the Kotlin Multiplatform Shared Module template . This option automates all the basic configuration so you don't have to deal with the configuration files.

The process is quite intuitive: go to File > New > New Module and select the KMP template. Here you'll need to define the module name (which will also serve as the framework name on iOS) and the file package. Once you click Finish, let Gradle work its magic by synchronizing the project. You'll see that platform-specific folders are created, along with a common space called commonMain , which is where the real work happens.

Integrating shared logic into Android

How to cleanly migrate the layer of an enterprise Android application to Kotlin Multiplatform

Creating the module is only half the battle; now you need to tell your Android app to use that code. The wizard doesn't do this automatically, so you must add the dependency manually to your Gradle file. The usual way to do this is to add `implementation(project(":shared"))` within the dependencies block.

If you are one of those who have activated the Type-safe project accessories From Gradle, you can simplify it using projects.sharedOnce linked, your Android app will be able to seamlessly consume everything you've programmed in androidMain or commonMain, integrating completely naturally into the workflow.

Configuration and deployment in the iOS ecosystem

This is where things get interesting, since Swift doesn't natively understand Kotlin. To make them work together, we need to generate a compiled binary frameworkThe Android Studio template already prepares the file build.gradle.kts with configurations for the various Apple architectures, such as iosX64, iosArm64 and iosSimulatorArm64.

By default, the framework is called sharedKitalthough you can change this variable xcfName If you prefer a name more in line with your brand. To let Xcode know what to do, you need to add a script execution phase called Compile Kotlin FrameworkIt is vital that this script runs before compiling the Swift sources so that the Kotlin code is already available when Swift looks for it.

To verify that everything works, simply go to the file ContentView.swift In Xcode, import the library sharedKit and call some function of the shared module, such as the classic platform()to confirm that the app recognizes that it is running on iOS.

Jumping to Kotlin 2.0 and Compose Multiplatform

If you want to upgrade to Kotlin version 2.0, there are a couple of tweaks required. First, update the version in your file libs.versions.toml And don't forget to add the folder .kotlin to file .gitignore to avoid uploading unnecessary junk to the repository.

A fundamental change is the Compose compilerwhich is now a built-in Kotlin plugin. You need to add org.jetbrains.kotlin.plugin.compose in the version catalog and apply it to the corresponding build.gradle files. This allows you to remove the composeOptions block and take advantage of a much cleaner DSL connection to configure things like strongSkippingMode.

Additionally, if you're using KSP (Kotlin Symbol Processing) , be sure to update it to a version compatible with Kotlin 2.0 to avoid annoying compilation errors. If you already have experience with Jetpack Compose, you can adapt that code for Compose Multiplatform , allowing you to share not only the logic but also part of the user interface, although some specific details will still need to be adjusted to ensure a smooth experience on iOS.

Migrating an enterprise infrastructure to KMP involves organizing the code into a shared module, properly linking it to Android via Gradle, and configuring Xcode using iOS build scripts. Upgrading to Kotlin 2.0 simplifies Compose compiler management and optimizes the project structure, ensuring consistent domain logic and a platform-specific interface.


Surfshark Antivirus for Android
It may interest you:
How to remove viruses on Android: A complete and updated guide to cleaning your phone
Add as preferred source in Google