Create your first multiplatform application – tutorial

Here you will learn how to create and run your first KMM application.

  1. 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.

  1. In Android Studio, select File | New | New Project.

  2. Select KMM Application in the list of project templates, and click Next.

    Mobile Multiplatform project template
  3. Specify a name for your first application, and click Next.

    Mobile Multiplatform project - general settings
  4. 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.

    Mobile Multiplatform project - additional settings

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.

Select the Project view

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 multiplatform app on Android
    First mobile multiplatform app on Android

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 multiplatform app on iOS
    First mobile multiplatform app on Android

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.

  1. In the list of run configurations, click Edit Configurations.

    Edit run configurations
  2. Click the + button above the list of configurations and select iOS Application.

    New run configuration for iOS application
  3. Name your configuration.

  4. Select a simulated device in the Execution target list, and then click OK.

    New run configuration with iOS simulator
  5. Click Run to run your application on the new simulated device.

Run on a real iPhone device

  1. Connect a real iPhone device to Xcode.

  2. Create a run configuration by selecting iPhone in the Execution target list.

  3. 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

  1. Open the file iosTest.kt in shared/src/iosTest/kotlin/com.example.kmmapplication.shared.
    Directories with Test in their name contain tests.
    This file includes a sample test for iOS.

    iOS test Kotlin file
  2. 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.

iOS test result

Run tests on Android

For Android, follow a procedure that is very similar to the one for running tests on iOS.

  1. Open the file androidTest.kt in shared/src/androidTest/kotlin/com.example.kmmapplication.shared.

  2. Click the Run gutter icon next to the test.

Update your application

  1. Open the file Greeting.kt in shared/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.

    Common Kotlin file
  2. 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()}!" } } 
  3. Run the updated application on Android.

    Updated mobile multiplatform app on Android
  4. Run the updated application on iOS.

    Updated mobile multiplatform app on iOS
  5. Run tests on Android and iOS.
    As you see, the tests fail. Update the tests to pass. You know how to do this, right? ;)

    iOS test failed

Next steps

Once you've played with your first KMM application, you can:

Last modified: 05 October 2021

© 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