本期讲解下关于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的兼容性
评论区