Marco Milani

Android & IoT Developer

Home Projects Blog About Contact

Kotlin Multiplatform Mobile (KMM)

Written on:

Cross-platform Android & iOS development explained.

What is KMM?

  • Kotlin Multiplatform Mobile is an SDK for Android and iOS development.
  • It offers all the benefits of native development while enabling cross-platform apps.
KMM Architecture

How does it work?

  • A KMM app is composed of three modules:
  • Android app module: Contains all code to draw and manage the UI, preferably with Jetpack Compose.
  • iOS app module: Contains Swift code to draw and manage the UI. The shared module is compiled into an Apple Framework.
  • Shared module: Common to both Android and iOS, contains all shared logic (API calls, DB calls, business logic).
KMM Modules

Code Structure

  • Project structure includes androidApp, iosApp, and shared modules.
  • androidMain: Android-specific code.
  • iosMain: iOS-specific code.
  • commonMain: Code usable by both platforms.
KMM Project Structure

Project Structure

  • A KMM module can be nested by one or many other KMM modules.
  • Shared code can be organized in submodules for single functionalities.
  • Each submodule is a KMM module with its own androidMain, iosMain, and commonMain sections.
KMM Nested Modules

Gradle

  • Define all targets and dependencies in Gradle.
  • Dependencies of commonMain are inherited by all targets.
KMM Gradle

androidMain / iosMain

  • Platform-specific packages are essential for unique features.
  • Preferences, logging, and other platform features must be managed in androidMain and iosMain.
  • Use the expect/actual mechanism to manage platform-specific implementations from commonMain.
KMM Platform Packages

Expect and Actual

  • Mark a method with expect in commonMain to indicate a platform-specific implementation.
  • Implement the method in the same package in androidMain and iosMain.
  • Example: define expect fun log() in it.eng.logger and implement in both platforms.
KMM Expect/Actual KMM Expect/Actual Example KMM Expect/Actual Implementation

Problems

Dependency Injection

Database and Room

Database Implementation

KMM SQL Table KMM SQL Implementation Android KMM SQL Implementation iOS KMM SQLDriver KMM AppDatabaseImpl

Network Calls and Retrofit

KMM Ktor Network

Network Implementation

KMM Network Implementation

Performance Comparison

Below is a test made with the same app, developed in three different ways:

All apps perform similarly, with some differences:

Tests were made on the same device and network conditions.