24 September, 2024 by

mienpv

Overview of Kotlin Multiplatform Mobile

In this article, we will learn what Kotlin Multiplatform Mobile is and how we can use it in our projects. We will see how to set up our environment and prepare to create our first cross-platform mobile application with KMM!

1. What is Kotlin Multiplatform Mobile (KMM)?

So what is KMM? KMM stands for Kotlin Multiplatform Mobile. It is an SDK for developing mobile applications on both iOS and Android. It uses the cross-platform capabilities of Kotlin, including various tools and features designed to improve the experience of building mobile cross-platform applications (figure 1).

figure 1: overview of Kotlin Multiplatform Mobilefigure 1: overview of Kotlin Multiplatform Mobile

I’ll talk about all of these tools in upcoming posts! Whether you’re starting a project or have an existing codebase, you can integrate KMM seamlessly. Shared code (in a KMM project there are 3 important folders: androidApp, iosApp, and shared), written in Kotlin, compiled to JVM bytecode with Kotlin/JVM (just like regular Android development) and native binaries for iOS with Kotlin/Native, so you can use your KMM modules just like any other regular mobile library. (figure 2)

figure 2: KMM Module operating mechanismfigure 2: KMM Module operating mechanism

This also means that KMM will not impose any restrictions on how you develop your app's UI. You are free to use any style and framework you want, whatever you like, including the most modern ones, such as Jetpack Compose for Android or SwiftUl for iOS.

I believe the real beauty of KMM lies in its flexibility. KMM lets you share what you want to share! It is designed to help developers reuse their business logic across mobile platforms, writing platform-specific code only when necessary, like implementing native Ul or when working with platform-specific APIs.

figure 3: native code versus shared codefigure 3: native code versus shared code

2. Using Kotlin Multiplatform

The ability to leverage the power of each platform without any overhead while keeping the logic abstract allows you to use KMM in any way you want. You can decide to share only the data layers (Figure 4 - Data/Core), using popular libraries like Ktor or SQLDelight.

figure 4: Structure of a KMM Project - KMM Module now only has Data/Corefigure 4: Structure of a KMM Project - KMM Module now only has Data/Core

Maybe you already have a mobile application built, in which case you can use KMM when you need to implement some new data processing algorithms. This logic can be easily integrated into existing projects, ensuring identical behavior on both platforms. (Figure 5 - Business / Domain)

figure 5: Structure of a KMM Project - KMM Module now has Business / Domainfigure 5: Structure of a KMM Project - KMM Module now has Business / Domain

If your iOS and Android apps are similar enough, you can use KMM to share presentation logic (Figure 6 - Presentation), allowing you to use a single presentation, view model, view controller, or even navigation logic. In future posts, we'll look at different projects and sharing strategies.

figure 6: Structure of a KMM Project - KMM Module now has Presentation - in Native only displays UI, other Modules can work directly in KMM Module without touching Native.figure 6: Structure of a KMM Project - KMM Module now has Presentation - in Native only displays UI, other Modules can work directly in KMM Module without touching Native.

3. What is needed to Build a KMM Project (Prerequisites)

Enough about the concept. Let’s talk about what we need to get started with KMM. I hope you’re excited to read this article – it’s not much and we can do most of the setup in just a few minutes. Developing mobile cross-platform applications requires Xcode to build iOS Apps. This means you’ll need a MacBook with MacOS to develop iOS apps with KMM, while you can still run the Android-only part on a Windows or Linux machine. You’ll also need Android Studio to code in Kotlin for both Android apps and cross-platform modules. Additionally, you’ll need Gradle to build all of the above.

All Kotlin Multiplatform projects are created using the Kotlin Multiplatform Gradle plugin. And finally, to use Gradle, you will need to have the Java Development Kit (jdk) installed on your machine.

In summary, the required tools include:

  • Android Studio: to create multiplatform applications and run the app on a virtual machine or a real machine.
  • XCode (only runs on Macbook)
  • Gradle
  • Kotlin Multiplatform Mobile plugin
  • JDK
  • Kotlin plugin: Check it similar to KMM Plugin, go to Plugins | Installed. Check Kotlin version at Tools | Kotlin | Configure Kotlin Plugin Updates.

Instruction

Install JDK

Download Xcode on MacOS appStore to download the latest version or download Xcode by version

Download Android Studio

KMM document official

4. Kotlin for Android Studio

First you should install Kotlin Multiplatform Mobile Plugin for Android Studio.

Step by step

  1. Run Android Studio
  2. Select File -> Settings (Preferences for MacOS )
  3. Plugins
  4. At Marketplace -> Search Kotlin Multiplatform Mobile -> Press Install (figure 7)

figure 7: Install Plugin Kotlin Multiplatform Mobile for Android Studiofigure 7: Install Plugin Kotlin Multiplatform Mobile for Android Studio

This plugin provides integration with iOS simulators and iOS devices in Android Studio. You will be able to run and debug your apps and tests on iOS from Android Studio without having to switch IDEs while working on shared parts of your code. The plugin also provides handy wizards to create new KMM projects or add modules to existing Android projects. With the “new project” wizard, you can create a ready-to-run cross-platform mobile application in just a few clicks!

So our environment is set up, images 8 and 9 are images of my project using KMM. You can refer to the source code here: MienPV KMM project

figure 8: Run iOS Simulator right on Android Studio without XCodefigure 8: Run iOS Simulator right on Android Studio without XCode

figure 9: Kotlin Multiplatform Mobile Projectfigure 9: Kotlin Multiplatform Mobile Project

Thank you for reading the article, look forward to the content about Kotlin, KMM in the next articles.