依赖版本管理有多种方式
- config.gradle
用于Groovy DSL,新建一个 config.gradle 文件,然后将项目中所有依赖写在里面,更新只需修改 config.gradle文件内容,作用于所有module
- buildSrc
可用于Kotlin DSL或Groovy DSL,对于多项目构建,只有一个 buildSrc 目录,该目录必须位于根项目目录中,Gradle 会自动编译并测试目录代码
- version catalog
可用于Kotlin DSL或Groovy DSL,与config.gradle类似,缺点是要求gradle版本在7.4.2以上
这里只介绍config.gradle和version catalog
一.原gradle
1.Groovy DSL:
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.myapplication'
compileSdk 34
defaultConfig {
applicationId "com.example.myapplication"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
2.Kotlin DSL:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.myapplication"
compileSdk = 33
defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
二.config.gradle
1.先在根目录创建config.gradle文件
2.根据自己的依赖情况填写config.gradle
在config.gradle中我们还可以定义方法,做到某些关键字可以动态定义,假设我们有多buildTypes的应用,我们可以通过这种方式输出不同的版本名称,版本号等等。
ext {
AppDemoDebug = "debug"
AppDemoRelease = "release"
versionCode = 1
android = [
applicationId : "com.example.myapplication",
compileSdk : 34,
minSdk : 24,
targetSdk : 33,
versionCode : versionCode,
versionName : getVersionName(),
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner",
]
version = [
material : "1.11.0",
core : "1.9.0",
appcompat : "1.6.1",
constraintlayout: "2.1.4",
navigation : "2.5.3",
navigation_ui : "2.5.3",
junit : "4.13.2",
test_ext : "1.1.5",
espresso : "3.5.1"
]
dependencies = [
material : "com.google.android.material:material:$version.material",
core : "androidx.core:core-ktx:$version.core",
appcompat : "androidx.appcompat:appcompat:$version.appcompat",
constraintLayout: "androidx.constraintlayout:constraintlayout:$version.constraintLayout",
navigation : "androidx.navigation:navigation-fragment-ktx:$version.navigation",
navigation_ui : "androidx.navigation:navigation-ui-ktx:$version.navigation_ui",
test_junit : "junit:junit:$version.junit",
test_ext : "androidx.test.ext:junit:$version.test_ext",
test_espresso : "androidx.test.espresso:espresso-core:$version.espresso",
]
}
def getVersionName() {
String taskName = getGradle().getStartParameter().getTaskRequests().toString()
if (taskName.contains(AppDemoDebug)) {
return versionCode + "_" + releaseTime() + "_DEBUG"
} else {
return versionCode + "_" + releaseTime() + "_RELEASE"
}
}
static def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
3.对内容进行关联
在项目层级的build.gradle中增加 apply from:"config.gradle"
如果config.gradle内存书写有错误,则会在这个时候报错,根据错误内容修改文件就行了
4.对内容进行引用
引用后的build.gradle文件,可以对照前面的Groovy DSL,所有的版本号和依赖都引用了config.gradle,以此进行统一管理。
但是有明显的缺点是
- 点击无法跳转到config.gradle对应关键字。
- 依赖没有升级提示。
优点就是可以动态定义版本名称等。
//file:noinspection DependencyNotationArgument
plugins {
id 'com.android.application'
}
android {
namespace rootProject.ext.android.applicationId
compileSdk rootProject.ext.android.compileSdk
defaultConfig {
applicationId rootProject.ext.android.applicationId
minSdk rootProject.ext.android.minSdk
targetSdk rootProject.ext.android.targetSdk
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
testInstrumentationRunner rootProject.ext.android.testInstrumentationRunner
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation rootProject.ext.dependencies.appcompat
implementation rootProject.ext.dependencies.material
implementation rootProject.ext.dependencies.constraintLayout
implementation rootProject.ext.dependencies.navigation
implementation rootProject.ext.dependencies.navigation_ui
testImplementation rootProject.ext.dependencies.test_junit
androidTestImplementation rootProject.ext.dependencies.test_ext
androidTestImplementation rootProject.ext.dependencies.test_espresso
}
三.version catalog
1.在gradle目录下创建libs.versions.toml文件
2.根据自己的依赖情况填写libs.versions.toml
[versions]
#plugins-version
application = "8.1.3"
kotlin = "1.9.0"
#config-version
applicationId = "com.example.myapplication"
compileSdk = "34"
minSdk = "24"
targetSdk = "33"
versionCode = "1"
versionName = "1.0"
#dependence-version
material = '1.11.0'
core = '1.9.0'
appcompat = '1.6.1'
constraintlayout = '2.1.4'
navigation = '2.5.3'
navigation-ui = '2.5.3'
junit = '4.13.2'
test-ext = '1.1.5'
espresso = '3.5.1'
[libraries]
android-material = { module = "com.google.android.material:material", version.ref = "material" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "core" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
navigation = { module = "androidx.navigation:navigation-fragment-ktx", version.ref = "navigation" }
navigation-ui = { module = "androidx.navigation:navigation-ui-ktx", version.ref = "navigation-ui" }
test-junit = { module = "junit:junit", version.ref = "junit" }
test-ext = { module = "androidx.test.ext:junit", version.ref = "test-ext" }
test-espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" }
[bundles]
android-test = ["test-ext","test-espresso"]
androidx-navigation = ["navigation", "navigation-ui"]
[plugins]
appliaction = { id = "com.android.application", version.ref = "application" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
该文件分了四个板块,[versions],[libraries],[bundles],[plugins],bundles的作用是将libraries中你觉得可以绑定到一起的引用绑定到一块,在build.gradle中引用时,只引用bundle的值就行,相当于一次引用多个libraries。
该文件不需要进行关联,gradle会自动识别****.toml文件,所以只要按这个格式命名就行。
3. 对内容进行引用
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
alias(libs.plugins.appliaction)
alias(libs.plugins.kotlin.android)
}
android {
namespace = "com.example.myapplication"
compileSdk = Integer.parseInt(libs.versions.compileSdk.get())
defaultConfig {
applicationId = libs.versions.applicationId.get()
minSdk = Integer.parseInt(libs.versions.minSdk.get())
targetSdk = Integer.parseInt(libs.versions.targetSdk.get())
versionCode = Integer.parseInt(libs.versions.versionCode.get())
versionName = libs.versions.versionName.get()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.android.material)
implementation(libs.androidx.constraintlayout)
implementation(libs.bundles.androidx.navigation)
testImplementation(libs.test.junit)
androidTestImplementation(libs.bundles.android.test)
}
引用后的build.gradle文件,可以对照前面的Kotlin DSL,所有的版本号和依赖都引用了libs.versions.toml,以此进行统一管理。version catalog弥补了config.gradle的缺点,可支持跳,在文件中也可以看到引用的升级建议: