KMM — Sample Android & iOS
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.
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
Android 10 Emulator
iPhone 11 | iOS 13.3 Emulator
- Install KMM Plugin in Android Studio.
- 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.
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).
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.
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
Thanks for reading my article. We have made our first KMM application.