Konfigurowanie wtyczki Gradle kompilatora Compose
Jeśli używasz Gradle, możesz skorzystać z wtyczki Gradle kompilatora Compose, aby ułatwić sobie konfigurowanie Compose.
Konfigurowanie za pomocą katalogów wersji Gradle
Poniżej znajdziesz instrukcje konfigurowania wtyczki Gradle Compose Compiler:
- W pliku
libs.versions.tomlusuń wszystkie odwołania do kompilatora Compose. - W sekcji wtyczek dodaj tę nową zależność:
[versions]
kotlin = "2.0.0"
[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" }
- W pliku
build.gradle.ktsw katalogu głównym projektu dodaj ten fragment kodu do sekcji plugins.
plugins {
// Existing plugins
alias(libs.plugins.compose.compiler) apply false
}
- W każdym module, który korzysta z Compose, zastosuj wtyczkę w ten sposób:
plugins {
// Existing plugins
alias(libs.plugins.compose.compiler)
}
Jeśli używasz domyślnej konfiguracji, aplikacja powinna zostać skompilowana. Jeśli w kompilatorze Compose masz skonfigurowane opcje niestandardowe, zapoznaj się z następną sekcją.
Konfigurowanie kompilatora Compose bez katalogów wersji Gradle
Dodaj ten plugin do plików build.gradle.kts powiązanych z modułami, których używasz w Compose:
plugins {
id("org.jetbrains.kotlin.plugin.compose") version "2.0.0" // this version matches your Kotlin version
}
Może też być konieczne dodanie tej ścieżki klasy do pliku projektu najwyższego poziomu:build.gradle.kts
buildscript {
dependencies {
classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.0.0")
}
}
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
Dodaj do pliku build.gradle aplikacji tę definicję:
Groovy
android {
buildFeatures {
compose true
}
}
Kotlin
android {
buildFeatures {
compose = true
}
}
Ustawienie flagi compose na true w bloku BuildFeatures Androida włącza funkcje Compose w Android Studio.
Na koniec dodaj BOM Compose i podzbiór zależności biblioteki Compose, których potrzebujesz, do zależności z tego bloku:
Groovy
dependencies {
def composeBom = platform('androidx.compose:compose-bom:2026.01.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.11.0'
// Optional - Integration with ViewModels
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.8.5'
// 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.01.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.11.0")
// Optional - Integration with ViewModels
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.5")
// Optional - Integration with LiveData
implementation("androidx.compose.runtime:runtime-livedata")
// Optional - Integration with RxJava
implementation("androidx.compose.runtime:runtime-rxjava2")
}