Compose框架分层

本文基础内容参考官方文档 Jetpack Compose架构层,架构图如下图,这里扩充给出具体每个层包含的库和它们的依赖关系。

架构图

每一层均基于较低的层逐级构建,并通过组合功能来创建更高级别的组件。每一层都是基于较低层的公共 API 构建的,用于验证模块边界,还支持您根据需要替换任何层。让我们自下而上地分析这些层。

运行时(runtime)

此模块提供了 Compose 运行时的基本组件,例如 remember、mutableStateOf、@Composable 注释和 SideEffect。如果您只需要 Compose 的树管理功能,而不需要其界面,则可以考虑直接基于此层进行构建。

runtime-android

1.5.0版本两个库是一样的

groovy 复制代码
implementation("androidx.compose.runtime:runtime:1.5.0")
implementation("androidx.compose.runtime:runtime-android:1.5.0")

runtime-livedata

依赖 runtime-android

groovy 复制代码
implementation("androidx.compose.runtime:runtime-livedata:1.5.0")

runtime-saveable

依赖 runtime-android, 1.5.0版本两个引用是一样的

scss 复制代码
implementation("androidx.compose.runtime:runtime-saveable:1.5.0")
implementation("androidx.compose.runtime:runtime-saveable-android:1.5.0")

runtime-rxjava2

依赖 runtime-android

scss 复制代码
implementation("androidx.compose.runtime:runtime-rxjava2:1.5.0")

runtime-rxjava3

依赖 runtime-android

scss 复制代码
implementation("androidx.compose.runtime:runtime-rxjava3:1.5.0")

其他组件

  • runtime-dispatch
  • runtime-tracing:调试相关的独立组件,不依赖runtime,与 Perfetto 关联使用;
  • runtime-saved-instance-state:依赖 runtime-dispatch

界面层(ui)

界面层由多个模块(ui-text、ui-graphics 和 ui-tooling 等)组成。这些模块实现了界面工具包的基本组件,例如 LayoutNode、Modifier、输入处理程序、自定义布局和绘图。如果您只需要用到界面工具包的基本概念,则可以考虑基于此层进行构建。

ui-geometry-android

scss 复制代码
implementation("androidx.compose.ui:ui-geometry:1.5.0")
implementation("androidx.compose.ui:ui-geometry-android:1.5.0")

ui-unit-android

依赖 ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui-unit:1.5.0")
implementation("androidx.compose.ui:ui-unit-android:1.5.0")

ui-graphics-android

依赖 ui-geometry 和 ui-unit

scss 复制代码
implementation("androidx.compose.ui:ui-graphics:1.5.0")
implementation("androidx.compose.ui:ui-graphics-android:1.5.0")

ui-text-android

依赖ui-graphics、ui-unit、ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui-text:1.5.0")
implementation("androidx.compose.ui:ui-text-android:1.5.0")

ui-tooling-preview-android

依赖 runtime

scss 复制代码
implementation("androidx.compose.ui:ui-tooling-preview:1.5.0")
implementation("androidx.compose.ui:ui-tooling-preview-android:1.5.0")

ui-tooling-data-android

依赖 runtime-saveable、runtime、ui、ui-text、ui-graphics、ui-unit、ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui-tooling-data:1.5.0")
implementation("androidx.compose.ui:ui-tooling-data-android:1.5.0")

ui-android

依赖 runtime-saveable、runtime、ui-text、ui-graphics、ui-unit、ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui:1.5.0")
implementation("androidx.compose.ui:ui-android:1.5.0")

ui-tooling-android

依赖 runtime-saveable、runtime、ui、ui-tooling-preview、ui-tooling-data、ui-text、ui-graphics、ui-unit、ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui-tooling:1.5.0")
implementation("androidx.compose.ui:ui-tooling-android:1.5.0")

ui-util

scss 复制代码
implementation("androidx.compose.ui:ui-util:1.5.0")
implementation("androidx.compose.ui:ui-util-android:1.5.0")

ui-viewbinding

scss 复制代码
implementation("androidx.compose.ui:ui-viewbinding:1.5.0")

ui-text-google-fonts

scss 复制代码
implementation("androidx.compose.ui:ui-text-google-fonts:1.5.0")

ui-test

ui-test

依赖 runtime-saveable、runtime、ui、ui-text、ui-graphics、ui-unit、ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui-test:1.5.0")
implementation("androidx.compose.ui:ui-test-android:1.5.0")
ui-test-junit4

依赖 runtime-saveable、runtime、ui-test、ui、ui-text、ui-graphics、ui-unit、ui-geometry

scss 复制代码
implementation("androidx.compose.ui:ui-test-junit4:1.5.0")

基础(Foundation)

此模块为 Compose 界面提供了与设计系统无关的构建块,例如 Row 和 Column、LazyColumn、特定手势的识别等。您可以考虑基于基础层构建自己的设计系统。

Material

此模块为 Compose 界面提供了 Material Design 系统的实现,同时提供了一个主题系统以及若干样式化组件、涟漪效果指示元素和图标。在您的应用中使用 Material Design 时,不妨基于此层进行构建。

其他库对Compose的支持

  • 官方文档中 Compose和其他库
  • Maven Repository搜索 Compose结果
    • androidx.activity:acitivity-compose
    • androidx.lifecycle:lifecycle-viewmodel-compose
    • androidx.navigation:navigation-compose
    • io.coil-kt:coil-compose
    • androidx.constraintlayout:constraintlayout-compose
    • androidx.lifecycle-runtime-compose
    • androidx.hilt:hilt-navigation-compose
    • io.insert-koin:koin-androidx-compose
    • androidx.paging:paging-compose
    • com.airbnb.android:lottie-compose
相关推荐
shenshizhong1 天前
Compose + Mvi 架构的玩android 项目,请尝鲜
android·架构·android jetpack
alexhilton5 天前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
ljt27249606618 天前
Compose笔记(六十一)--SelectionContainer
android·笔记·android jetpack
QING6188 天前
Jetpack Compose 中的 ViewModel 作用域管理 —— 新手指南
android·kotlin·android jetpack
惟恋惜9 天前
Jetpack Compose 的状态使用之“界面状态”
android·android jetpack
喜熊的Btm9 天前
探索 Kotlin 的不可变集合库
kotlin·android jetpack
惟恋惜9 天前
Jetpack Compose 界面元素状态(UI Element State)详解
android·ui·android jetpack
惟恋惜9 天前
Jetpack Compose 多页面架构实战:从 Splash 到底部导航,每个 Tab 拥有独立 ViewModel
android·ui·架构·android jetpack
alexhilton11 天前
Jetpack Compose 2025年12月版本新增功能
android·kotlin·android jetpack