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
相关推荐
ljt27249606611 天前
Compose笔记(二十六)--DatePicker
笔记·android jetpack
_一条咸鱼_1 天前
Android Runtime类卸载条件与资源回收策略(29)
android·面试·android jetpack
harry235day1 天前
Compose 带动画的待办清单列表页
android·android jetpack
_一条咸鱼_2 天前
Android Runtime链接(Linking)阶段准备工作(27)
android·面试·android jetpack
大耳猫6 天前
Android 中的 DataBinding 详解
android·android jetpack·databinding
_一条咸鱼_8 天前
Android Runtime内存管理子系统启动流程原理(13)
android·面试·android jetpack
_一条咸鱼_8 天前
Android Runtime JNI环境构建与注册过程原理(15)
android·面试·android jetpack
alexhilton8 天前
Jetpack Compose 中ViewModel的最佳实践
android·kotlin·android jetpack
兰琛9 天前
Compose仿微信底部导航栏NavigationBar :底部导航控制滑动并移动
android·android jetpack
_一条咸鱼_9 天前
Android Runtime解释器与编译器初始化机制原理(14)
android·面试·android jetpack