Gradle-Plug-in für den Compose-Compiler einrichten
Verwenden Sie für Gradle das Gradle-Plug-in für den Compose-Compiler, um Compose einzurichten und zu konfigurieren.
Mit Gradle-Versionskatalogen einrichten
So richten Sie das Gradle-Plug-in für den Compose-Compiler ein:
- Entfernen Sie in der Datei
libs.versions.tomlalle Verweise auf den Compose-Compiler. - Fügen Sie in den Abschnitten
versionsundpluginsdie neue Abhängigkeit hinzu:
[versions]
kotlin = "2.3.10"
[plugins]
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
// Add this line
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
- Fügen Sie in der
build.gradle.kts-Datei des Projekts im AbschnittpluginsFolgendes hinzu:
plugins {
// Existing plugins
alias(libs.plugins.compose.compiler) apply false
}
- Wenden Sie das Plug-in in jedem Modul an, das Compose verwendet:
plugins {
// Existing plugins
alias(libs.plugins.compose.compiler)
}
Das Projekt sollte jetzt erstellt und kompiliert werden, wenn die Standardeinrichtung verwendet wurde. Wenn benutzerdefinierte Optionen für den Compose-Compiler konfiguriert wurden, folgen Sie der Anleitung im nächsten Abschnitt.
Compose-Compiler ohne Gradle-Versionskataloge einrichten
Fügen Sie das Plug-in den build.gradle.kts-Dateien hinzu, die mit Modulen verknüpft sind, in denen Compose verwendet wird:
plugins {
id("org.jetbrains.kotlin.plugin.compose") version "2.3.10" // this version matches your Kotlin version
}
Fügen Sie den Klassenpfad der build.gradle.kts-Datei Ihres Projekts auf oberster Ebene hinzu:
buildscript {
dependencies {
classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.3.10")
}
}
Konfigurationsoptionen mit dem Gradle-Plug-in für den Compose-Compiler
Wenn Sie den Compose-Compiler mit dem Gradle-Plug-in konfigurieren möchten, fügen Sie den Block composeCompiler der build.gradle.kts-Datei des Moduls auf oberster Ebene hinzu:
android { … }
composeCompiler {
reportsDestination = layout.buildDirectory.dir("compose_compiler")
stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
}
Eine vollständige Liste der verfügbaren Optionen finden Sie in der Dokumentation.
Compose-Abhängigkeiten einrichten
Verwenden Sie immer die neueste Compose-BOM-Version: 2026.03.00.
Setzen Sie das Flag compose in BuildFeatures von Android auf true, um die Compose-Funktionalität in Android Studio zu aktivieren.
Fügen Sie der build.gradle-Datei Ihrer App die folgende Definition hinzu:
Groovy
android {
buildFeatures {
compose true
}
}
Kotlin
android {
buildFeatures {
compose = true
}
}
Fügen Sie die Compose-BOM und die Teilmenge der Compose-Bibliotheksabhängigkeiten hinzu:
Groovy
dependencies {
def composeBom = platform('androidx.compose:compose-bom:2026.03.00')
implementation composeBom
androidTestImplementation composeBom
// Choose one of the following:
// Material Design 3
implementation 'androidx.compose.material3:material3'
// or skip Material Design and build directly on top of foundational components
implementation 'androidx.compose.foundation:foundation'
// or only import the main APIs for the underlying toolkit systems,
// such as input and measurement/layout
implementation 'androidx.compose.ui:ui'
// Android Studio Preview support
implementation 'androidx.compose.ui:ui-tooling-preview'
debugImplementation 'androidx.compose.ui:ui-tooling'
// UI Tests
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
// Optional - Add window size utils
implementation 'androidx.compose.material3.adaptive:adaptive'
// Optional - Integration with activities
implementation 'androidx.activity:activity-compose:1.13.0'
// Optional - Integration with ViewModels
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.10.0'
// Optional - Integration with LiveData
implementation 'androidx.compose.runtime:runtime-livedata'
// Optional - Integration with RxJava
implementation 'androidx.compose.runtime:runtime-rxjava2'
}
Kotlin
dependencies {
val composeBom = platform("androidx.compose:compose-bom:2026.03.00")
implementation(composeBom)
androidTestImplementation(composeBom)
// Choose one of the following:
// Material Design 3
implementation("androidx.compose.material3:material3")
// or skip Material Design and build directly on top of foundational components
implementation("androidx.compose.foundation:foundation")
// or only import the main APIs for the underlying toolkit systems,
// such as input and measurement/layout
implementation("androidx.compose.ui:ui")
// Android Studio Preview support
implementation("androidx.compose.ui:ui-tooling-preview")
debugImplementation("androidx.compose.ui:ui-tooling")
// UI Tests
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-test-manifest")
// Optional - Add window size utils
implementation("androidx.compose.material3.adaptive:adaptive")
// Optional - Integration with activities
implementation("androidx.activity:activity-compose:1.13.0")
// Optional - Integration with ViewModels
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.10.0")
// Optional - Integration with LiveData
implementation("androidx.compose.runtime:runtime-livedata")
// Optional - Integration with RxJava
implementation("androidx.compose.runtime:runtime-rxjava2")
}