# Getting Started with Compose Multiplatform
## What is covered
In this tutorial we will create a simple desktop UI application
using the Compose Multiplatform UI framework.
## Prerequisites
Compose for Desktop can produce applications for macOS, Linux and Windows platforms.
So any of these platforms can be used for this tutorial.
The following software must be preinstalled:
* JDK 11 or later
* IntelliJ IDEA Community Edition or Ultimate Edition 2020.3 or later (other editors could be used, but we assume you are using IntelliJ IDEA in this tutorial)
## Creating a new project
### New project wizard
Starting with the version 2020.3, Kotlin support in IDEA comes with the new project wizard,
which creates a Compose application automatically.
Note that JDK must be at least JDK 11, and to use the native distribution
packaging, JDK 15 or later must be used.
< img alt = "Create new project 1" src = "wizard1.png" height = "500" / >
< img alt = "Create new project 2" src = "wizard2.png" height = "500" / >
### IDE plugin
Compose Multiplatform [IDEA plugin ](https://plugins.jetbrains.com/plugin/16541-compose-multiplatform-ide-support )
can simplify compose development by adding support for the `@Preview` annotation on argument-less
`@Composable` functions. You can see how a particular composable function looks
directly in the IDE panel. This plugin can also be installed via the plugins marketplace.
Just search for "Compose Multiplatform".
### Update the wizard plugin
The Compose plugin version used in the wizard above might not be the latest. Update to the latest plugin version by editing the `build.gradle.kts` file and updating the version information as shown below.
For the latest versions, see the [latest versions ](https://github.com/JetBrains/compose-jb/releases ) site and the [Kotlin ](https://kotlinlang.org/ ) site.
```
plugins {
kotlin("jvm") version "1.7.20"
Release 1.2.2 (#2561)
* Compose 1.2.1-rc01
* Fix Web build for Kotlin 1.7.20
* Use 1.3.2.1-rc02 in Gradle plugin
* Fix Gradle Plugin tests
* Fix Gradle Plugin tests
* Compose 1.2.1-rc03
* Update CHANGELOG.md
* Update CHANGELOG.md
* Compose Compiler 1.3.2.1
* Compose 1.2.1
* Update VERSIONING.md
* Update gradle.properties
* Fix custom JDK tests on Linux
* Remove JVM target version override (#2515)
Previously, we were setting kotlin.jvmTarget version
to 1.8 if it was null or < 1.8.
As an unintended consequence we were also overriding
a version set by the jvmToolchain property.
So while users expected the jvmToolchain property
to set both jdk home & jdk target, we were quietly
overriding jdk target.
At the same time, Kotlin 1.7 sets the minimum
target version to 1.8 anyway, so our override
does not make sense with Kotlin 1.7+.
This commit removes overriding altogether.
Fixes #2511
* Update CHANGELOG.md
* Update CHANGELOG.md
* Update CHANGELOG.md
* Update Compose
* Update default ProGuard rules with changes from main branch
* Test Gradle plugin on relevant PRs (#2509)
* Update Gradle used in tooling subprojects
* Update Kotlin in Compose Gradle plugin
* Decrease verbosity of Gradle plugin tests
* Disable mac sign test
* Add workflow to test Gradle plugin
* Fix custom jdk tests on Linux
* Make Compose Gradle plugin build compatible with Configuration cache
* Print tests summary
* Remove unused code
* Refactor tests configuration
* Turn off parallel execution
* Try adding windows runner
* Turn off fail fast
* Fix Windows test issues
#2368
* Adjust default proguard rules
The following rule is needed to fix tests on Windows:
```
-dontwarn org.graalvm.compiler.core.aarch64.AArch64NodeMatchRules_MatchStatementSet*
```
Other rules are just to make builds less noisy.
Kotlin's `*.internal` packages often contain
bytecode, which triggers ProGuard's notes.
However, these notes are not actionable for
most users, so we can ignore notes by default.
#2393
# Conflicts:
# gradle-plugins/gradle/wrapper/gradle-wrapper.properties
* Improve DSL for setting a custom Compose Plugin (#2527)
* Improve DSL for setting a custom Compose Plugin
Fixes https://github.com/JetBrains/compose-jb/issues/2459
Readme: https://github.com/JetBrains/compose-jb/pull/2526
1. Add `dependencies: Dependencies` extension that is accessible in `compose { }` block
2. Add `Dependencies.compiler` property that can return versions of Compose compiler used by the plugin:
```
compose {
kotlinCompilerPlugin.set(dependencies.compiler.forKotlin("1.7.20"))
//kotlinCompilerPlugin.set(dependencies.compiler.auto) // determined by applied version of Kotlin. It is a default.
}
```
3. Add ability to set arguments for Compose Compiler. Now we can write:
```
compose {
kotlinCompilerPlugin.set(dependencies.compiler.forKotlin("1.7.20"))
kotlinCompilerPluginArgs.add("suppressKotlinVersionCompatibilityCheck=1.7.21")
}
```
4. Remove checks for different targets
We had a separate check for JS, when we released 1.2.0. It doesn't support Kotlin 1.7.20 at that moment.
It is hard to refactor this feature in the new code, so I removed it. It is not needed now and it had an ugly code. When we will need it again, we'll write it again.
5. Remove the `compose.tests.androidx.compiler.version` property from gradle.properties and remove `defaultAndroidxCompilerEnvironment`
Because they are used only in one test, and it seems there is no reason to use it in another place in the future
* Discussions
* Update ComposeCompilerCompatability.kt (#2557)
* Update CHANGELOG.md
* 1.2.2-rc01
* Update Compose
* Update CHANGELOG.md
* Compose 1.2.2
* Remove shared.podspec
* Remove usages of deprecated Intellij APIs
Co-authored-by: Alexey Tsvetkov <alexey.tsvetkov@jetbrains.com>
Co-authored-by: Alexey Tsvetkov <654232+AlexeyTsvetkov@users.noreply.github.com>
2 years ago
id("org.jetbrains.compose") version "1.2.2"
}
```
### Create a new Compose project without the wizard
It is also possible to create a Compose project manually.
The recommended way to build Compose for Desktop projects is with Gradle.
JetBrains provides a simple way of building Compose for Desktop projects
using a special Gradle plugin.
You can clone an existing template for a [desktop ](https://github.com/JetBrains/compose-jb/tree/master/templates/desktop-template ) or
[multiplatform ](https://github.com/JetBrains/compose-jb/tree/master/templates/multiplatform-template ) application, or create it from scratch.
First create a new directory, named `sample` .
```shell script
mkdir sample
cd sample
```
Create `settings.gradle.kts` as follows:
``` kotlin
pluginManagement {
repositories {
gradlePluginPortal()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
}
```
Then create `build.gradle.kts` with the following content:
``` kotlin
plugins {
kotlin("jvm") version "1.7.20"
Release 1.2.2 (#2561)
* Compose 1.2.1-rc01
* Fix Web build for Kotlin 1.7.20
* Use 1.3.2.1-rc02 in Gradle plugin
* Fix Gradle Plugin tests
* Fix Gradle Plugin tests
* Compose 1.2.1-rc03
* Update CHANGELOG.md
* Update CHANGELOG.md
* Compose Compiler 1.3.2.1
* Compose 1.2.1
* Update VERSIONING.md
* Update gradle.properties
* Fix custom JDK tests on Linux
* Remove JVM target version override (#2515)
Previously, we were setting kotlin.jvmTarget version
to 1.8 if it was null or < 1.8.
As an unintended consequence we were also overriding
a version set by the jvmToolchain property.
So while users expected the jvmToolchain property
to set both jdk home & jdk target, we were quietly
overriding jdk target.
At the same time, Kotlin 1.7 sets the minimum
target version to 1.8 anyway, so our override
does not make sense with Kotlin 1.7+.
This commit removes overriding altogether.
Fixes #2511
* Update CHANGELOG.md
* Update CHANGELOG.md
* Update CHANGELOG.md
* Update Compose
* Update default ProGuard rules with changes from main branch
* Test Gradle plugin on relevant PRs (#2509)
* Update Gradle used in tooling subprojects
* Update Kotlin in Compose Gradle plugin
* Decrease verbosity of Gradle plugin tests
* Disable mac sign test
* Add workflow to test Gradle plugin
* Fix custom jdk tests on Linux
* Make Compose Gradle plugin build compatible with Configuration cache
* Print tests summary
* Remove unused code
* Refactor tests configuration
* Turn off parallel execution
* Try adding windows runner
* Turn off fail fast
* Fix Windows test issues
#2368
* Adjust default proguard rules
The following rule is needed to fix tests on Windows:
```
-dontwarn org.graalvm.compiler.core.aarch64.AArch64NodeMatchRules_MatchStatementSet*
```
Other rules are just to make builds less noisy.
Kotlin's `*.internal` packages often contain
bytecode, which triggers ProGuard's notes.
However, these notes are not actionable for
most users, so we can ignore notes by default.
#2393
# Conflicts:
# gradle-plugins/gradle/wrapper/gradle-wrapper.properties
* Improve DSL for setting a custom Compose Plugin (#2527)
* Improve DSL for setting a custom Compose Plugin
Fixes https://github.com/JetBrains/compose-jb/issues/2459
Readme: https://github.com/JetBrains/compose-jb/pull/2526
1. Add `dependencies: Dependencies` extension that is accessible in `compose { }` block
2. Add `Dependencies.compiler` property that can return versions of Compose compiler used by the plugin:
```
compose {
kotlinCompilerPlugin.set(dependencies.compiler.forKotlin("1.7.20"))
//kotlinCompilerPlugin.set(dependencies.compiler.auto) // determined by applied version of Kotlin. It is a default.
}
```
3. Add ability to set arguments for Compose Compiler. Now we can write:
```
compose {
kotlinCompilerPlugin.set(dependencies.compiler.forKotlin("1.7.20"))
kotlinCompilerPluginArgs.add("suppressKotlinVersionCompatibilityCheck=1.7.21")
}
```
4. Remove checks for different targets
We had a separate check for JS, when we released 1.2.0. It doesn't support Kotlin 1.7.20 at that moment.
It is hard to refactor this feature in the new code, so I removed it. It is not needed now and it had an ugly code. When we will need it again, we'll write it again.
5. Remove the `compose.tests.androidx.compiler.version` property from gradle.properties and remove `defaultAndroidxCompilerEnvironment`
Because they are used only in one test, and it seems there is no reason to use it in another place in the future
* Discussions
* Update ComposeCompilerCompatability.kt (#2557)
* Update CHANGELOG.md
* 1.2.2-rc01
* Update Compose
* Update CHANGELOG.md
* Compose 1.2.2
* Remove shared.podspec
* Remove usages of deprecated Intellij APIs
Co-authored-by: Alexey Tsvetkov <alexey.tsvetkov@jetbrains.com>
Co-authored-by: Alexey Tsvetkov <654232+AlexeyTsvetkov@users.noreply.github.com>
2 years ago
id("org.jetbrains.compose") version "1.2.2"
}
repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
google()
}
dependencies {
implementation(compose.desktop.currentOs)
}
compose.desktop {
application {
mainClass = "MainKt"
}
}
```
Then create `src/main/kotlin/main.kt` and put the following code in it:
```kotlin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
fun main() = application {
Window(
onCloseRequest = ::exitApplication,
title = "Compose for Desktop",
state = rememberWindowState(width = 300.dp, height = 300.dp)
) {
val count = remember { mutableStateOf(0) }
MaterialTheme {
Column(Modifier.fillMaxSize(), Arrangement.spacedBy(5.dp)) {
Button(modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
count.value++
}) {
Text(if (count.value == 0) "Hello World" else "Clicked ${count.value}!")
}
Button(modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
count.value = 0
}) {
Text("Reset")
}
}
}
}
}
```
## Running the project
Open `build.gradle.kts` [as a project ](https://www.jetbrains.com/help/idea/jetgradle-tool-window.html ) in IntelliJ IDEA.
< img alt = "New project" src = "open.png" height = "500" / >
After you download the Compose for Desktop dependencies from the Maven repositories, your new project is ready
to go. Open the Gradle toolbar on the right and select `sample/Tasks/compose desktop/run` .
The first run may take some time. Afterwards, the following dialog will be shown:
< img alt = "Application running" src = "app.gif" height = "500" / >
You can click on the button several times and see that the application reacts and
updates the UI.
Running and debugging the `main()` function using run gutter is also supported.
< img alt = "Application running" src = "app-run.png" height = "500" / >
## Next steps
Congratulations on getting your first Compose Multiplatform project working! We encourage you to continue playing around with the areas that interest you, and look forward to seeing what you build! When you're ready to continue learning, we have many more great tutorials available here: [Compose Multiplatform Tutorials ](https://github.com/JetBrains/compose-jb#tutorials ).
We also have some more advanced [Compose Multiplatorm Example Projects ](https://github.com/JetBrains/compose-jb#examples ) that you can learn from.