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 semua referensi ke Compose Compiler. Di bagian
versionsdanplugins, tambahkan dependensi baru:[versions] kotlin = "2.3.21" [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" }Di 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 harus 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.3.21" // this version matches your Kotlin version
}
Tambahkan classpath ke file build.gradle.kts project tingkat atas Anda:
buildscript {
dependencies {
classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.3.21")
}
}
Opsi konfigurasi dengan Plugin Gradle Compiler Compose
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")
stabilityConfigurationFile = 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.06.00.
Tetapkan tanda 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 BOM Compose dan subset dependensi library Compose:
Groovy
dependencies {
def composeBom = platform('androidx.compose:compose-bom:2026.06.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.06.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")
}
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 penggunaan rilis Compose baru juga mengharuskan project menggunakan versi Plugin Android Gradle yang baru. Sebaiknya Anda selalu mengupdate compileSdk project Anda dengan versi terbaru yang dirilis. compileSdk tidak terkait dengan targetSdk.
Misalnya, mulai dari Compose 1.12.0, project diwajibkan untuk 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.