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

Đối với Gradle, hãy sử dụng trình bổ trợ Compose Compiler Gradle để thiết lập và định cấu hình Compose.

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

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

  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 các mục versionsplugins, hãy thêm phần phụ thuộc mới:
[versions]
kotlin = "2.3.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" }
  1. Trong tệp build.gradle.kts gốc của dự án, hãy thêm nội dung sau vào phần plugins.
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ợ:
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler)
}

Dự án hiện sẽ tạo và biên dịch nếu đ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 làm theo phần tiếp theo.

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ợ vào các tệp build.gradle.kts được liên kết với những mô-đun sử dụng Compose:

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

Thêm đường dẫn lớp vào tệp build.gradle.kts cấp cao nhất của dự án:

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

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

Luôn sử dụng phiên bản BOM Compose mới nhất: 2026.03.00.

Đặt cờ compose thành true bên trong BuildFeatures của Android để bật chức năng Compose trong Android Studio.

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
    }
}

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:

Groovy

dependencies {

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

}