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
相关推荐
Lei活在当下19 小时前
【业务场景架构实战】4. 支付状态分层流转的设计和实现
架构·android jetpack·响应式设计
天花板之恋1 天前
Compose之图片加载显示
android jetpack
消失的旧时光-19432 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Tans52 天前
Androidx Fragment 源码阅读笔记(下)
android jetpack·源码阅读
Lei活在当下3 天前
【业务场景架构实战】2. 对聚合支付 SDK 的封装
架构·android jetpack
Tans55 天前
Androidx Fragment 源码阅读笔记(上)
android jetpack·源码阅读
alexhilton7 天前
runBlocking实践:哪里该使用,哪里不该用
android·kotlin·android jetpack
Tans59 天前
Androidx Lifecycle 源码阅读笔记
android·android jetpack·源码阅读
ljt272496066110 天前
Compose笔记(四十九)--SwipeToDismiss
android·笔记·android jetpack
4z3312 天前
Jetpack Compose重组优化:机制剖析与性能提升策略
性能优化·android jetpack