ตั้งค่าปลั๊กอิน Gradle ของตัวคอมไพล์ Compose

สำหรับ Gradle ให้ใช้ปลั๊กอิน Compose Compiler Gradle เพื่อตั้งค่าและกำหนดค่า Compose

ตั้งค่าด้วยแคตตาล็อกเวอร์ชัน Gradle

ตั้งค่าปลั๊กอิน Compose Compiler Gradle ดังนี้

  1. ในไฟล์ libs.versions.toml ให้นำการอ้างอิงถึง Compose Compiler ออก
  2. ในส่วน versions และ plugins ให้เพิ่มทรัพยากร Dependency ใหม่

    [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" }
    
  3. ในไฟล์ build.gradle.kts ระดับรากของโปรเจ็กต์ ให้เพิ่มโค้ดต่อไปนี้ลงในส่วน plugins

    plugins {
    // Existing plugins
    alias(libs.plugins.compose.compiler) apply false
    }
    
  4. ใช้ปลั๊กอินในแต่ละโมดูลที่ใช้ Compose

    plugins {
    // Existing plugins
    alias(libs.plugins.compose.compiler)
    }
    

ตอนนี้โปรเจ็กต์ควรจะบิลด์และคอมไพล์ได้หากใช้การตั้งค่าเริ่มต้น หากมีการกำหนดค่าตัวเลือกที่กำหนดเองใน Compose Compiler ให้ทำตามส่วนถัดไป

ตั้งค่า Compose Compiler โดยไม่ใช้แคตตาล็อกเวอร์ชัน Gradle

เพิ่มปลั๊กอินลงในไฟล์ build.gradle.kts ที่เชื่อมโยงกับโมดูลที่ใช้ Compose

plugins {
    id("org.jetbrains.kotlin.plugin.compose") version "2.3.21" // 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.3.21")
    }
}

ตัวเลือกการกำหนดค่าด้วยปลั๊กอิน Compose Compiler Gradle

หากต้องการกำหนดค่า Compose Compiler โดยใช้ปลั๊กอิน Gradle ให้เพิ่มบล็อก composeCompiler ลงในไฟล์ build.gradle.kts ของโมดูลที่ระดับบนสุด

android {  }

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

ดูรายการตัวเลือกทั้งหมดที่มีได้ในเอกสารประกอบ

ตั้งค่าทรัพยากร Dependency ของ Compose

ใช้ Compose BOM เวอร์ชันล่าสุด 2026.08.00 เสมอ

ตั้งค่าแฟล็ก compose เป็น true ใน BuildFeatures ของ Android เพื่อ เปิดใช้ ฟังก์ชันการทำงานของ Compose ใน Android Studio

เพิ่มคำจำกัดความต่อไปนี้ลงในไฟล์ build.gradle ของแอป

ดึงดูด

android {
    buildFeatures {
        compose true
    }
}

Kotlin

android {
    buildFeatures {
        compose = true
    }
}

เพิ่ม Compose BOM และทรัพยากร Dependency ของไลบรารี Compose ซึ่งเป็นส่วนย่อย

ดึงดูด

dependencies {

    def composeBom = platform('androidx.compose:compose-bom:2026.08.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.08.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")

}

compileSdk และความเข้ากันได้ของปลั๊กอิน Android Gradle

การเผยแพร่ไลบรารี Compose จะใช้ compileSdk เวอร์ชันล่าสุดอย่างต่อเนื่องเพื่อให้เข้าถึงฟีเจอร์ล่าสุดของ Android ได้ compileSdk เวอร์ชันใหม่กว่าต้องใช้ปลั๊กอิน Android Gradle เวอร์ชันใหม่กว่า ดังนั้นการใช้ Compose เวอร์ชันใหม่จึงกำหนดให้โปรเจ็กต์ต้องใช้ปลั๊กอิน Android Gradle เวอร์ชันใหม่ด้วย เราขอแนะนำให้คุณอัปเดต compileSdk ของโปรเจ็กต์ให้เป็นเวอร์ชันล่าสุดที่เผยแพร่ compileSdk ไม่เกี่ยวข้องกับ targetSdk

ตัวอย่างเช่น เริ่มตั้งแต่ Compose 1.12.0 โปรเจ็กต์จะต้องใช้ compileSdk 37 และปลั๊กอิน Android Gradle (AGP) 9

หากต้องการตรวจสอบว่า AGP เวอร์ชันใดที่รองรับ API ระดับต่างๆ โปรดดูเอกสารประกอบการรองรับ API ระดับของปลั๊กอิน Android Gradle