設定 Compose 編譯器 Gradle 外掛程式

如果是 Gradle,請使用 Compose 編譯器 Gradle 外掛程式設定及設定 Compose。

透過 Gradle 版本目錄進行設定

設定 Compose 編譯器 Gradle 外掛程式:

  1. libs.versions.toml 檔案中,移除對 Compose 編譯器的所有參照。
  2. versionsplugins 區段中,新增依附元件:
[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. 在專案的根層級 build.gradle.kts 檔案中,將以下內容新增至 plugins 區段。
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler) apply false
}
  1. 在每個使用 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.10" // 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.10")
    }
}

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

}