目录
-
什么是version catalogs?
version catalogs是AGP7.0之后推出的一种依赖版本管理的方式。
这时会有小伙伴问:之前不是也有其他方式的版本管理方式吗,也挺好用啊,为什么要使用这个?
ok,带着这个问题咱们对之前常用的依赖管理方式做一个比较
-
依赖管理方式对比
ext | version catalogs | |
---|---|---|
声明域 | *.gradle | *.toml / stting.gradle |
是否可修改 | 是 | 否 |
写法 | 花里胡哨 | 固定写法 xxx.xxx.xxx |
校验 | 无 | 同步时校验 |
ext:
就是在各个.gradle文件当中都可以去声明,这种声明方式会零散,而且定义变量时如果后面定义了一样的变量,就会覆盖掉原有的变量(团队开发时容易出现该问题),写法也比较随意,每个开发人员的写法风格可能都不一样,会造成比依赖管理杂乱的情况,没有编译时或同步时校验处理
version catalogs:
这种方式的声明都是在固定的文件 *.toml 或stting.gradle当中去编写,并且不会出现ext当中变量名重复导致后面声明的变量替换前面定义变量的情况,并且在同步时会做校验,有利于团队的开发,不容易出现各个模块下依赖版本冲突问题
-
具体用法
版本管理类型 | 类型描述 | 定义举例 | 用法举例 app/build.gradle |
library | arr、jar版本管理 | library( 'core-ktx', 'androidx.core', 'core-ktx') .version('1.7.0') |
api 分组名.core.ktx |
bundle | 依赖聚合管理 | bundle('androidx', ['core-ktx', 'constraintlayout', 'appcompat'] ) |
api 分组名.androidx |
version | 版本号常量管理 | version('compileSdk','33') |
api 分组名.versions .compileSdk .get() .toInteger() |
plugin | gradle插件版本管理 | plugin('hilt-android', 'com.google.dagger.hilt.android' ).version('2.41') |
分组名.plugins.hilt.android |
在stting.gradle示例:
pluginManagement { ... } //1.启用Version Catalogs enableFeaturePreview('VERSION_CATALOGS') dependencyResolutionManagement { ... //2.在这里添加versionCatalogs versionCatalogs{ //3.创建一个分组名称 这里起名androidxLibs create('androidxLibs'){ //4.依赖管理分组定义 //如:api'androidx.core:core-ktx:1.7.0' //参数:(别名(自己看着取),group,artifact,version) library('core-ktx','androidx.core','core-ktx').version('1.7.0') library('appcompat','androidx.appcompat','appcompat').version('1.6.1') //5.依赖聚合管理 即:将多个依赖进行合并,引用时只需要调用聚合管理分组即可 //参数:(引用时的别名,[需要聚合的别名,需要聚合的别名]) bundle('androidx',['core-ktx','appcompat']) } //可以创建多个分组 create('googleLibs'){ library('material','com.google.android.material','material').version('1.9.0') } /** * 6.版本号常量管理 * 在app/build.gradle中 * compileSdk buildsdk.versions.compileSdk.get().toInteger() */ create('buildsdk'){ version('compileSdk','33') version('minSdk','24') version('targetSdk','33') } /* 7.插件管理 * 只需在app/build.gradle中alias(pluginLibs.plugins.hilt.android)即可 * 对于没有发布到gradlePluginPortal的插件,暂时沿用老的模式,即buildScript { dependencies{ classpath 'xxx.xxx.xxx:1.0.0'}} */ create('pluginLibs'){ plugin('application','com.android.application').version('7.4.1') } //8.引用文件(在根目录当中创建libs.version.toml文件,示例代码在下面) //直接将四种类型的管理放到文件当中 create("libs"){ from(files("libs.version.toml")) } } }
在根目录创建libs.version.toml
ps:写法基本上都是固定的
示例:
#版本管理 [versions] compileSdk = '33' minSdk = '24' targetSdk = '33' #插件管理 [plugins] android-application = { id = "com.android.application", version = "7.4.1" } android-library = { id = "com.android.library", version = "7.4.1" } #依赖管理 [libraries] core-ktx = { module = "androidx.core:core-ktx", version = "1.7.0" } appcompat = { module = "androidx.appcompat:appcompat", version = "1.6.1" } constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version = "2.1.4" } #依赖聚合 [bundles] androidx = ["core-ktx", "appcompat", "constraintlayout"]
app/bundile.gradles示例:
plugins { //1.引用插件示例 alias(pluginLibs.plugins.application) } android { namespace 'com.techme.jetpack_android_online' //2.版本管理引用示例: compileSdk buildsdk.versions.compileSdk.get().toInteger() defaultConfig { applicationId "com.techme.jetpack_android_online" minSdk buildsdk.versions.minSdk.get().toInteger() targetSdk buildsdk.versions.targetSdk.get().toInteger() versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } dependencies { //3.依赖管理引用示例 implementation googleLibs.material //4.聚合依赖管理示例 implementation androidxLibs.bundles.androidx //5.引用libs.version.toml文件示例 implementation libs.constraintlayout }
-
注意事项
经过测试,如果使用*.toml文件的形式引时依赖时和其他的不太一样
对比 →
ps:libs即是分组名
依赖引用:
libs.constraintlayout
即:分组名.别名其他(聚合,插件,版本):
聚合:分组名.bundles.别名
插件:分组名.plugins.别名
版本:分组名.versions.别名
-
备注
此篇文章基本都是大白话讲解,也可以说是个人学习笔记,没有这么多专业术语,如有不对的地方欢迎大家指正