Compose Compiler-Gradle-Plug-in einrichten

Verwenden Sie für Gradle das Compose Compiler Gradle-Plug-in, um Compose einzurichten und zu konfigurieren.

Mit Gradle-Versionskatalogen einrichten

Richten Sie das Gradle-Plug-in für den Compose-Compiler ein:

  1. Entfernen Sie in der Datei libs.versions.toml alle Verweise auf den Compose-Compiler.
  2. Fügen Sie in den Abschnitten versions und plugins die neue Abhängigkeit hinzu:

    [versions]
    kotlin = "2.4.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" }
    
  3. Fügen Sie in der build.gradle.kts-Datei im Stammverzeichnis des Projekts dem Abschnitt plugins Folgendes hinzu.

    plugins {
    // Existing plugins
    alias(libs.plugins.compose.compiler) apply false
    }
    
  4. 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 Sie benutzerdefinierte Optionen für den Compose-Compiler konfiguriert haben, folgen Sie dem 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.4.10" // this version matches your Kotlin version
}

Fügen Sie den Klassenpfad der Datei build.gradle.kts auf oberster Ebene Ihres Projekts hinzu:

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.4.10")
    }
}

Konfigurationsoptionen mit dem Compose Compiler Gradle-Plug-in

Wenn Sie den Compose-Compiler mit dem Gradle-Plug-in konfigurieren möchten, fügen Sie den Block composeCompiler auf der obersten Ebene der Datei build.gradle.kts des Moduls hinzu:

android { … }

composeCompiler {
    reportsDestination = layout.buildDirectory.dir("compose_compiler")
    stabilityConfigurationFiles.add(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.11.01.

Setzen Sie das Flag compose in der Android-BuildFeatures auf true, um die Compose-Funktion in Android Studio zu aktivieren.

Fügen Sie der Datei build.gradle Ihrer Anwendung 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.11.01')
    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.11.1'
    // 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.11.01")
    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.11.1")
    // Optional - Integration with LiveData
    implementation("androidx.compose.runtime:runtime-livedata")
    // Optional - Integration with RxJava
    implementation("androidx.compose.runtime:runtime-rxjava2")

}

Kompatibilität von compileSdk und Android-Gradle-Plug-in

In Compose-Bibliotheksreleases werden kontinuierlich die neuesten compileSdk-Versionen übernommen, um Zugriff auf die neuesten Android-Funktionen zu bieten. Für neuere compileSdk-Versionen sind neuere Versionen des Android-Gradle-Plug-ins erforderlich. Wenn Sie also neue Compose-Releases verwenden, müssen Sie auch neue Versionen des Android-Gradle-Plug-ins verwenden. Wir empfehlen, die compileSdk Ihres Projekts auf dem neuesten Stand zu halten. compileSdk steht in keinem Zusammenhang mit targetSdk.

Ab Compose 1.12.0 müssen in Projekten beispielsweise compileSdk 37 und das Android-Gradle-Plug-in (AGP) 9 verwendet werden.

Informationen dazu, welche AGP-Version für die verschiedenen API-Level unterstützt wird, finden Sie in der Dokumentation zur Unterstützung von API-Leveln durch das Android-Gradle-Plug-in.