这么多年的安卓开发中,让人最头疼的莫过于多个项目之间的三方库的版本统一,用过多种多样的版本统一方式,从最开始的ext
,到buildSrc
,再到自定义插件,无论是哪种方式都没有完全解决这个疼点。 现在在gradle 7.0以上的版本中,gralde官方添加了一个新的特性Catalog
,用于统一不同项目之间的版本依赖库。
官方文档 Sharing dependency versions between projects (gradle.org)
参考:【Gradle7.0】依赖统一管理的全新方式,了解一下~ - 掘金 (juejin.cn)
一、配置CataLog 1.1、创建依赖文件 在根目录下创建libs.versions.toml
文件,里面配置依赖
[versions] groovy = "3.0.5" checkstyle = "8.37" compilesdk = "30" targetsdk = "30" [libraries] retrofit = "com.squareup.retrofit2:retrofit:2.9.0" groovy-core = { module = "org.codehaus.groovy:groovy" , version.ref = "groovy" } groovy-json = { module = "org.codehaus.groovy:groovy-json" , version.ref = "groovy" } groovy-nio = { module = "org.codehaus.groovy:groovy-nio" , version.ref = "groovy" } commons-lang3 = { group = "org.apache.commons" , name = "commons-lang3" , version = { strictly = "[3.8, 4.0[" , prefer="3.9" } } [bundles] groovy = ["groovy-core" , "groovy-json" , "groovy-nio" ]
1.2、配置CataLog 在setting.gradle中打开Catalog
支持
pluginManagement { repositories { gradlePluginPortal() mavenCentral() google() mavenLocal() } } rootProject.name = 'VersionManager' include('plugin' ) enableFeaturePreview('VERSION_CATALOGS' ) dependencyResolutionManagement { repositories { gradlePluginPortal() mavenCentral() google() mavenLocal() } versionCatalogs { libs { from(files("libs.versions.toml" )) } }}
1.3、推送到maven中 1、插件中添加下面两个插件
plugins { id 'groovy' id 'version-catalog' id 'maven-publish' }
2、配置catalog文件
catalog { versionCatalog { from files('../libs.versions.toml' ) } }
3、配置maven
publishing { publications { maven(MavenPublication) { groupId = 'com.lyy.plugin' artifactId = 'catalog' version = '0.0.1' from components.versionCatalog } }}
二、使用 1、在setting.gradle中配置我们的插件
enableFeaturePreview('VERSION_CATALOGS' ) dependencyResolutionManagement { repositories { google() mavenCentral() mavenLocal() } versionCatalogs { libs { from("com.lyy.plugin:0.0.1" ) version("groovy" , "3.0.6" ) } } }
2、在根目录的build.gradle
中配置下面插件 由于目前的gradle版本还不支持versionCatalogs自动补全,需要手动引入本地依赖进行补全Code completion for Gradle Version Catalogs in groovy build files : IDEA-279603 (jetbrains.com)
buildscript { dependencies { classpath files(libs.class .superclass.protectionDomain.codeSource.location) } }
3、在dependencies中引入依赖
dependencies { ... implementation libs.retrofit }
三、依赖包bundles
因为在不同的项目中经常系统地一起使用某些依赖项,所以Catalog
提供了bundle
(依赖包)的概念。依赖包基本上是几个依赖项打包的别名。
[versions] compose = "2.4.3" [libraries] compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" , version.ref = "compose" } compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" , version.ref = "compose" } compose-ui = { module = "androidx.compose.ui:ui" , version.ref = "compose" } compose-runtime = { module = "androidx.compose.runtime:runtime" , version.ref = "compose" } compose-material3 = { module = "androidx.compose.material3:material3" , version.ref = "compose" } [bundles] compose = ["compose-ui-tooling" , "compose-ui-tooling-preview" , "compose-ui" , "compose-runtime" , "compose-material3" ]
一次性依赖所有的compose
android { compileSdkVersion libs.versions.compilesdk.get().toInteger() buildToolsVersion libs.versions.buildtools.get() defaultConfig { applicationId "com.lyy.keepassa" minSdkVersion libs.versions.minSdk.get().toInteger() targetSdkVersion libs.versions.targetsdk.get().toInteger() } } dependencies { implementation libs.bundles.compose implementation libs.compose.ui }
最终效果:
Vector Landscape Vectors by Vecteezy