Mobile App Development: Kotlin Multiplatform Mobile

Akqeel
99.co
Published in
4 min readOct 16, 2022

Introduction

Kotlin Multiplatform Mobile (KMM) is a technology developed by JetBrains (https://www.jetbrains.com/) that enables mobile application developers to share business logic between iOS and Android apps from a single codebase. One of the key differences KMM brings to the cross-platform development world is its ability to compile to native code.

This has several advantages:

  • Developers can expect native level performance while leveraging cross-platform capabilities.
  • It allows developers to extend on the frameworks and libraries that they know and love without having to move or learn a new framework.
  • Current teams with existing large scale projects can do a gradual migration without having to worry about rewriting their entire codebase.

These benefits in turn can result in faster development cycles since teams can just write and test a single codebase. It can also help free up some of the team resources to focus more on improving user experience, writing documentation, writing unit tests and so on.

KMM recently went into Beta (https://blog.jetbrains.com/kotlin/2022/10/kmm-beta/) and I spent a couple of hours giving it a try! In this article I thought of sharing my learnings. If you are interested in trying out KMM or curious to know how it works, I hope this article can be of use to you.

Getting started

The getting started guide for KMM from JetBrains can be found here: https://kotlinlang.org/docs/multiplatform-mobile-getting-started.html

Steps

  1. Installing Android Studio and Xcode
  2. Install the Kotlin Multiplatform Mobile plugin on Android Studio
  3. Install kdoctor(https://github.com/Kotlin/kdoctor)
  4. Run kdoctor and resolve any errors. I was running an unsupported version of Ruby on my machine. To update my Ruby version I used rbenv(https://github.com/rbenv/rbenv)

Once your environment is setup correctly, you should see the below status when you run `kdoctor` from your command line:

Creating the cross-platform app

The next step is to create our cross platform project. The guide for creating a new KMM project can be found here: https://kotlinlang.org/docs/multiplatform-mobile-create-first-app.html#create-the-project-from-a-template

Once the project is setup, you would see three modules under the Project view on Android Studio:

  • androidApp: This is the Android application module and contains the entry point to your android application.
  • iosApp: This is the iOS project and can be opened from Xcode.
  • shared: This module contains the shared code. The code in this module compiles to an android library (which is declared in the androidApp/build.gradle dependencies) and an iOS framework dependency which is imported into the iOS project.
Source: https://kotlinlang.org/docs/multiplatform-mobile-create-first-app.html#examine-the-project-structure

Running the application

Now that you have your base project setup, you can go ahead and run your iOS and android applications. https://kotlinlang.org/docs/multiplatform-mobile-create-first-app.html#run-your-application

Implementation Details

The shared module contains three Gradle source sets (excluding the test source sets)

  1. androidMain
  2. iosMain
  3. commonMain
  • The commonMain source set defines two classes: Platform and Greeting.
  • The Platform class in this source set defines one expect function: getPlatform.
  • The expect keyword is used to declare functions that require platform specific implementation.
commonMain/Platform.kt

The androidMain and iosMain source sets extend the Platform class from the commonMain source set and implement the getPlatform function as actual functions with platform specific details.

androidMain/Platform.kt
iosMain/Platform.kt

User interfaces

The user interface for Android is written in Jetpack Compose (https://developer.android.com/jetpack/compose) and for iOS in Swift UI (https://developer.apple.com/xcode/swiftui/)

androidApp/MainActivity.kt
iosApp/ContentView.swift

That’s it! It only took me a couple of hours to understand and set up this KMM project. If you are interested to learn more about the implementation details, you can find the repository for this project here: https://github.com/akqeel/KMM-Tutorial

I hope you found this article useful. If you have any suggestions please do share in the comments section below!

--

--

Akqeel
99.co
Writer for

App developer, on a mission to learn… and share!