设置 Compose Compiler Gradle 插件
对于 Gradle 用户,您可以使用 Compose 编译器 Gradle 插件来更轻松地设置和配置 Compose。
使用 Gradle 版本目录进行设置
以下说明概述了如何设置 Compose 编译器 Gradle 插件:
- 在
libs.versions.toml文件中,移除对 Compose 编译器的所有引用。 - 在“plugins”部分中,添加以下新依赖项。
[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" }
- 在项目的根
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.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 编译器,请将 composeCompiler 块添加到模块的 build.gradle.kts 文件中的顶层。
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")
}