个人学习整理。
前言
现在大项目流行模块化,组件化开发,一个项目通常有多个 module,如果每个module里面的依赖和版本分开管理,会导致依赖进行更新的时候要逐一进行更新,增大工作量的同时还可能出现项目中引用了多个版本的依赖库,为了方便Gradle的管理,对其进行统一管理就显得更加重要,统一管理实现方式多种,可根据自己需求选择合适的进行配置。
配置
1、在Project根目录新建 xxx.gradle
文件。
如
config.gradle
2、在 Project 根目录的 build.gradle
中引入刚刚添加的 xxx.gradle
apply from:"config.gradle"
3、在 xxx.gradle
中对依赖库版本进行统一管理
ext {
android = [
compileSdkVersion: 28,
minSdkVersion : 15,
targetSdkVersion : 28,
versionCode : 1,
]
version = [
supportLibraryVersion: '28.0.0',
daggerVersion : '2.21',
]
dependencies = [
"dagger2" : "com.google.dagger:dagger:${version["daggerVersion"]}",
"dagger2-Compile": "com.google.dagger:dagger-compiler:${version["daggerVersion"]}",
"recycleView" : "com.android.support:recyclerview-v7:${version["supportLibraryVersion"]}",
"appcomtV7" : "com.android.support:appcompat-v7:${version["supportLibraryVersion"]}",
"junit" : "junit:junit:4.12",
"constraint" : "com.android.support.constraint:constraint-layout:1.1.3",
"runner" : "com.android.support.test:runner:1.0.2",
"espresso_core" : "com.android.support.test.espresso:espresso-core:3.0.2"
]
}
4、在module的 builde.gradle
中引入
apply plugin: 'com.android.application'
def versionMajor = 1
def versionMinor = 0
def versionPatch = 0
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
def versions = rootProject.ext.android
defaultConfig {
applicationId "com.evan.wanandroid"
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode versions.versionCode
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def dependencies = rootProject.ext.dependencies
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation dependencies.appcomtV7
implementation dependencies.constraint
testImplementation dependencies.junit
androidTestImplementation dependencies.runner
androidTestImplementation dependencies.espresso_core
}