如果是 Gradle,請使用 Compose 編譯器 Gradle 外掛程式設定及配置 Compose。
透過 Gradle 版本目錄進行設定
設定 Compose 編譯器 Gradle 外掛程式:
- 在
libs.versions.toml檔案中,移除對 Compose 編譯器的所有參照。 在
versions和plugins區段中,新增依附元件:[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" }在專案的根層級
build.gradle.kts檔案中,將以下內容新增至plugins區段。plugins { // Existing plugins alias(libs.plugins.compose.compiler) apply false }在每個使用 Compose 的模組中套用外掛程式:
plugins { // Existing plugins alias(libs.plugins.compose.compiler) }
如果專案使用預設設定,現在應該會建構及編譯。如果已在 Compose 編譯器上設定自訂選項,請參閱下一節。
設定不使用 Gradle 版本目錄的 Compose 編譯器
將外掛程式新增至與使用 Compose 的模組相關聯的 build.gradle.kts 檔案:
plugins {
id("org.jetbrains.kotlin.plugin.compose") version "2.3.21" // this version matches your Kotlin version
}
在頂層專案 build.gradle.kts 檔案中新增類別路徑:
buildscript {
dependencies {
classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.3.21")
}
}
Compose 編譯器 Gradle 外掛程式的設定選項
如要使用 Gradle 外掛程式設定 Compose 編譯器,請在頂層的模組 build.gradle.kts 檔案中加入 composeCompiler 區塊:
android { … }
composeCompiler {
reportsDestination = layout.buildDirectory.dir("compose_compiler")
stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
}
如需完整清單,請參閱說明文件。
設定 Compose 依附元件
一律使用最新版 Compose BOM:2026.06.00。
在 Android 的 BuildFeatures 區塊中,將 compose 旗標設為 true,即可在 Android Studio 中啟用 Compose 功能。
請在應用程式的 build.gradle 檔案中加入以下定義:
Groovy
android {
buildFeatures {
compose true
}
}
Kotlin
android {
buildFeatures {
compose = true
}
}
新增 Compose BOM 和 Compose 程式庫依附元件子集:
Groovy
dependencies {
def composeBom = platform('androidx.compose:compose-bom:2026.06.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.06.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。
如要查看不同 API 級別支援的 AGP 版本,請參閱「Android Gradle 外掛程式 API 級別支援」說明文件。