KMM — Sample Android & iOS

Amal Jofy
3 min readSep 24, 2020

Kotlin | Android | iOS | KMM | Multiplatform

In this article we are going to experience how to implement Multiplatform application, Android and iOS using KMM (Kotlin Multiplatform Mobile). In mobile application development, both Android and iOS have a lot of things in common the user interface, business logic, parsing remote calls, files handling etc, a lot of things are pretty much similar.

So if we are familiar with kotlin language and want to try multi-platform, JetBrain community came up with Kotlin Multiplatform Mobile Project.

What is KMM ?

Kotlin Multiplatform Mobile (KMM) is a platform provided by JetBrain for the development of cross platform mobile applications. Multiplatform programming is one of the key benefits of kotlin which reduces time in writing and maintaining the same code for different platforms.

While writing this article KMM is in alpha stage and may change in future koltin versions.

Building first KMM App

Software and OS Used

macOS Catalina 10.15.6

Android Studio 4.0.1

Xcode 11.3

Android 10 Emulator

iPhone 11 | iOS 13.3 Emulator

Steps

  1. Install KMM Plugin in Android Studio.
  2. Create new KMM Application from the project template.

3. Now you can start coding your multiplatform project.

4. Let’s setup the emulator for iOS in Android Studio.

6. Click play button to run your application by selecting the device.

iOS And Android Emulators.

KMM Codebase

When moving on to the KMM codebase we can see that it consists of three modules, shared module (Kotlin module — common code for both android and iOS), androidApp module (Kotlin module — Builds the android application) and iosApp module (Xcode module — Builds the iOS application).

Shared Module

The KMM project structure, shared Module is layered in three sets, Common main, Android main and iOS main.

Common Main — Contains code that is common for both platform that defines an expect declaration.

Android Main — Contains code that is Android specific and defines an actual declaration.

iOS Main — Contains code that is iOS specific and defines an actual declaration.

In Multiplatform it's common to have platform specific versions for accessing sensor data, Bluetooth, Proximity, etc. In order to access the platform specific code (Android Main and iOS Main) from the Common Main Kotlin introduce the mechanism of expect and actual declarations.

Use Cases

commonMain

androidMain

iOSMain

If we use expect declaration in a common main package, then we should use the same package name in actual in androidMain and iOSMain.

Android — MainActivity.kt

iOS — ContentView.swift

--

--