Create your first multiplatform application – tutorial
Here you will learn how to create and run your first KMM application.
-
Set up your environment for KMM development by installing the necessary tools on a suitable operating system.
You can also watch a video of this tutorial created by Ekaterina Petrova, Kotlin Developer Advocate.
In Android Studio, select File | New | New Project.
-
Select KMM Application in the list of project templates, and click Next.
-
Specify a name for your first application, and click Next.
-
In the window that opens, do the following:
Keep the default names for the application and shared folders.
Select the checkbox to generate sample tests for your project.
Select Xcode build phases (packForXcode task) in the list of iOS framework distribution options.
Click Finish to create a new project.
Now wait while your project is set up. It may take some time to download and set up the required components when you do this for the first time.
To view the complete structure of your mobile multiplatform project, switch the view from Android to Project. You can understand the KMM project structure and how you can use this.
Run your application
You can run your multiplatform application on Android or iOS.
Run your application on Android
-
In the list of run configurations, select androidApp and then click Run.
Run on a different Android simulated device
Learn how to configure the Android Emulator and run your application on a different simulated device.
Run on a real Android device
Learn how to configure and connect a hardware device and run your application on it.
Run your application on iOS
-
In the list of run configurations, select iosApp and then click Run.
Run on a different iPhone simulated device
If you want to run your application on another simulated device, you can add a new run configuration.
-
In the list of run configurations, click Edit Configurations.
-
Click the + button above the list of configurations and select iOS Application.
Name your configuration.
-
Select a simulated device in the Execution target list, and then click OK.
Click Run to run your application on the new simulated device.
Run on a real iPhone device
Create a run configuration by selecting iPhone in the Execution target list.
Click Run to run your application on the iPhone device.
Run tests
You can run tests to check that the shared code works correctly on both platforms. Of course, you can also write and run tests to check the platform-specific code.
Run tests on iOS
-
Open the file
iosTest.kt
inshared/src/iosTest/kotlin/com.example.kmmapplication.shared
.
Directories withTest
in their name contain tests.
This file includes a sample test for iOS. Click the Run icon in the gutter next to the test.
Tests run on a simulator without UI. Congratulations! The test has passed – see test results in the console.
Run tests on Android
For Android, follow a procedure that is very similar to the one for running tests on iOS.
Open the file
androidTest.kt
inshared/src/androidTest/kotlin/com.example.kmmapplication.shared
.Click the Run gutter icon next to the test.
Update your application
-
Open the file
Greeting.kt
inshared/src/commonMain/kotlin/com.example.kmmapplication.shared
.
This directory stores the shared code for both platforms – Android and iOS. If you make changes to the shared code, you will see changes in both applications. -
Update the shared code – use the Kotlin standard library function that works on all platforms and reverts text:
reversed()
.class Greeting { fun greeting(): String { return "Guess what it is! > ${Platform().platform.reversed()}!" } }
-
Run the updated application on Android.
-
Run the updated application on iOS.
-
Run tests on Android and iOS.
As you see, the tests fail. Update the tests to pass. You know how to do this, right? ;)
Next steps
Once you've played with your first KMM application, you can:
© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/kmm-create-first-app.html