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
相关推荐
雨白2 小时前
Hilt 入门指南:从 DI 原理到核心用法
android·android jetpack
我命由我123452 小时前
Android 开发 - Android JNI 开发关键要点
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
alexhilton1 天前
在Jetpack Compose中创建CRT屏幕效果
android·kotlin·android jetpack
峰哥的Android进阶之路1 天前
viewModel机制及原理总结
android jetpack
我命由我123452 天前
Android WebView - loadUrl 方法的长度限制
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
Coffeeee2 天前
面试被问到Compose的副作用不会,只怪我没好好学
android·kotlin·android jetpack
Frank_HarmonyOS5 天前
Android APP 的压力测试与优化
android jetpack
QING6186 天前
Jetpack Compose 条件布局与 Layout 内在测量详解
android·kotlin·android jetpack
Lei活在当下7 天前
【现代 Android APP 架构】09. 聊一聊依赖注入在 Android 开发中的应用
java·架构·android jetpack
bqliang7 天前
Jetpack Navigation 3:领航未来
android·android studio·android jetpack