Kotlin Help

Create your first cross-platform app

Here you will learn how to create and run your first Kotlin Multiplatform Mobile application using Android Studio.

Create the project from a template

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

  2. Select Kotlin Multiplatform App 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 iOS framework distribution list, select the Regular framework option.

    Mobile Multiplatform project - additional settings
  5. Keep the default names for the application and shared folders. Click Finish.

The project will be set up automatically. It may take some time to download and set up the required components when you do this for the first time.

Examine the project structure

To view the full structure of your mobile multiplatform project, switch the view from Android to Project.

Select the Project view

Each Kotlin Multiplatform Mobile project includes three modules:

  • shared is a Kotlin module that contains the logic common for both Android and iOS applications – the code you share between platforms. It uses Gradle as the build system that helps you automate your build process. The shared module builds into an Android library and an iOS framework.

  • androidApp is a Kotlin module that builds into an Android application. It uses Gradle as the build system. The androidApp module depends on and uses the shared module as a regular Android library.

  • iOSApp is an Xcode project that builds into an iOS application. It depends on and uses the shared module as an iOS framework. The shared module can be used as a regular framework or as a CocoaPods dependency, based on what you've chosen in the previous step in iOS framework distribution. In this tutorial, it's a regular framework dependency.

Basic Multiplatform Mobile project structure

The shared module consists of three source sets: androidMain, commonMain, and iosMain. Source set is a Gradle concept for a number of files logically grouped together where each group has its own dependencies. In Kotlin Multiplatform, different source sets in a shared module can target different platforms.

Source sets and modules structure

Run your application

You can run your multiplatform application on Android or iOS.

Run your application on Android

  1. Create an Android virtual device.

  2. In the list of run configurations, select androidApp.

  3. Choose your Android virtual device and click Run.

    Run multiplatform app on Android
    First mobile multiplatform app on Android

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.

Update your application

  1. Open the Greeting.kt file in shared/src/commonMain/kotlin. This directory stores the shared code for both Android and iOS. If you make changes to the shared code, you will see them reflected in both applications.

    Common Kotlin file
  2. Update the shared code by using reversed(), the Kotlin standard library function for reversing text that works on all platforms:

    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

Next step

Learn about dependencies and add a third-party library to your project to expand its functionality.

See also

Get help

Last modified: 27 September 2022