Google Endorses Kotlin Multiplatform

Google announced first class support for Kotlin Multiplatform at Google I/O 2024. This article provides an overview of the framework and how to get started creating multiplatform apps with it. By Carlos Mota.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 2 of this article. Click here to view the first page.

Creating a New KMP App

To start developing with Kotlin Multiplatform, follow one of these options:

Then import it on iOS either via CocoaPods or as a Swift Package.

  • Creating a new project: On Android Studio, go to the status bar and click FileNewNew Project…. In this new window, scroll to the bottom and select Kotlin Multiplatform App to create an Android and an iOS app, along with a shared module that will contain your app’s business logic.
  • Adding a new library: In the New Project window mentioned above, add a new library that supports Kotlin Multiplatform. To use it in your Android app, add it as an implementation in build.gradle.kts file:
    implementation(project(":shared"))
    

    Then import it on iOS either via CocoaPods or as a Swift Package.

  • Kotlin Multiplatform Wizard: JetBrains developed an online tool that helps you customize your project by defining its name, package, and the platforms that you want to target. Once everything is ready, it’ll generate a compressed file for you to unzip and open with Android Studio.
implementation(project(":shared"))

Kotlin Multiplatform Wizard allows you to customize your project

You’ll have a shared module that contains your business logic and all the targets you’re currently targeting.

Project tree structure. Android app and the shared module with all the targets where it can run

If you open the build.gradle.kts file located inside this folder, you’ll spot the following differences from a generic Android project:

If they’re Android-related:

And if they belong to iOS:

  • Kotlin Multiplatform library:
    alias(libs.plugins.kotlinMultiplatform)
    
  • Different application targets inside the kotlin section:
    androidTarget()
    
    listOf(
      iosX64(),
      iosArm64(),
      iosSimulatorArm64()
    )
    
  • Libraries are imported according to their dependency. If it’s going to be used by all the platforms, and they support Kotlin Multiplatform, they’re added on commonMain:
    commonMain.dependencies {
      // Libraries go here
    }
    

    If they’re Android-related:

    androidMain.dependencies {
      // Libraries go here
    }
    

    And if they belong to iOS:

    iosMain.dependencies {
      // Libraries go here
    }
    
alias(libs.plugins.kotlinMultiplatform)
androidTarget()

listOf(
  iosX64(),
  iosArm64(),
  iosSimulatorArm64()
)
commonMain.dependencies {
  // Libraries go here
}
androidMain.dependencies {
  // Libraries go here
}
iosMain.dependencies {
  // Libraries go here
}

In the sample app, Fruitties, you can also see the androidApp and the iOSApp, which import the generated library/framework from the shared module.

Where to Go From Here?

Google plans to further support Kotlin Multiplatform on other existing Jetpack libraries used for Android development and keep improving its tools for Multiplatform support, namely Android Lint, Kotlin Symbol Processing (KSP) and Android Studio.

Note: This article provided a brief overview of the Kotlin Multiplatform framework. If you are looking for a deep dive, see our Kotlin Multiplatform by Tutorials book. It is written by the author of this article.

Although not mentioned during Google I/O, Compose Multiplatform is now stable. With the increased number of developers building libraries and adapting the existing ones to Multiplatform, Google is expected to follow the same path. In the next few years, you’ll see a similar announcement.

You can follow everything announced during this year’s Google I/O directly from the official website and keep updated with the latest launches at the Google Developers blog. To learn more about Kotlin Multiplatform, read our book and keep checking back with Kodeco for more KMP updates.