· 3 min read Posted by Ben Whitley
Kotlin Multiplatform Tutorial: Two Apps in Less than Five Minutes with KMP
Kotlin Multiplatform Tutorial
Check out our Kotlin Multiplatform tutorial
If you’re reading this you’ve probably heard a lot about Kotlin Multiplatform, but aren’t sure where to start.
The platform is evolving fast, which means a lot of the docs are out of date, and most of the tutorials seem to walk you through setting up projects and tools from scratch. But nobody likes reading the directions! We’re going to walk you through setting up some shared code in less than five minutes with this super handy Kotlin Multiplatform tutorial.
Prerequisite: We’ll cheat and not start the clock until you have Xcode 10.3+ and Android Studio 3.4.2 installed, and have an emulator installed and running for Android.
For Xcode, find it on the Mac App Store here and install it. Not on a Mac? Well, none of this will work (you need a Mac).
For Android Studio, go here to get the app, follow instructions here to set it up, and the instructions to setup an Android emulator here. Ready? OK, now start the clock.
Let’s get started.
Clone this repository and open it in Android Studio. The project currently contains two separate Hello World applications: one for Android and iOS each. Run the Android app to see it display “Hello, Android world!”
Now, open /iosApp/iosApp.xcworkspace
and run iosApp
in the iOS simulator to see it display “Hello, iOS world!” Although they share a folder structure, these iOS and Android apps are separate from one another. Now we’ll join them together with Kotlin Multiplatform.
First, open /app/src/commonMain/kotlin/sample/Sample.kt
, which contains the following common code:
package sample
class Greeting {
fun hello(): String = "Hello, Android and iOS worlds!"
}
Next, open /app/src/main/java/sample/SampleAndroid.kt
in Android Studio. It contains the Android version of the Hello World app. To make it into a KMP app, replace the value being assigned to textView.text
— which is currently “Hello, Android world!” — and replace it with Greeting().hello()
.
Finally, go back to Xcode and open ViewController.swift
. Underneath \import UIKit
, add \import app
, which is the name of the KMP library. Then, just like in the Android app, change the value being assigned to label.text
from “Hello, iOS world!” to Greeting().hello()
. Rerun both applications, and see them both displaying the same string!
“Hello, Android and iOS worlds!”
All done! Stop the clock! You’re a Kotlin Multiplatform developer now!
What are some good next steps? Try calling out to some native code with expect
/actual
statements, then add an existing multiplatform library like Ktor and do some networking.
If you come from an iOS background and want a deeper explanation of how to use Kotlin Multiplatform to make a more involved app, then you might want to check out this article I wrote.
As you can see, it’s all about starting small with Kotlin Multiplatform. We recently launched The Touchlab Refactory; an incremental refactoring service with KMP which focuses on a single feature at a time with no disruption to your current development workflow. Learn more here.
Would you like to know more?
We hope you enjoyed this Kotlin Multiplatform tutorial, if you have any questions or are ready to get stared with Kotlin Multiplatform then get in touch today.