Jetpack Compose - 设置 Compose 编译器、设置 Compose 依赖项

设置 Compose 编译器

1、具体实现
  1. libs.versions.toml 文件中,添加如下内容
toml 复制代码
[versions]
kotlin = "2.0.0"

[plugins]
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

# 添加 Compose 编译器
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
  1. 在项目级 build.gradle.kts 文件中,添加如下内容,项目级声明,告诉 Gradle 这个插件可能会用,但不激活
kotlin 复制代码
plugins {
    alias(libs.plugins.kotlin.compose) apply false
}
  1. 在模块级 build.gradle.kts 文件中,添加如下内容,模块级启用,在具体的模块真正激活插件
kotlin 复制代码
plugins {
    alias(libs.plugins.kotlin.compose)
}
2、补充学习
  1. 可以直接在模块级启用,项目级声明不是必须的

  2. 项目级声明的真正价值在于,项目级 build.gradle.kts 文件只声明版本,不应用,各个模块级 build.gradle.kts 文件继承版本,直接应用

  3. 版本集中在 libs.versions.toml 文件管理,所有模块版本统一,升级只需改一处

  4. 对于单模块项目,可以直接模块级启用;对于多模块项目,项目级声明更好


设置 Compose 依赖项

1、具体实现
  1. 在模块级 build.gradle.kts 文件中,添加如下内容,以在 Android Studio 中启用 Compose 功能
kotlin 复制代码
android {
    buildFeatures {
        compose = true
    }
}
  1. 设置 Compose 依赖项
kotlin 复制代码
dependencies {

    val composeBom = platform("androidx.compose:compose-bom:2026.04.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.10.0")
    // Optional - Integration with LiveData
    implementation("androidx.compose.runtime:runtime-livedata")
    // Optional - Integration with RxJava
    implementation("androidx.compose.runtime:runtime-rxjava2")

}
2、解读
kotlin 复制代码
val composeBom = platform("androidx.compose:compose-bom:2026.04.01")
implementation(composeBom)
androidTestImplementation(composeBom)
  1. BOM(Bill of Materials,物料清单)统一管理所有 Compose 库的版本,确保兼容性,后续依赖不需要写版本号,由 BOM 自动决定

  2. 主代码模块使用 BOM,后续所有 Compose 依赖无需写版本号

  3. 测试模块也使用同一个 BOM,保证测试代码与主代码版本一致

kotlin 复制代码
// 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")
依赖项 说明
material3 Material Design 3 组件(按钮、卡片等),大多数项目选这个
foundation 无 Material 风格的基础组件(例如,BasicTextField)
ui 最底层,布局、输入、测量,需要自己构建 UI
  • 一般只选 material3,它已包含 foundation 和 ui
kotlin 复制代码
// 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")
依赖项 说明
ui-tooling-preview @Preview 注解支持
ui-tooling 交互式预览、检查布局
ui-test-junit4 UI 测试框架
ui-test-manifest 测试用的 AndroidManifest
kotlin 复制代码
// 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")
依赖项 说明
adaptive 窗口大小适配工具
activity-compose 将 Compose 与 ComponentActivity 深度集成
lifecycle-viewmodel-compose ViewModel 集成
runtime-livedata LiveData 集成
runtime-rxjava2 RxJava2 集成
相关推荐
前端 贾公子21 分钟前
第09章:上下文与记忆 (2)
java·服务器·前端
2601_9622035123 分钟前
Java进阶07集合(续)
java·开发语言
郑州光合科技余经理44 分钟前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
roman_日积跬步-终至千里1 小时前
【数据治理(6)】DataOps 不是调度工具,而是让数据产品长期可信的运行机制
java·服务器·网络
落魄大学生之流水线上谋生计1 小时前
LangGraph 基本用法指南
java·windows·python·langchain
2601_962128412 小时前
SpringBoot篇(缓存层)
java·spring boot·缓存
Dovis(誓平步青云)2 小时前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
2501_937860942 小时前
Java多线程初阶(下)—— synchronized、volatile、wait/notify与经典并发工具
java·开发语言·jvm
牛油果子哥q2 小时前
C++内存池与对象池精讲:内存碎片、自定义分配器、对象池实现、STL allocator原理、工程落地与性能对比
java·开发语言·c++
CodeStats3 小时前
《源纹天书》第三百六十三章至第三百六十四章
java·开发语言·源纹天书