Konfigurowanie wtyczki Gradle kompilatora Compose

W przypadku Gradle użyj wtyczki Gradle kompilatora Compose, aby skonfigurować Compose.

Konfigurowanie za pomocą katalogów wersji Gradle

Skonfiguruj wtyczkę Gradle kompilatora Compose:

  1. W pliku libs.versions.toml usuń wszelkie odwołania do kompilatora Compose.
  2. W sekcjach versionsplugins dodaj nową zależność:
[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" }
  1. W pliku build.gradle.kts w katalogu głównym projektu dodaj ten kod do sekcji plugins.
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler) apply false
}
  1. W każdym module, który korzysta z Compose, zastosuj wtyczkę:
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler)
}

Jeśli projekt korzystał z domyślnej konfiguracji, powinien zostać skompilowany. Jeśli w kompilatorze Compose skonfigurowano opcje niestandardowe, przejdź do następnej sekcji.

Konfigurowanie kompilatora Compose bez katalogów wersji Gradle

Dodaj wtyczkę do plików build.gradle.kts powiązanych z modułami, w których używany jest Compose:

plugins {
    id("org.jetbrains.kotlin.plugin.compose") version "2.3.10" // this version matches your Kotlin version
}

Dodaj ścieżkę klasy do pliku build.gradle.kts projektu najwyższego poziomu:

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

Opcje konfiguracji w przypadku wtyczki Gradle Compose Compiler

Aby skonfigurować kompilator Compose za pomocą wtyczki Gradle, dodaj blok composeCompiler do pliku build.gradle.kts modułu na najwyższym poziomie:

android {  }

composeCompiler {
    reportsDestination = layout.buildDirectory.dir("compose_compiler")
    stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
}

Pełną listę dostępnych opcji znajdziesz w dokumentacji.

Konfigurowanie zależności Compose

Zawsze używaj najnowszej wersji BOM Compose: 2026.03.00.

Ustaw flagę compose na true w Androidzie BuildFeatures, aby włączyć funkcje Compose w Android Studio.

Dodaj do pliku build.gradle aplikacji tę definicję:

Groovy

android {
    buildFeatures {
        compose true
    }
}

Kotlin

android {
    buildFeatures {
        compose = true
    }
}

Dodaj BOM Compose i podzbiór zależności biblioteki Compose:

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")

}