Understanding KMM: Beginners guide

Pedro Miguel da Cruz Santos
Deemaze Writing Wall
6 min readApr 19, 2023

In today’s world, mobile applications have become a necessity for businesses to reach a wider audience. However, creating an app for multiple platforms can be a daunting task. It requires developers to write code for each platform separately, which is time-consuming and expensive, or write code for cross-platform frameworks such as React Native which may fall behind in terms of performance when compared with native code. This is where Kotlin Multiplatform Mobile (KMM) comes into play. KMM allows developers to create apps for different platforms (such as Android and iOS) with a shared business logic shortening the developing time.

What is KMM?

KMM is an open-source framework developed by JetBrains, the same company behind the Kotlin programming language. It allows developers to write code in Kotlin that can be shared across multiple platforms such as iOS, Android, and the web. This entails that developers only need business logic written once that can then be shared across all platforms. The views related to each platform will then be dealt with using native languages that allow for better performance (Jetpack Compose and SwiftUI for example).

Benefits of KMM

Having a shared business logic means that while working on an app for two different platforms, two different developers can work independently on each platform. It also means that there won’t be duplicated code since the business logic (repositories and data models) will only be written once. This means that the development time is shorter. Having a clear distinction between the shared modules between the platforms and the platform-specific modules allows users to follow an MVVM architecture when developing their projects very easily since their models and views/viewModels are already separated.

Coding with KMM

To develop a KMM project on your own, you will need both Android Studio and Xcode as well as some plugins for Android Studio, namely, the Kotlin Multiplatform Mobile plugin and the Kotlin plugin.

When creating a KMM project the structure that is loaded is fairly simple. The three main folders are the androidApp folder which contains platform-specific code for Android, the iosApp folder which contains platform-specific code for iOS and the shared folder which contains data classes and repositories that will be shared between the Android and iOS apps.

Structure of a newly created KMM project

Within the shared module, there are three more folders, one which contains logic that is adapted from the common folder to be used in Android, another for iOS, and the common main itself.

The structure inside the shared module

These three folders interact in the following way:

  • The common main houses all repositories, data models, and classes that are shared between Android and iOS;
  • The Android and iOS main specify the classes that can be used in their platform-specific parts.

When creating a new KMM project, you will notice that the common main has two classes (Greeting and Platform). The Greeting class is fairly simple and uses the Platform class to return a String. However, in the Platform class, we see a new keyword: expected. This is used for communication with the platform-specific modules within the shared module. In the commonMain we specify functions that will be common between both platforms with the expected class, knowing that in the platform-specific modules, we will have the actual class.

actual class Platform actual constructor() {
actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT}"
}
actual class Platform actual constructor() {
actual val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}
expect class Platform() {
val platform: String
}

You can define functions without a body within the common main as long as you provide the expected keyword since it is always guaranteed that the actual counterparts will contain the body and logic for those functions.

Communication with databases, external APIs, or services should all be done within the commonMain module. Any function or method that might need a more specific behavior depending on the platform can be called using the expected and actual keywords.

Working on platform-specific modules

Working on the Android views and viewModels will be just the same as if it were a normal project and not a KMM project. Since everything can be done in Android Studio, it shouldn’t be hard to adapt to the slight change of having a common module that houses the business logic for your project.

However, when working on the iOS portion of the project, things might get tricky with the setup. You can follow these steps to do so:

  1. Open Android Studio and navigate to your KMM project.
  2. Make sure that you have already generated the Xcode project files by running the following command in the terminal: ./gradlew :shared:packForXCode.
  3. Locate the generated MyKmmProject.xcworkspace file in the MyKmmProject/iosApp directory.
  4. Double-click the MyKmmProject.xcworkspace file to open it in Xcode.
  5. Once the project is open in Xcode, you can run it on a simulator or physical device by selecting the appropriate scheme and target.

Whenever the setup is complete, you will just need to develop the views and viewModels you might need to create the iOSApp, similarly to how you did in the Android portion.

It is worth mentioning that currently JetBrains is developing a cross-platform framework for the UI portion of multi-platform projects. This is called, Compose Multiplatform and it goes hand in hand with KMM. Although it is still in the Alpha version, it seems like a good starting point and worth investigating further for cross-platform projects.

Versioning

There are several gradle files that are used within a KMM project, one of the files for the root folder and all the project dependencies, one other file for the common main folder and the shared dependencies, and one last folder for the Android App itself. This can become challenging since the versions you use need to be compatible with the other versions you are using in different files. Therefore you need to make sure that as new dependencies are added they all fit with the rest of the dependencies you already have, this might save some hours of dependency juggling to try and make them fit the project configuration.

The future of KMM

As of this moment, more and more tech giants are turning to KMM as a possibility to develop their own apps without compromising performance and development time. This leads to dependencies being updated so they can be shared as well as some possible refurbishing to the current versioning problems there might be between Android and iOS. The future of KMM is bright as a tool that promises the use of native components and less development time for multiple platforms. It may fall short when compared to other frameworks such as Flutter when it comes to development time since a single implementation for the UI works on both platforms, however, the native components and rich business logic can make a difference if you have a little more time to develop your app. Furthermore, with the appearance of Compose Multiplatform, the issue of implementing two different platform-specific views should become less relevant as it would be possible to implement these views all at once.

Conclusion

In conclusion, Kotlin Multiplatform Mobile is a powerful tool for building high-quality mobile apps that can run on both Android and iOS platforms with shared code. It allows developers to write code in Kotlin, a language that is easy to learn and maintain, and to share common logic across platforms.

With KMM, developers can create robust and feature-rich apps that leverage platform-specific APIs and libraries while also maintaining a high level of code reuse. It is a technology that has gained significant traction recently, with many companies adopting it for their mobile development needs.

If you are looking to build cross-platform mobile apps, then KMM is definitely worth considering. With its ease of use, flexibility, and efficiency, KMM provides a powerful and cost-effective solution for developing apps that can run on Android and iOS platforms. By embracing KMM, developers can take their mobile development to the next level, delivering high-quality apps that are both reliable and user-friendly.

Deemaze Software is a digital agency developing products for web and mobile. Catch up with us on Twitter, Facebook, Instagram or Linkedin. Let us know if there’s something you want us to write about! 🎈

--

--