一般對項目的依賴管理會有以下需求。
1、項目依賴同意管理,在單獨文件中配置。
2、不同的Module種的依賴版本號統(tǒng)一
3、不同項目種的依賴版本號統(tǒng)一
針對這些需求,提出了以下集中方案:
1、使用循環(huán)優(yōu)化Gradle依賴管理
2、使用buildSrc管理Gradle依賴
3、使用includeBuild統(tǒng)一配置依賴版本
4、使用Version Catalog統(tǒng)一配置依賴版本。
使用循環(huán)優(yōu)化Gradle依賴管理
這種方式就是在app的目錄下創(chuàng)建一個xxx.gradle文件,然后將我們項目所有需要用到的依賴放到該文件里,并分好類,例如:
ext{
android = [
compileSdkVersion : 29,
buildToolsVersion : "29.0.3",
minSdkVersion : 21,
targetSdkVersion : 29,
versionCode : 10101,
versionName : "1.01.01"
]
dependencies = [
"rxlifecycle" : "com.trello.rxlifecycle2:rxlifecycle:2.2.2",
"rxlifecycle-android" : "com.trello.rxlifecycle2:rxlifecycle-android:2.2.2",
"rxlifecycle-components" : "com.trello.rxlifecycle2:rxlifecycle-components:2.2.2",
//網(wǎng)絡(luò)
"retrofit" : "com.squareup.retrofit2:retrofit:2.6.2",
"retrofit_adapter" : "com.squareup.retrofit2:adapter-rxjava2:2.6.2",
"retrofit_gson" : "com.squareup.retrofit2:converter-gson:2.6.2",
"rxjava" : "io.reactivex.rxjava2:rxjava:2.2.14",
"rx_android" : "io.reactivex.rxjava2:rxandroid:2.1.1",
//gson
........
]
annotationProcessor = [
"butterknife-compiler" :"com.jakewharton:butterknife-compiler:10.0.0",
"glide-compiler" : "com.github.bumptech.glide:compiler:4.9.0",
"dagger-compiler" : "com.google.dagger:dagger-compiler:2.24",
]
apiFileDependencies = [
"launchstarter" :"libs/launchstarter-release-1.0.0.aar"
]
debugImplementationDependencies = [
"MethodTraceMan" : "com.github.zhengcx:MethodTraceMan:1.0.7"
]
...
implementationExcludes = [
"com.android.support.test.espresso:espresso-idling-resource:3.0.2" : [
'com.android.support' : 'support-annotations'
]
]
然后在我們的build.gradle文件中引用該文件
apply from:"config.gradle"
最后在build.gradle的dependencies中添加如下代碼:
def implementationDependencies = project.ext.dependencies
def processors = project.ext.annotationProcesso
def implementationExcludes = project.ext.implementationExcludes
dependencies{
// 處理所有的 xxximplementation 依賴
implementationDependencies.each { k, v -> implementation v }
// 處理 annotationProcessor 依賴
processors.each { k, v -> annotationProcessor v }
// 處理所有包含 exclude 的依賴
implementationExcludes.each { entry ->
implementation(entry.key) {
entry.value.each { childEntry ->
exclude(group: childEntry)
}
}
}
這樣做的優(yōu)點在于
1.后續(xù)添加依賴不需要改動build.gradle,直接在config.gradle中添加即可
2.精簡了build.gradle的長度
但是還是有一些痛點:
1.不支持代碼提示
2.不支持單擊跳轉(zhuǎn)
3.多模塊開發(fā)時,不同模塊相同的依賴需要復(fù)制粘貼。
使用buildSrc管理Gradle依賴
創(chuàng)建一個名為buildSrc的module.因為運行Gradle時,它會檢查項目中是否存在一個名為buildSrc的目錄。然后Gradle會自動編譯并測試這段代碼,并將其放入構(gòu)建腳本的類路徑中。您不需要提供任何進一步的操作提示。 所以也不需要在settings.gradle文件中添加include xxx。
步驟如下:
1.新建文件 build.gradle.kts,并同步 gradle
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
2.創(chuàng)建 /src/main/java/Version.kt 文件,在 Dependencies.kt 文件中編寫依賴管理代碼:
object BuildVersion {
const val compileSdk = 30
const val buildTools = "30.0.2"
const val minSdk = 21
const val targetSdk = 30
const val versionCode = 1
const val versionName = "1.0.0"
}
object Versions {
const val kotlin = "1.4.30"
const val core_ktx = "1.3.2"
const val appcompat = "1.2.0"
const val material = "1.3.0"
const val retrofit = "2.9.0"
}
object Libs {
const val kotlin_stdlib = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"
const val core_ktx = "androidx.core:core-ktx:1.3.2:${Versions.core_ktx}"
const val appcompat = "androidx.appcompat:appcompat:${Versions.appcompat}"
const val material = "com.google.android.material:material:${Versions.material}"
const val retrofit = "com.squareup.retrofit2:retrofit:${Versions.retrofit}"
}
3.在 module 中 build.gradle 應(yīng)用依賴項,如下
plugins {
id 'com.android.library'
id 'kotlin-android'
}
android {
compileSdkVersion BuildVersion.compileSdk
buildToolsVersion BuildVersion.buildTools
defaultConfig {
minSdkVersion BuildVersion.minSdk
targetSdkVersion BuildVersion.targetSdk
versionCode BuildVersion.versionCode
versionName BuildVersion.versionName
...
}
...
}
dependencies {
implementation Libs.kotlin_stdlib
implementation Libs.core_ktx
implementation Libs.appcompat
implementation Libs.material
implementation Libs.retrofit
}
缺點:由于 buildSrc 是對全局的所有 module 的配置,因此在構(gòu)建速度上會慢一些。
使用includeBuild統(tǒng)一配置依賴版本
步驟如下:
1.先創(chuàng)建一個java或kotlin的library.
2.然后在創(chuàng)建好的library的build.gradle文件中添加以下代碼,我這里用的kotlin,如果用的Java,則 apply plugin: 'java-library'。
apply plugin: 'kotlin'
apply plugin: 'java-gradle-plugin'
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
}
}
repositories {
jcenter()
google()
}
dependencies {
implementation gradleApi()
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
}
gradlePlugin{
plugins{
version{
//id 對應(yīng)package name
id = "com.example.version"
//implementationClass 則是對應(yīng)我們第三步需要創(chuàng)建的一個類,其內(nèi)容為包名 + 類名
implementationClass = "com.example.version.DependencyVersionPlugin"
}
}
}
3.在com.example.version路徑下創(chuàng)建一個類
class DependencyVersionPlugin : Plugin<Project> {
override fun apply(target: Project) {
}
}
4.在src/main/java的路徑下添加文件,文件里就是所有的依賴集合
5.在settings.gradle添加
//括號里對應(yīng)library名
includeBuild("version")
6.然后在需要的module,添加
plugins{
id "com.example.version"
}
sync之后,就可使用了。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(AndroidX.coreKtx)
implementation(AndroidX.appcompat)
implementation(AndroidX.constraintlayout)
}
這里有一個坑,不能將
android {
compileSdkVersion ProjectBuildConfigs.compileSdkVersion
buildToolsVersion ProjectBuildConfigs.buildToolsVersion
defaultConfig {
minSdkVersion ProjectBuildConfigs.minSdkVersion
targetSdkVersion ProjectBuildConfigs.targetSdkVersion
versionCode ProjectBuildConfigs.versionCode
versionName ProjectBuildConfigs.versionName
consumerProguardFiles "consumer-rules.pro"
ndk {
// 設(shè)置支持的SO庫架構(gòu)
//abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86'
}
}
}
這些公共的東西放在一個公共的base_xxx.gradle,然后再在build.gradle里面 apply from "base_xxx.gradle ",這樣會一直報compileSdkVersion is not specified.的錯誤。我在這上面弄了好久,一直懷疑是寫的有問題。

使用紅色框的方式會一直報錯,所以我們需要采用綠色框的方式
還有一個坑就是不能套娃:
object Kotlin {
const val Kotlin = "org.jetbrains.kotlin:kotlin-stdlib:${Version.Kotlin}"
const val CoroutinesCore =
"org.jetbrains.kotlinx:kotlinx-coroutines-core:${Version.Coroutines}"
const val CoroutinesAndroid =
"org.jetbrains.kotlinx:kotlinx-coroutines-android:${Version.Coroutines}"
object GitHub {
const val OkHttp = "com.squareup.okhttp3:okhttp:${Version.OkHttp}"
const val OkHttpInterceptorLogging =
"com.squareup.okhttp3:logging-interceptor:${Version.OkHttpInterceptorLogging}"
const val Retrofit = "com.squareup.retrofit2:retrofit:${Version.Retrofit}"
}
}
這樣會編譯不過。
使用Version Catalog統(tǒng)一配置依賴版本
Catalog 是Gradle7.0推出了一個新的特性,它支持以下特性:
1.對所有module可見,可統(tǒng)一管理所有module的依賴
2.支持聲明依賴bundles,即總是一起使用的依賴可以組合在一起
3.支持版本號與依賴名分離,可以在多個依賴間共享版本號
4.支持在單獨的libs.versions.toml文件中配置依賴
5.支持在項目間共享依賴
具體使用請看【Gradle7.0】依賴統(tǒng)一管理的全新方式,了解一下~