Android Studio gradle 技巧二 依赖统一管理

开个头

为什么统一管理依赖呢?
当你的项目只有一个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,也不用担心会有落下没有修改的地方。

参考

google官方开发者文档

如有错误,欢迎指正!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值