Two Apps in Less than Five Minutes with Kotlin Multiplatform
Tags: kotlin, ios, android, tutorial
Published: 2019-09-04 15:53:29



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.


* * *

_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](https://apps.apple.com/us/app/xcode/id497799835?mt=12) and install it. Not on a Mac? Well, none of this will work (you need a Mac)._


_For Android Studio, go [here](https://developer.android.com/studio) to get the app, follow instructions [here](https://developer.android.com/studio/install) to set it up, and the instructions to setup an Android emulator [here](https://developer.android.com/studio/run/emulator). Ready? OK, now start the clock._

* * * 

Clone [this repository](https://github.com/touchlab/KotlinMultiplatformStarter) 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:


```kotlin
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](https://www.infoq.com/articles/kotlin-multiplatform-ios-developers/) 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](http://touchlab.co/refactory).