原文地址:Jetpack Compose学习(10)——使用Compose物料清单BOM,更好管理依赖版本 - Stars-One的杂货小窝

本期讲解下关于Android推出的BOM来简化我们添加compose依赖过于繁杂的问题

本系列以往文章请查看此分类链接Jetpack compose学习

介绍

BOM为Bill of Material的缩写

原本是制造业中的一个概念,比如组装一个手机,BoM包括屏幕、手机壳、芯片、主板、电池等,按照既定的物料清单采购好配件,工厂进行组装生产

对于我们开发者来说, 有什么作用的?

举个例子,像compose的一系列依赖,版本众多,更新且又频繁,且又相互有所依赖,对于我们开发来说,理清这些层层次次关系足以头大,然后还有个致命问题,我们几个库使用不同版本,可能还会导致编译直接报错,出现依赖版本等冲突问题

鉴于上述原因,Android官方就是提供了一个BOM的概念,也就是今天的正文。

BoM 是否会自动将所有 Compose 库添加到我的应用中?

不会。要在您的应用中实际添加和使用 Compose 库,您必须在模块(应用级)Gradle 文件(通常是 app/build.gradle)中将每个库声明为单独的依赖项行。

使用 BoM 可确保应用中的任何 Compose 库版本兼容,但 BoM 实际上并不会将这些 Compose 库添加到您的应用中。

为什么建议使用 BoM 管理 Compose 库版本?

今后,Compose 库将单独进行版本控制,这意味着版本号将开始按照自己的节奏递增。每个库的最新稳定版本已经过测试,并保证能够很好地协同工作。不过,找到每个库的最新稳定版本可能比较困难,而 BoM 会帮助您自动使用这些最新版本

使用

使用的话也很简单,如下面例子:

dependencies {

    def composeBom = platform('androidx.compose:compose-bom:2022.12.00')
implementation composeBom
androidTestImplementation composeBom implementation 'androidx.compose.ui:ui'
implementation 'androidx.activity:activity-compose'
// Material Design 3
implementation 'androidx.compose.material3:material3'
}

需要注意的是,我们引入之后,后续的compose相关的库,都不需要写版本号了,由BOM默认指定版本

当然,如果你想指定版本,也是可以的,会优先以你指定的版本为准

库组 版本 (2022.10.00) 版本 (2022.11.00) 版本 (2022.12.00) 版本 (2023.01.00)
androidx.compose.animation:animation 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.animation:animation-core 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.animation:animation-graphics 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.foundation:foundation 1.3.0 1.3.1 1.3.1 1.3.1
androidx.compose.foundation:foundation-layout 1.3.0 1.3.1 1.3.1 1.3.1
androidx.compose.material:material 1.3.0 1.3.1 1.3.1 1.3.1
androidx.compose.material:material-icons-core 1.3.0 1.3.1 1.3.1 1.3.1
androidx.compose.material:material-icons-extended 1.3.0 1.3.1 1.3.1 1.3.1
androidx.compose.material:material-ripple 1.3.0 1.3.1 1.3.1 1.3.1
androidx.compose.material3:material3 1.0.0 1.0.1 1.0.1 1.0.1
androidx.compose.material3:material3-window-size-class 1.0.0 1.0.1 1.0.1 1.0.1
androidx.compose.runtime:runtime 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.runtime:runtime-livedata 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.runtime:runtime-rxjava2 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.runtime:runtime-rxjava3 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.runtime:runtime-saveable 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-geometry 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-graphics 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-test 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-test-junit4 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-test-manifest 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-text 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-text-google-fonts 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-tooling 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-tooling-data 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-tooling-preview 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-unit 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-util 1.3.0 1.3.1 1.3.2 1.3.3
androidx.compose.ui:ui-viewbinding 1.3.0 1.3.1 1.3.2 1.3.3

最新的版本信息可以通过官方的链接进行查看BoM 到库的版本映射 |  Android Developers

除此之外,还需要注意与kotiln的版本对应关系,BOM的各版本兼容的最低Kotlin版本可是有所不同的!详情见下文

compose版本与Kotlin的兼容性

参考

最新文章

  1. SQL优化技术分析-3:SQL语句索引的利用
  2. PHP中面向对象的关键字
  3. Leetcode 35 Search Insert Position 二分查找(二分下标)
  4. 小白科普之JavaScript的函数
  5. iOS的runtime(转)
  6. 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq(线段树)
  7. Dire Wolf ---hdu5115(区间dp)
  8. iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
  9. MTK MOTA升级步骤
  10. 移动 云MAS 发短信 .net HTTP 请求
  11. 剑指Offer-按之字形顺序打印二叉树
  12. Dynamics CRM 安装Microsoft Dynamics CRM Reporting Extensions
  13. 20170831工作日记--自定义View学习
  14. S3C2440时钟体系结构
  15. 面向对象编程思想(前传)--你必须知道的javascript(转载)
  16. Memcached Windows 测试
  17. 自动化测试的组成部分:SEARCH
  18. Quartz_2_简单编程式任务调度使用(CronTrigger)
  19. vs2015发布网站至azure web应用服务
  20. (转)使用Cobbler批量部署Linux和Windows:Cobbler服务端部署(一)

热门文章

  1. 你听说过OTA吗?
  2. [C# 中的序列化与反序列化](.NET 源码学习)
  3. HDC.Cloud Day | 全国首场上海站告捷,聚开发者力量造梦、探梦、筑梦
  4. PEP8语法规范解释说明
  5. c++题目:数迷
  6. JavaEE Day13 Tomcat和Servlet
  7. 【Java SE】Day03流程控制语句
  8. 【每日一题】【判断栈是否为空的方法】2022年1月9日-NC76 用两个栈实现队列的出队入队【入队简单】
  9. 【CDH数仓】Day02:业务数仓搭建、Kerberos安全认证+Sentry权限管理、集群性能测试及资源管理、邮件报警、数据备份、节点添加删除、CDH的卸载
  10. DC-9靶场练习