在进行项目开发的时候有时候需要添加第三方依赖,或者导入Modules进行依赖,而导入的一些Modules中就有可能存在相同的依赖而版本却不一致,因此需要进行修改成统一版本,还有编译版本和构造版本最好尽量保持一致,而如果都是去拷贝粘贴使得版本一致,会使得如果版本需要更改时,每一个Modules都需要进行去更改,因此这里提供一种方法,让子使用到的Modules依赖于主Modules,而需要更改版本时,只需要更改主Modules的版本就可以了。
1.在Project同级目录下创建一个config.gradle文件,将需要用到的依赖写入该文件
ext {
android = [
compileSdkVersion: 25,
buildToolsVersion: "25.0.2",
minSdkVersion : 15,
targetSdkVersion : 22,
versionCode : 49,
versionName : '3.6.5',
VSupportSdk : '23.3.0',
VRetrofitSdk : "2.2.0",
VOkhttp : "3.4.2",
VRxlifecycle : "2.0.1",
multidex : "1.0.1"
]
dependencies = [
"appcompat-v7" : "com.android.support:appcompat-v7:${android["VSupportSdk"]}",
"support-v4" : "com.android.support:support-v4:${android["VSupportSdk"]}",
"design" : "com.android.support:design:${android["VSupportSdk"]}",
"annotations" : "com.android.support:support-annotations:${android["VSupportSdk"]}",
"recyclerview-v7" : "com.android.support:recyclerview-v7:${android["VSupportSdk"]}",
"multidex" : "com.android.support:multidex:${android["multidex"]}",
"butterknife" : "com.jakewharton:butterknife:8.4.0",
"butterknife-apt" : "com.jakewharton:butterknife-compiler:8.4.0",
"eventbus" : "org.greenrobot:eventbus:3.0.0",
"glide" : "com.github.bumptech.glide:glide:3.7.0",
"picasso" : "com.squareup.picasso:picasso:2.5.2",
"xrecyclerview" : "com.github.limedroid:ARecyclerView:v1.1.5",
"avi-loading" : "com.wang.avi:library:1.0.2",
"gson" : "com.google.code.gson:gson:2.6.2",
"rxandroid" : "io.reactivex.rxjava2:rxandroid:2.0.1",
"rxjava" : "io.reactivex.rxjava2:rxjava:2.0.1",
}`
可以在Android节点下设置占位,并在dependencies节点下进行引用。
2.在项目的build.gradle中添加引用apply from: “config.gradle”
3.在Modules中进行需要的依赖添加,APP中同样可以进行使用
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile rootProject.ext.dependencies["appcompat-v7"]
compile rootProject.ext.dependencies["support-v4"]
compile rootProject.ext.dependencies["design"]
compile rootProject.ext.dependencies["annotations"]
compile rootProject.ext.dependencies["recyclerview-v7"]
compile rootProject.ext.dependencies["xrecyclerview"]
compile rootProject.ext.dependencies["butterknife"]
compile rootProject.ext.dependencies["glide"]
}