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.
implementation(project(":shared"))
You’ll have a shared module that contains your business logic and all the targets you’re currently targeting.
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.