What is KMP? Kotlin Multiplatform Overview

What is KMP? Kotlin Multiplatform Overview

What is KMP? Kotlin Multiplatform technology aims to streamline the creation of cross-platform projects. 

It minimizes the effort required to write and maintain identical code for various platforms while preserving the flexibility and advantages of native programming.

What is KMP?
What is KMP?

What is KMP? Kotlin Multiplatform use cases

A key use case for Kotlin Multiplatform is sharing code between mobile platforms. With Kotlin Multiplatform, you can develop cross-platform mobile applications that share code for Android and iOS projects, enabling you to efficiently handle networking, data storage, data validation, analytics, computations, and other application logic

What is KMP?
What is KMP?

Code sharing between platforms

Kotlin Multiplatform enables you to maintain a single codebase for application logic across different platforms while enjoying the benefits of native programming, such as excellent performance and full access to platform SDKs.

Kotlin offers the following code-sharing mechanisms:

  • Share common code across all platforms used in your project.
  • Share code among specific platforms in your project, allowing for code reuse on similar platforms
KMP - Code Sharing
KMP - Code Sharing

Create a Kotlin Multiplatform app

Start using the web wizard to create a new Kotlin Multiplatform application.

To create a new Kotlin Multiplatform project named “ComposeDemo,” follow these steps:

1. Open the Kotlin Multiplatform wizard: Visit the Kotlin Multiplatform website or open the wizard from your preferred Kotlin development environment.

2. Configure the project:

  • New project tab: Change the project name to `ComposeDemo`.
  • Set the project ID to `kmp.project.demo`.

3. Select platforms:

  • Check the boxes for Android, Desktop, and Web.
  • If you’re using a Mac, select iOS as well.
  • Ensure that the Share UI option is selected.

4. Download and unpack:

  • Click the Download button to download the project archive.
  • Unpack the downloaded archive to access your new project files.

You now have a new Kotlin Multiplatform project configured for Android, Desktop, Web, and optionally iOS, with shared UI components

What is KMP? - MultiPlatform Wizard
What is KMP? - MultiPlatform Wizard

Examine the project structure

Here’s how to examine the project structure of your “ComposeDemo” Kotlin Multiplatform project:

1. Launch Android Studio

2. Open the project:

  • On the Welcome screen, click Open, or go to File | Open in the editor.
  • Navigate to the unpacked ComposeDemo folder  — Click Open

3. Project detection:

  • Android Studio will detect that the folder contains a Gradle build file and will open it as a new project.
  • If you didn’t select iOS in the wizard, you won’t see folders beginning with “ios” or “apple”.
KMP - Project Structure
KMP - Project Structure

4. Switch to Project view:

  • The default view in Android Studio is optimized for Android development. To see the full project structure, switch the view from Android to Project:

5. Project modules: The project contains two main modules:

  • composeApp: This Kotlin module contains the logic shared among the Android, desktop, iOS (if selected), and web applications. It uses Gradle to automate the build process.
  • iosApp: This module is an Xcode project that builds into an iOS application. It depends on and uses the shared module (`composeApp`) as an iOS framework
KMP - Folders
KMP - Folders

Source Sets in composeApp:

  1. commonMain:
  • Contains common Kotlin code shared across all platforms.
  • This is where you write the platform-agnostic logic.

2. androidMain:

  • Contains Android-specific Kotlin code.
  • Uses Kotlin/JVM.

3. desktopMain:

  • Contains desktop-specific Kotlin code.
  • Also uses Kotlin/JVM.

4. iosMain:

  • Contains iOS-specific Kotlin code.
  • Uses Kotlin/Native.

5. wasmJsMain:

  • Contains code for web applications.
  • Uses Kotlin/Wasm

KMP - Source Structure
KMP - Source Structure

Run your application

For Android:

  1. Create an Android virtual device.
  2. In the list of run configurations, select composeApp.
  3. Choose your Android virtual device and then click Run
KMP - Android
KMP - Android

For iOS:

  1. Launch Xcode in a separate window to complete the initial setup.
  2. In Android Studio, select iosApp in the list of run configurations and click Run

Kotlin Multiplatform samples

This curated list showcases cross-platform projects developed with Kotlin Multiplatform. Some projects share nearly all their functionality using Compose Multiplatform for the user interface. Others employ native code for the user interface while sharing components like the data model and algorithms. 

KMP - Samples
KMP - Samples