In mobile development, the Android and iOS versions of an application often have a lot in common . The business logic behind the apps is probably pretty much the same. Download files, read from and write to a database, get and send data to a remote host, parsing the remote data, etc. So why not we write the business logic only once and share it between different platforms?
With this idea, JetBrains came up with Kotlin Multiplatform Project.
➡️ What is Kotlin Multiplatform Mobile?
Kotlin Multiplatform Mobile (KMM) is an SDK for cross-platform mobile development provided by JetBrains. Leveraging the multiplatform capabilities of Kotlin, you can compile/transpile a single project for multiple platforms.
With KMM, you get this flexibility and retain the benefits of native programming. Use a single codebase for the business logic of iOS and Android apps and write platform-specific code only where it’s necessary, to implement a native UI or when working with platform-specific APIs.
KMM can seamlessly be integrated with your mobile project. Shared code, written in Kotlin, is compiled to JVM bytecode with Kotlin/JVM and to native binaries with Kotlin/Native, so you can use your KMM business-logic modules just like any other regular mobile library.
While writing this blog, KMM is in Alpha and you can begin sharing business rationale in your versatile applications with it immediately.
KMM is still quite new to the mobile development world, JetBrains also developed KMM plugin for Android Studio to seamlessly setup your KMM project. Plugin permits you to compose, run, test, and debug shared code in the Android Studio.
➡️ Steps of build your HELLO WORLD KMM app
- Install Kotlin Multiplatform Mobile plugin in Android studio. Open your Android Studio -> Click Configure -> Select Plugins
2. Select Marketplace in the plugin section and search for “KMM”, Install it and restart your Android studio
3. In the Android studio first screen Select “ Start a new Android Studio project”.
4. In the “ Select a project Template” screen, scroll down and select “KMM Application”
5. Now set your project a name, minimum SDK, directory and package name.
Now, you will have to wait while your project is set up for the first time. It may take some time to download and set up the required components.
➡️ Run Your Application
Select your platform you want to run from the toolbar, select the Emulator/Simulator and click Run
To run IOS app you need to have Xcode and Simulater installed.
If you want to run your ios application on another simulated device, you can add a new run configuration.
- In the list of run configurations, click Edit Configurations.
2. Click the + button above the list of configurations and select iOS Application.
3. Name your configuration.
4. Select a simulated device in the Execution target list, and then click OK.
5. Click Run to run your application on the new simulated device.
➡️ Dive Into The Code
Android Devs? Looks Familiar? 😎
IOS Devs? Looks Alien? 👽
➡️ Modules
- Shared module — a Kotlin module where your common business logic code resides for both Android and iOS applications. Builds into an Android library and an iOS framework. Uses Gradle as a build system.
- androidApp module — a Kotlin module that builds into the Android application. Uses Gradle as a build system.
- iosApp module — an Xcode project that builds into the iOS application.
Project: build.gradle.kts
➡️ 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 like Bluetooth, Wifi. 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.
build.gradle.kts
This build.gradle.kts has the configurations of Android as well as IOS for shared module
AndroidApp: build.gradle.kts
➡️ Using the Expect/Actual Keywords
It’s common for a multiplatform app to have platform-specific code. For example, you may want to know if your app is running on an Android or iOS device and get the exact model of your device. To achieve this you need to use the expect/actual
keywords.
First, declare a class, method or function in the common module using the expect
keyword and leave the body empty as you often do when creating interfaces or abstract classes. Then, write the platform-specific implementations for the class, method or function in all of the other modules using the actual
keyword in the signature.
Note: If you use expect
in a package, you also need to use the corresponding actual
command in a package with the same name.
Else you’ll this error
➡️ Expect/Actual Usage
commonMain
expect class Platform() { val platform: String }
androidMain
actual class Platform actual constructor() { actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT}" }
iosMain
import platform.UIKit.UIDevice actual class Platform { actual val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion }
MainActivity.kt (Android)
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val tv: TextView = findViewById(R.id.text_view) tv.text = "Hello World, ${Platform().platform}!" } }
ContentView.swift (iOS)
struct ContentView: View { var body: some View { Text("Hello World, "+ Platform().platform) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
CONGRATULATIONS!! You have made your first KMM app.
➡️ Here are some KMM apps
JetBrains/kotlinconf-app |
![]() |
saket/press |
![]() |
jarroyoesp/KotlinMultiPlatform |
![]() |
➡️ Check Out Available Libraries
AAkira/Kotlin-Multiplatform-LibrariesKotlin Multiplatform Libraries. Welcome PR if you find or create new Kotlin Multiplatform Library. … |
![]() |
Feel free to connect with me on Twitter
Hope You’ve learned something new today..