ตั้งค่าปลั๊กอิน Gradle ของตัวคอมไพล์ Compose
สำหรับผู้ใช้ Gradle คุณสามารถใช้ปลั๊กอิน Gradle ของตัวคอมไพล์ Compose เพื่อให้การตั้งค่า และกำหนดค่า Compose ง่ายขึ้น
ตั้งค่าด้วยแคตตาล็อกเวอร์ชัน Gradle
วิธีการต่อไปนี้จะอธิบายวิธีตั้งค่าปลั๊กอิน Gradle ของ Compose Compiler
- ในไฟล์
libs.versions.tomlให้นำการอ้างอิงใดๆ ไปยัง Compose Compiler ออก - ในส่วนปลั๊กอิน ให้เพิ่มทรัพยากร Dependency ใหม่ต่อไปนี้
[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" }
- ในไฟล์
build.gradle.ktsรูทของโปรเจ็กต์ ให้เพิ่มข้อมูลโค้ดต่อไปนี้ลงในส่วน ปลั๊กอิน
plugins {
// Existing plugins
alias(libs.plugins.compose.compiler) apply false
}
- ในแต่ละโมดูลที่ใช้ Compose ให้ใช้ปลั๊กอินดังนี้
plugins {
// Existing plugins
alias(libs.plugins.compose.compiler)
}
ตอนนี้แอปของคุณควรสร้างและคอมไพล์ได้หากคุณใช้การตั้งค่าเริ่มต้น หากคุณกําหนดค่าตัวเลือกที่กําหนดเองในคอมไพเลอร์ Compose โปรดดูส่วนต่อไปนี้
ตั้งค่าคอมไพเลอร์ Compose โดยไม่มีแคตตาล็อกเวอร์ชัน Gradle
เพิ่มปลั๊กอินต่อไปนี้ลงในไฟล์ build.gradle.kts ที่เชื่อมโยงกับโมดูลที่คุณใช้
Compose
plugins {
id("org.jetbrains.kotlin.plugin.compose") version "2.0.0" // this version matches your Kotlin version
}
นอกจากนี้ คุณอาจต้องเพิ่ม classpath นี้ลงในไฟล์
build.gradle.kts ของโปรเจ็กต์ระดับบนสุดด้วย
buildscript {
dependencies {
classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.0.0")
}
}
ตัวเลือกการกำหนดค่าด้วยปลั๊กอิน Gradle ของคอมไพเลอร์ Compose
หากต้องการกำหนดค่าคอมไพเลอร์ Compose โดยใช้ปลั๊กอิน Gradle ให้เพิ่มบล็อก composeCompiler ลงในไฟล์ build.gradle.kts ของโมดูลที่ระดับบนสุด
android { … }
composeCompiler {
reportsDestination = layout.buildDirectory.dir("compose_compiler")
stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
}
ดูรายการตัวเลือกทั้งหมดที่พร้อมใช้งานได้ในเอกสารประกอบ
ตั้งค่าการอ้างอิง Compose
เพิ่มคำจำกัดความต่อไปนี้ลงในไฟล์ build.gradle ของแอป
Groovy
android {
buildFeatures {
compose true
}
}
Kotlin
android {
buildFeatures {
compose = true
}
}
การตั้งค่าสถานะ compose เป็น true ภายในบล็อก BuildFeatures ของ Android
จะเปิดใช้ฟังก์ชันการทำงานของ Compose ใน Android Studio
สุดท้าย ให้เพิ่ม BOM ของ Compose และทรัพยากร Dependency ของไลบรารี Compose ที่เป็นเซ็ตย่อย ที่คุณต้องการลงในทรัพยากร Dependency จากบล็อกต่อไปนี้
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")
}