A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-create-first-app.html below:

Create your Kotlin Multiplatform app

Create your Kotlin Multiplatform app

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

Kotlin Multiplatform technology simplifies the development of cross-platform projects. Kotlin Multiplatform applications can work on a variety of platforms like iOS, Android, macOS, Windows, Linux, web, and others.

One of the major Kotlin Multiplatform use cases is sharing code between mobile platforms. You can share application logic between iOS and Android apps and write platform-specific code only when you need to implement a native UI or work with platform APIs.

Create a project
  1. In the quickstart, complete the instructions to set up your environment for Kotlin Multiplatform development.

  2. In IntelliJ IDEA, select File | New | Project.

  3. In the panel on the left, select Kotlin Multiplatform.

  4. Specify the following fields in the New Project window:

  5. Select Android and iOS targets.

  6. For iOS, select the Do not share UI option to keep the UI native.

  7. Once you've specified all the fields and targets, click Create.

IntelliJ IDEA may automatically suggest upgrading the Android Gradle plugin in the project to the latest version. We don't recommend upgrading as Kotlin Multiplatform is not compatible with the latest AGP version (see the compatibility table).

Examine the project structure

In IntelliJ IDEA, expand the GreetingKMP folder.

This Kotlin Multiplatform project includes three modules:

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:

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:

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 shared/src/commonMain/kotlin/com/jetbrains/greeting/greetingkmp/Greeting.kt file where you can find an automatically generated Greeting class with a greet() function:

    class Greeting { private val platform = getPlatform() fun greet(): String { return "Hello, ${platform.name}!" } }

  2. Add a bit of variety to the greeting. Import 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. Update the shared code with the reversed() call 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-specific functionality. Using interfaces and the expect/actual mechanism solves this.

Check out platform-specific implementations

The common source set can define expected declarations (interfaces, classes, and so on). Then each platform source set, in this case androidMain and iosMain, has to provide actual platform-specific implementations for the expected declarations.

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 Kotlin Multiplatform project with IntelliJ IDEA, 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: import android.os.Build class AndroidPlatform : Platform { override val name: String = "Android ${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 }

  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 source set expect fun getPlatform(): Platform

    // Platform.android.kt in the androidMain source set actual fun getPlatform(): Platform = AndroidPlatform()

    // Platform.ios.kt in the iosMain source set 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. Hover the num property and click Create missed actuals.... 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. In the commonMain/Greeting.kt file, 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 IntelliJ IDEA.

If you have explored the expect/actual mechanism earlier, you can see "[1]" added to the greeting for Android and "[2]" added for iOS.

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 your application on iOS

If you haven't launched Xcode as part of the initial setup, do that before running the iOS app.

In IntelliJ IDEA, select iosApp in the list of run configurations, select a simulated device next to the run configuration, and click Run.

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

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.

  2. Click the + button above the list of configurations and then select Xcode Application.

  3. Name your configuration.

  4. Select the Working directory. To do so, navigate to your project, for example, KotlinMultiplatformSandbox, in the iosApp folder.

  5. 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 IntelliJ IDEA or choose your team in Xcode.

For KDoctor:

  1. In IntelliJ IDEA, 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 IntelliJ IDEA, 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, IntelliJ IDEA 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 IntelliJ IDEA 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: 28 July 2025


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4