Get started with Compose Multiplatform
Compose Multiplatform apps consist of a single Kotlin module with multiple source sets, each one for every platform you wish your app to run on.
You can define the targets within the kotlin
block of your build.gradle.kts
file.
Step 1: Set up your development environment
Before you do any multiplatform development, make sure to have the required tools installed on your machine.
Check out Jetbrains' 'Install the necessary tools' on the topic, or use kdoctor if you are on a Mac.
Step 2a: Use the Kotlin Multiplatform Wizard
There is no option on Android Studio or Intellij IDEA to create a new Compose Multiplatform project. The closest thing we have is the Kotlin Multiplatform Wizard. Use it to create a template with the platforms of your choice and then import it in your favorite IDE.
Step 2b: Use the Compose App template
Even though Jetbrains seem to be keeping the KMP Wizard up to date, you might still need to understand how things are wired under the hood. This section shows you how to wire things manually.
Start by creating a new project based off the Compose App template.
The template contains an empty Compose Multiplatform project with no platforms or dependencies.
Next, let's set up the platforms you need in your project:
Add & configure the JVM target
Add the jvm()
target in the kotlin
block:
// build.gradle.kts (composeApp)
kotlin {
// 1. Add the jvm target
jvm()
sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.ui)
implementation(compose.components.uiToolingPreview)
}
// 2. Add the following jvm dependencies
jvmMain.dependencies {
implementation(compose.desktop.common)
implementation(compose.desktop.currentOs)
}
}
}
// 3. Configure the desktop app
compose.desktop {
application {
// the entry point to your app
mainClass = "MainKt"
}
}
Main.kt
Create In the previous step we specified MainKt
as our mainClass
(entry point) to our application.
Create a file called Main.kt
at composeApp/src/jvmMain/kotlin/Main.kt
and add the following code:
// Main.kt
import androidx.compose.foundation.text.BasicText
import androidx.compose.ui.window.singleWindowApplication
fun main() = singleWindowApplication {
// you can use composable functions here
BasicText("Desktop says hi")
}
Run the desktop app
Run your new desktop app using ./gradlew run
.