Kotlin Multiplatform Development stable Help

Create your first cross-platform app

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

Create the project with a wizard

  1. Open the Kotlin Multiplatform wizard.

  2. On the New project tab, change the project name to "GreetingKMP" and the project ID to "com.jetbrains.greeting".

  3. Ensure that the Android and iOS options are selected.

  4. For iOS, choose the Do not share UI option to keep the UI native.

  5. Click the Download button and unpack the resulting archive.

Kotlin Multiplatform wizard

Examine the project structure

  1. Launch Android Studio.

  2. On the Welcome screen, click Open, or select File | Open in the editor.

  3. Navigate to the unpacked project folder and then click Open.

    Android Studio detects that the folder contains a Gradle build file and opens the folder as a new project.

  4. The default view in Android Studio is optimized for Android development. To see the full file structure of the project, which is more convenient for multiplatform development, switch the view from Android to Project:

    Select the project view

Each Kotlin Multiplatform 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 to help automate your build process.

  • composeApp is a Kotlin module that builds into an Android application. It uses Gradle as the build system. The composeApp 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. By default, the Kotlin Multiplatform wizard creates projects that use the regular framework dependency.

Basic Multiplatform 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.

The common source set contains shared Kotlin code, and platform source sets use Kotlin code specific to each target. Kotlin/JVM is used for androidMain and Kotlin/Native for iosMain:

Source sets and modules structure

When the shared module is built into an Android library, common Kotlin code is treated as Kotlin/JVM. When it is built into an iOS framework, common Kotlin is treated as Kotlin/Native:

Common Kotlin, Kotlin/JVM, and Kotlin/Native

Write common declarations

The common source set contains shared code that can be used across multiple target platforms. It's designed to contain code that is platform-independent. If you try to use platform-specific APIs in the common source set, the IDE will show a warning:

  1. Open the Greeting.kt file and try to access one of the Java classes, java.util.Random().nextBoolean(), inside the greet() function:

    import java.util.Random fun greet(): String { val firstWord = if (Random().nextBoolean()) "Hi!" else "Hello!" return firstWord }

    Android Studio highlights that the Random class is unresolved because you can't call specific Java functions from the common Kotlin code.

  2. Follow the IDE's suggestions and replace it with kotlin.random.Random from the Kotlin standard library. This is a multiplatform library that works on all platforms and is included automatically as a dependency.

  3. Remove brackets from Random(), as it is an abstract class. The code should now compile successfully.

  4. Add a bit of variety to the greeting. Update the shared code with the reversed() function from the Kotlin standard library to reverse the text:

    import kotlin.random.Random class Greeting { private val platform: Platform = getPlatform() fun greet(): String { val firstWord = if (Random.nextBoolean()) "Hi!" else "Hello!" return "$firstWord Guess what this is! > ${platform.name.reversed()}!" } }

Writing the code only in common Kotlin has obvious limitations because it can't use any platform specifics. Using interfaces and the expect/actual mechanism solves this.

Check out platform-specific implementations

The common source set can define an interface or an expected declaration. Then each platform source set, in this case androidMain and iosMain, has to provide actual platform-specific implementations for the expected declarations from the common source set.

While generating the code for a specific platform, the Kotlin compiler merges expected and actual declarations and generates a single declaration with actual implementations.

  1. When creating a project with the web wizard or using the Kotlin Multiplatform plugin in Android Studio, you get a template with the Platform.kt file in the commonMain module:

    interface Platform { val name: String }

    It's a common Platform interface with information about the platform.

  2. Switch between the androidMain and the iosMain modules. You'll see that they have different implementations of the same functionality for the Android and the iOS source sets:

    // Platform.android.kt in the androidMain module: class AndroidPlatform: Platform { override val name: String = "Android ${android.os.Build.VERSION.SDK_INT}" }
    // Platform.ios.kt in the iosMain module: import platform.UIKit.UIDevice class IOSPlatform: Platform { override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion }
    • The name property implementation from AndroidPlatform uses the Android platform code, namely the android.os.Build dependency. This code is written in Kotlin/JVM. If you try to access java.util.Random here, this code will compile.

    • The name property implementation from IOSPlatform uses iOS platform code, namely the platform.UIKit.UIDevice dependency. It's written in Kotlin/Native, meaning you can write iOS code in Kotlin. This code becomes a part of the iOS framework, which you will later call from Swift in your iOS application.

  3. Check the getPlatform() function in different source sets. Its expected declaration doesn't have a body, and actual implementations are provided in the platform code:

    // Platform.kt in the commonMain module: expect fun getPlatform(): Platform
    // Platform.android.kt in the androidMain module: actual fun getPlatform(): Platform = AndroidPlatform()
    // Platform.ios.kt in the iosMain module: actual fun getPlatform(): Platform = IOSPlatform()

Here, the common source set defines an expected getPlatform() function and has actual implementations, AndroidPlatform() for the Android app and IOSPlatform() for the iOS app, in the platform source sets.

While generating the code for a specific platform, the Kotlin compiler merges expected and actual declarations into a single getPlatform() function with its actual implementations.

That's why expected and actual declarations should be defined in the same package − they are merged into one declaration in the resulting platform code. Any invocation of the expected getPlatform() function in the generated platform code calls a correct actual implementation.

Now you can run the apps and see all of this in action.

Explore the expect/actual mechanism (optional)

The template project uses the expect/actual mechanism for functions, but it also works for most Kotlin declarations, such as properties and classes. Let's implement an expected property:

  1. Open Platform.kt in the commonMain module and add the following at the end of the file:

    expect val num: Int

    The Kotlin compiler complains that this property has no corresponding actual declarations in the platform modules.

  2. Try to provide the implementation right away with:

    expect val num: Int = 42

    You'll get an error saying that expected declarations must not have a body, in this case an initializer. The implementations must be provided in actual platform modules. Remove the initializer.

  3. Select the num property. Press Option + Enter and choose "Add missing actual declarations". Choose the androidMain source set. You can then complete the implementation in androidMain/Platform.android.kt:

    actual val num: Int = 1
  4. Now provide the implementation for the iosMain module. Add the following to iosMain/Platform.ios.kt:

    actual val num: Int = 2
  5. Add the num property to the greet() function to see the differences:

    fun greet(): String { val firstWord = if (Random.nextBoolean()) "Hi!" else "Hello!" return "$firstWord [$num] Guess what this is! > ${platform.name.reversed()}!" }

Run your application

You can run your multiplatform application for both Android or iOS from Android Studio.

Run your application on Android

  1. In the list of run configurations, select composeApp.

  2. Choose an Android virtual device next to the list of configurations and click Run.

    If you don't have a device in the list, create a new Android virtual device.

    Run multiplatform app on Android
    First mobile multiplatform app on Android

Run your application on iOS

  1. Launch Xcode in a separate window. The first time you do this, you may also need to accept the license terms and allow Xcode to perform some necessary initial tasks.

  2. In Android Studio, select iosApp in the list of run configurations and click Run.

    If you don't have an available iOS configuration in the list, add a new run configuration.

    Run multiplatform app on iOS
    First mobile multiplatform app on iOS

Run on a new iOS simulated device

If you want to run your application on a 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 then select iOS Application.

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

  4. Select the Xcode project file. To do so, navigate to your project, for example, KotlinMultiplatformSandbox, open the iosApp folder, and then select the .xcodeproj file.

  5. In the Execution target list, select a simulated device and then click OK.

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

Run on a real iOS device

You can run your multiplatform application on a real iOS device. Before you start, you'll need to set the Team ID associated with your Apple ID.

Set your Team ID

To set the Team ID in your project, you can either use the KDoctor tool in Android Studio or choose your team in Xcode.

For KDoctor:

  1. In Android Studio, run the following command in the terminal:

    kdoctor --team-ids

    KDoctor will list all Team IDs currently configured on your system, for example:

    3ABC246XYZ (Max Sample) ZABCW6SXYZ (SampleTech Inc.)
  2. In Android Studio, open the iosApp/Configuration/Config.xcconfig and specify your Team ID.

Alternatively, choose the team in Xcode:

  1. Go to Xcode and select Open a project or file.

  2. Navigate to the iosApp/iosApp.xcworkspace file of your project.

  3. In the left-hand menu, select iosApp.

  4. Navigate to Signing & Capabilities.

  5. In the Team list, select your team.

    If you haven't set up your team yet, use the Add an Account option in the Team list and follow Xcode instructions.

  6. Make sure that the Bundle Identifier is unique and the Signing Certificate is successfully assigned.

Run the app

Connect your iPhone with a cable. If you already have the device registered in Xcode, Android Studio should show it in the list of run configurations. Run the corresponding iosApp configuration.

If you haven't registered your iPhone in Xcode yet, follow Apple recommendations. In short, you should:

  1. Connect your iPhone with a cable.

  2. On your iPhone, enable the developer mode in Settings | Privacy & Security.

  3. In Xcode, go to the top menu and choose Window | Devices and Simulators.

  4. Click on the plus sign. Select your connected iPhone and click Add.

  5. Sign in with your Apple ID to enable development capabilities on the device.

  6. Follow the on-screen instructions to complete the pairing process.

Once you've registered your iPhone in Xcode, create a new run configuration in Android Studio and select your device in the Execution target list. Run the corresponding iosApp configuration.

Next step

In the next part of the tutorial, you'll learn how to update the UI elements using platform-specific libraries.

Proceed to the next part

See also

Get help

Last modified: 20 June 2024