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
相关推荐
alexhilton2 天前
Kotlin互斥锁(Mutex):协程的线程安全守护神
android·kotlin·android jetpack
是六一啊i3 天前
Compose 在Row、Column上使用focusRestorer修饰符失效原因
android jetpack
用户060905255225 天前
Compose 主题 MaterialTheme
android jetpack
用户060905255225 天前
Compose 简介和基础使用
android jetpack
用户060905255225 天前
Compose 重组优化
android jetpack
行墨5 天前
Jetpack Compose 深入浅出(一)——预览 @Preview
android jetpack
alexhilton6 天前
突破速度障碍:非阻塞启动画面如何将Android 应用启动时间缩短90%
android·kotlin·android jetpack
Pika7 天前
深入浅出 Compose 测量机制
android·android jetpack·composer
fundroid9 天前
掌握 Compose 性能优化三步法
android·android jetpack