Untuk Gradle, gunakan plugin Gradle Compose Compiler untuk menyiapkan dan mengonfigurasi Compose.
Menyiapkan dengan katalog versi Gradle
Siapkan plugin Gradle Compose Compiler:
- Dalam file
libs.versions.toml, hapus referensi apa pun ke Compiler Compose. Di bagian
versionsdanplugins, tambahkan dependensi baru:[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" }Dalam file
build.gradle.ktsroot project, tambahkan kode berikut ke bagianplugins.plugins { // Existing plugins alias(libs.plugins.compose.compiler) apply false }Di setiap modul yang menggunakan Compose, terapkan plugin:
plugins { // Existing plugins alias(libs.plugins.compose.compiler) }
Project sekarang akan di-build dan dikompilasi jika menggunakan penyiapan default. Jika telah mengonfigurasi opsi kustom di compiler Compose, ikuti bagian berikutnya.
Menyiapkan Compose Compiler tanpa katalog versi Gradle
Tambahkan plugin ke file build.gradle.kts yang terkait dengan modul tempat Compose
digunakan:
plugins {
id("org.jetbrains.kotlin.plugin.compose") version "2.4.10" // this version matches your Kotlin version
}
Tambahkan classpath ke file build.gradle.kts project level teratas:
buildscript {
dependencies {
classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.4.10")
}
}
Opsi konfigurasi dengan Plugin Gradle Compose Compiler
Untuk mengonfigurasi compiler Compose menggunakan plugin Gradle, tambahkan
blok composeCompiler ke file build.gradle.kts modul di tingkat
teratas:
android { … }
composeCompiler {
reportsDestination = layout.buildDirectory.dir("compose_compiler")
stabilityConfigurationFiles.add(rootProject.layout.projectDirectory.file("stability_config.conf"))
}
Untuk mengetahui daftar lengkap opsi yang tersedia, lihat dokumentasi.
Menyiapkan dependensi Compose
Selalu gunakan versi BOM Compose terbaru: 2026.11.01.
Setel flag compose ke true dalam BuildFeatures Android untuk
mengaktifkan fungsi Compose di Android Studio.
Tambahkan definisi berikut ke file build.gradle aplikasi Anda:
Groovy
android {
buildFeatures {
compose true
}
}
Kotlin
android {
buildFeatures {
compose = true
}
}
Tambahkan Compose BOM dan subset dependensi library Compose:
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")
}
Kompatibilitas compileSdk dan Plugin Android Gradle
Rilis library Compose terus mengadopsi versi compileSdk terbaru untuk
memberikan akses ke fitur Android terbaru. Versi compileSdk yang lebih baru
memerlukan versi Plugin Android Gradle yang lebih baru, sehingga mengadopsi rilis Compose
baru juga mengharuskan project mengadopsi versi baru Plugin Android
Gradle. Sebaiknya selalu update compileSdk project Anda dengan versi rilis terbaru. compileSdk tidak terkait dengan targetSdk.
Misalnya, mulai Compose 1.12.0, project harus menggunakan
compileSdk 37 dan Plugin Android Gradle (AGP) 9.
Untuk memeriksa versi AGP yang didukung untuk berbagai level API, lihat dokumentasi Dukungan level API plugin Android Gradle.