开个头
为什么统一管理依赖呢?
当你的项目只有一个app module的时候,你想要修改compileSdkVersion,可以在app build.gradle里面直接改就好了,不会存在漏改的情况,但是相应的依赖版本号也要修改,比如,support库,design库等等,也需要你再app build.gradle里面一个一个修改,此时就会出现漏改的情况,不过,没关系,编译器会提醒你。
当你的项目不只有一个app module,还有更多的module的时候,此时如果要修改compileSdkVerison,是不是就比较费劲了,我们需要打开每一个module的 build.gradle,一个接一个修改,并且还要把依赖的版本号也要相应修改。具体有没有落下,完全靠gradle sync。。。
所以,需要一种方式来管理我们项目的依赖版本号,方便我们维护。
依赖统一管理
1.抽取
把需要统一管理的抽取出来,放在一个地方。
1.1build.gradle
直接抽取到项目的build.gradle里面如下
build.gradle
buildscript {...}
allprojects {...}
// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
// The following are only a few examples of the types of properties you can define.
compileSdkVersion = 28
buildToolsVersion = "28.0.3"
// You can also use this to specify versions for dependencies. Having consistent
// versions between modules can avoid behavior conflicts.
supportLibVersion = "28.0.0"
...
dependencies = [
"appcompat-v7" : "com.android.support:appcompat-v7:${supportLibVersion}",
"constraint-layout": "com.android.support.constraint:constraint-layout:1.1.3",
"support-v4" : "com.android.support:support-v4:${supportLibVersion}",
"percent" : "com.android.support:percent:${supportLibVersion}",
"design" : "com.android.support:design:${supportLibVersion}",
]
}
...
1.2 config.gradle
新建一个.gradle文件,和项目的build.gradle 同级。
config.gradle
ext {
//因为这里的配置方法和直接在build.gradle一样,所以这里换一种写法
android = [
compileSdkVersion : 28, (注意冒号和最后的逗号)
buildToolsVersion : "28.0.3",
supportLibVersion : "28.0.0",
...
//这里也可以自定义其他的参数
//constraint库的版本号
constraintVersion : "1.1.3",(也可以吧所有第三方的的版本号都出去抽来)
//最小支持的android版本号
minSdkVersion : 16,
//版本号
versionCode : 94,
//版本名
versionName : "1.0.0",
targetSdkVersion : 23,
applicationId : "xxx.xxx.xxx",
compileSdkVersion: 27,
]
dependencies = [
"appcompat-v7" : "com.android.support:appcompat-v7:${supportLibVersion}",
"constraint-layout": "com.android.support.constraint:constraint-layout:${constraintVersion}",
"support-v4" : "com.android.support:support-v4:${supportLibVersion}",
"percent" : "com.android.support:percent:${supportLibVersion}",
"design" : "com.android.support:design:${supportLibVersion}",
]
}
用config.gradle的方式的时候,还要注意在项目build.gradle里面加入下面代码
build.gradle
//加入下面一行代码
apply from: "config.gradle"
buildscript{...}
allprojects{...}
2.使用
经过第一步的抽取,我们已经把需要在app build.gradle和其他module build.gradle需要的依赖和其他参数都抽取了出来,接下来,就是在app和module里面开始使用了。
app 和 module里面的使用方法都一样,所以就看app build.gradle
app build.gradle
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
defaultConfig {
applicationId rootProject.ext.android.applicationId
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
}
...
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation rootProject.ext.dependencies["appcompat-v7"]
implementation rootProject.ext.dependencies["constraint-layout"]
implementation rootProject.ext.dependencies["support-v4"]
这样已经完成了。
但是可以看到大量的重复代码 rootProject.ext.android 和rootProject.ext.dependencies。这个我们可以定义全局变量,来优化。
app build.gradle
//定义两个全局变量
def extAndroid = rootProject.ext.android
def extDependencies = rootProject.ext.dependencies
android {
compileSdkVersion extAndroid.compileSdkVersion
defaultConfig {
applicationId extAndroid.applicationId
minSdkVersion extAndroid.minSdkVersion
targetSdkVersion extAndroid.targetSdkVersion
versionCode extAndroid.versionCode
versionName extAndroid.versionName
}
...
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation extDependencies["appcompat-v7"]
implementation extDependencies["constraint-layout"]
implementation extDependencies["support-v4"]
这样看起来还是比较舒服的,而且以后维护的时候,不用改所有module,也不用担心会有落下没有修改的地方。
参考
如有错误,欢迎指正!