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 cho Gradle:

  1. Trong tệp libs.versions.toml, hãy xoá mọi tệp tham chiếu đến Trình biên dịch Compose.
  2. Trong phần versions và plugins, hãy thêm phần phụ thuộc mới:

    [versions]
    kotlin = "2.4.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" }
    
  3. Trong tệp build.gradle.kts gốc của dự án, hãy thêm đoạn mã sau vào phần plugins.

    plugins {
    // Existing plugins
    alias(libs.plugins.compose.compiler) apply false
    }
    
  4. 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 đã định cấu hình các tuỳ 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 Trình biên dịch Compose 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 liên kết với các mô-đun có sử dụng Compose:

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

Thêm classpath vào tệp build.gradle.kts của dự án cấp cao nhất:

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

Các lựa chọn cấu hình với Trình bổ trợ Compose Compiler cho 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")
    stabilityConfigurationFiles.add(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 phần phụ thuộc của Compose

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

Đặ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.11.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.13.0'
    // Optional - Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.11.1'
    // 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.11.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.13.0")
    // Optional - Integration with ViewModels
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.11.1")
    // Optional - Integration with LiveData
    implementation("androidx.compose.runtime:runtime-livedata")
    // Optional - Integration with RxJava
    implementation("androidx.compose.runtime:runtime-rxjava2")

}

Khả năng tương thích của compileSdk và trình bổ trợ Android cho Gradle

Các bản phát hành thư viện Compose liên tục áp dụng các phiên bản compileSdk mới nhất để cung cấp quyền truy cập vào các tính năng mới nhất của Android. Các phiên bản compileSdk mới hơn yêu cầu các phiên bản Trình bổ trợ Android cho Gradle mới hơn, vì vậy, việc áp dụng các bản phát hành Compose mới cũng yêu cầu các dự án phải áp dụng các phiên bản mới của Trình bổ trợ Android cho Gradle. Bạn nên cập nhật compileSdk của dự án lên phiên bản mới nhất. compileSdk không liên quan đến targetSdk.

Ví dụ: kể từ Compose 1.12.0, các dự án bắt buộc phải sử dụng compileSdk 37 và Trình bổ trợ Android cho Gradle (AGP) 9.

Để kiểm tra xem phiên bản AGP nào được hỗ trợ cho các cấp độ API khác nhau, hãy xem tài liệu về Hỗ trợ cấp độ API của trình bổ trợ Android cho Gradle.