Kotlin Multiplatform Mobile (KMM). Part 1: Getting Started.
What is Kotlin Multiplatform Mobile ?
With Kotlin Multiplatform Mobile (KMM), you can build multiplatform mobile applications sharing code, such as business logic, connectivity, and more, between Android and iOS.
“You only need to write platform-specific code where it’s necessary, for example to implement a native UI or when working with platform-specific APIs.”
How to set up your Kotlin Multiplatform project?
Open Android Studio, install Kotlin Multiplatform Mobile plugin
In Android Studio, select File | New | New Project. Select KMM Application in the list of project templates, and click Next.
Fill information for your application, and click Next
Project structure
A basic Kotlin Multiplatform Mobile(KMM) project consists of three components:
- Shared module — a Kotlin module that contains common logic for both Android and iOS applications. Builds into an Android library and an iOS framework. Uses Gradle as a build system.
- Android application — a Kotlin module that builds into the Android application. Uses Gradle as a build system.
- iOS application — an Xcode project that builds into the iOS application.
Shared module
The shared module contains the code that is common for Android and iOS applications. However, to implement the same logic on Android and iOS, you sometimes need to write two platform-specific versions of it. To handle such cases, Kotlin offers the expect/actual mechanism. The source code of the shared module is organized in three source sets accordingly:
commonMain
stores the code that works on both platforms, including theexpect
declarationsandroidMain
stores Android-specific parts, includingactual
implementationsiosMain
stores iOS-specific parts, includingactual
implementations
Each source set has its own dependencies. Kotlin standard library is added automatically to all source sets, you don’t need to declare it in the build script.
expect
/actual
expect defines an expected declaration (It’s used inside the commonMain), and platform source sets must provide the actual specific implementation.
// Common
expect class Platform() {
val platform: String
}
It expects the targets to provide platform-specific implementations for Platform, and the common code can now use this declaration without any consideration of how it is implemented.
//Android
actual class Platform actual constructor() {
actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT}"
}
//iOS
import platform.UIKit.UIDeviceactual class Platform actual constructor() {
actual val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}
Run your application on Android
In the list of run configurations, select androidApp and then click Run.
Run your application on iOS
In the list of run configurations, select iosApp and then click Run.
If you want to run your application on another simulated device, you can add a new run configuration by clicking on Edit Configurations
Here is github sample code that you can checkout:
https://github.com/alanrb/Kotlin-Multiplatform/tree/part1
Thanks for reading!