設定 Compose 編譯器 Gradle 外掛程式

如果您使用 Gradle,可以透過 Compose 編譯器 Gradle 外掛程式,更輕鬆地設定及配置 Compose。

透過 Gradle 版本目錄進行設定

下列操作說明大致說明如何設定 Compose 編譯器 Gradle 外掛程式:

  1. libs.versions.toml 檔案中,移除對 Compose 編譯器的所有參照。
  2. 在外掛程式部分中,新增下列依附元件。
[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. 在專案根層級的 build.gradle.kts 檔案中,將下列程式碼片段新增至外掛程式區段。
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.0.0" // 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.0.0")
    }
}

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 依附元件

請在應用程式的 build.gradle 檔案中加入以下定義:

Groovy

android {
    buildFeatures {
        compose true
    }
}

Kotlin

android {
    buildFeatures {
        compose = true
    }
}

在 Android 的 BuildFeatures 區塊中,將 compose 旗標設為 true 即可在 Android Studio 中啟用 Compose 功能

最後,將以下區塊中的 Compose BOM 和所需 Compose 程式庫依附元件子集加入依附元件:

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

}