Thiết lập trình bổ trợ Compose Compiler Gradle

Đối với người dùng Gradle, bạn có thể sử dụng trình bổ trợ Compose Compiler Gradle để thiết lập và định cấu hình Compose dễ dàng hơn.

Thiết lập bằng danh mục phiên bản Gradle

Các hướng dẫn sau đây trình bày cách thiết lập trình bổ trợ Gradle Compose Compiler:

  1. Trong tệp libs.versions.toml, hãy xoá mọi thông tin tham chiếu đến Trình biên dịch Compose.
  2. Trong mục trình bổ trợ, hãy thêm phần phụ thuộc mới sau đây.
[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" }
  1. Trong tệp build.gradle.kts gốc của dự án, hãy thêm đoạn mã sau vào phần trình bổ trợ.
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler) apply false
}
  1. Trong mỗi mô-đun sử dụng Compose, hãy áp dụng trình bổ trợ như sau.
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler)
}

Ứng dụng của bạn hiện sẽ tạo và biên dịch nếu bạn đang sử dụng chế độ thiết lập mặc định. Nếu bạn đã định cấu hình các lựa chọn tuỳ chỉnh trên trình biên dịch Compose, hãy xem phần sau.

Thiết lập Compose Compiler mà không cần danh mục phiên bản Gradle

Thêm trình bổ trợ sau vào các tệp build.gradle.kts được liên kết với những mô-đun mà bạn dùng Compose:

plugins {
    id("org.jetbrains.kotlin.plugin.compose") version "2.0.0" // this version matches your Kotlin version
}

Bạn cũng có thể cần thêm đường dẫn lớp này vào tệp build.gradle.kts dự án cấp cao nhất:

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.0.0")
    }
}

Các lựa chọn cấu hình với trình bổ trợ Compose Compiler Gradle

Để định cấu hình trình biên dịch Compose bằng trình bổ trợ Gradle, hãy thêm khối composeCompiler vào tệp build.gradle.kts của mô-đun ở cấp cao nhất.

android {  }

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

Để xem danh sách đầy đủ các lựa chọn có sẵn, hãy xem tài liệu.

Thiết lập các phần phụ thuộc Compose

Thêm phần khai báo sau vào tệp build.gradle của ứng dụng:

Groovy

android {
    buildFeatures {
        compose true
    }
}

Kotlin

android {
    buildFeatures {
        compose = true
    }
}

Việc thiết lập cờ compose thành true bên trong khối BuildFeatures của Android sẽ bật chức năng Compose trong Android Studio.

Cuối cùng, hãy thêm Bảng kê khai thành phần của Compose và tập hợp con gồm các phần phụ thuộc của thư viện Compose mà bạn cần vào các phần phụ thuộc trong khối sau:

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")

}