Overview of Kotlin Multiplatform Mobile

Tram Ho

1. What is Kotlin Multiplatform Mobile (KMM)?

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

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

Figure 1: Overview of Kotlin Multiplatform Mobile

I will talk about all these tools in upcoming posts! Whether you are planning to start a project or have a pre-written 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 into JVM bytecode with Kotlin / JVM (like normal Android development) and files native binaries for iOS with Kotlin/Native, so you can use your KMM modules just like any other regular portable library. (Figure 2)

Figure 2: KMM . Module Operation

 

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

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

Figure 3: Native code and Shared code

2. Using Kotlin Multiplatform

The ability to use 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 just share the data layers (Figure 4 – Data/Core), using popular libraries like Ktor or SQLDelight.

Presentation.png

Figure 4: Structure of a Project KMM – KMM Module now has only Data/Core

You’ve probably built a mobile app before, 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)

Presentation.png

Figure 5: Structure of a KMM Project – KMM Module now includes 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 presenter, view model, or controller views or even navigation logic. In the following articles, we will look at different projects and sharing strategies.

Presentation.png

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

3. What is required to Build KMM Project (Prerequisites)

That’s enough concept. Let’s talk about what we need to get started with KMM. I hope you enjoyed reading this – it’s really not much and most of the setup can be done 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 will also need Android Studio to code in the Kotlin programming language for both Android apps and the cross-platform module . Also, you will need Gradle to build all of the above.

All Kotlin Multiplatform projects are created with 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 tools needed include:

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

Install JDK following instructions: https://www.geeksforgeeks.org/download-and-install-java-development-kit-jdk-on-windows-mac-and-linux/

Download Xcode on the MacOS appStore to get the latest version or download Xcode by version https://xcodereleases.com/

Download Android Studio now from https://developer.android.com/android-studio/download

All documentation about KMM is available at: https://kotlinlang.org/docs/multiplatform-mobile-getting-started.html

4. Kotlin for Android Studio

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

The installation steps are as follows:

  1. Run the Android Studio App
  2. Choose File -> Settings ( Preferences for MacOS )
  3. Plugins
  4. In Marketplace -> Search Kotlin Multiplatform Mobile -> Click Install (Figure 7)

Screenshot 2022-12-02 at 20.41.20.png

Figure 7: Installing the Kotlin Multiplatform Mobile Plugin for Android Studio

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

So our environment is set up, 2 pictures 8 and 9 are the image of our project using KMM. You can refer to the source code here: https://github.com/misterbo271/mpvKmmApp

Screenshots 2022-12-02 at 19.25.50.png

Figure 8: Running iOS Simulator right on Android Studio without XCode

 

Screenshot 2022-12-02 at 19.28.27.png

Figure 9: Kotlin Multiplatform Mobile Project

 

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

Preferences

Share the news now

Source : Viblo