详解Android使用Gradle统一配置依赖管理

时间:2021-05-19

在介绍使用 Gradle 统一配置依赖管理前我们先来简单介绍一下 Gradle, Gradle 是一个基于 JVM 的构建工具,也是一款非常灵活强大的构建工具,支持 jcenter、maven、Ivy 仓库,支持传递性依赖管理(即 A 依赖 B,B 依赖 C,那么 A 也就可以依赖 C,不用再单独去依赖),而不需要远程仓库或者是 pom.xml 和 ivy.xml 配置文件,抛弃了各种繁琐,基于 Groovy,build 脚本使用 Groovy 编写

而在我们的 Android studio 中默认就是使用 Gradle 来构建管理我们的工程的,在我们的工程构建过程中通常会创建很多个 Module 来对我们的工程进行功能以及业务上的解耦(也就是模块化开发),这时候可能就会存在一个问题,就是每个 Module 以及 Module 中一些公用库的依赖可能会出现版本不统一的问题,包括使用的编译版本,SDK 的版本等,导致不能打包,这里可以使用 Gradle 统一配置文件来解决我们的问题

首先我们来看一下,正常情况下我们的项目目录的 build.gradle 情况:

先看 app 下的 build.gradle:

//说明module的类型,com.android.application为程序,com.android.library为库 apply plugin: 'com.android.application' android { //编译的 SDK 版本 compileSdkVersion 25 //编译的 Tools 版本 buildToolsVersion "25.0.2" //默认配置 defaultConfig { //应用程序的包名 applicationId "com.example.qiudengjiao.activitytest" //支持 SDK 的最低版本 minSdkVersion 15 //支持 SDK 的目标版本 targetSdkVersion 25 //版本号 versionCode 1 //版本名 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } //build 类型 buildTypes { release { //混淆是否开启,返回true则开启 minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } //在这里进行库的依赖 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' //support v7 支持库 compile 'com.android.support:appcompat-v7:25.1.0' }

接下来我们再来看一下项目根目录下的 build.gradle:

//构建脚本 buildscript { repositories { //依赖的仓库 jcenter() } dependencies { //项目依赖的Gradle版本 classpath 'com.android.tools.build:gradle:2.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }

现在我们添加一个 Module 库,来看一下我们 Module 库下的 build.gradle:

apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { minSdkVersion 13 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.0' testCompile 'junit:junit:4.12' }

这里我们来看一下和 app 目录下的 build.gradle 有什么区别:

app 目录下的 build.gradle 是:apply plugin:com.android.application

Module 库下的 build.gradle 是:apply plugin:com.android.library

其它的就是版本的不一样了,要素是一样的,这里就是我们今天着重要来介绍的,这里我们看到编译的 SDK 版本和编译的 Tools 版本以及支持 SDK 的最低版本等的版本号都是不一样的,这里我们就需要来统一,而我们总不能每次都来手动配置,当 Module 增多时则容易出错

解决办法:

方法一

在项目的根目录的 build.gradle 里进行统一配置如下:

ext { //Android compileSdkVersion = 25 buildToolsVersion = "25.0.2" minSdkVersion = 15 targetSdkVersion = 25 //Version supportLibrary = "25.1.0" //supportLibraries dependencies supportDependencies = [ supportAppcompat: "com.android.support:appcompat-v7:${supportLibrary}", ] }

配置完后工程根目录的 build.gradle 情况:

//构建脚本 buildscript { repositories { //依赖的仓库 jcenter() } dependencies { //项目依赖的Gradle版本 classpath 'com.android.tools.build:gradle:2.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } ext { //Android compileSdkVersion = 25 buildToolsVersion = "25.0.2" minSdkVersion = 15 targetSdkVersion = 25 //Version supportLibrary = "25.1.0" //supportLibraries dependencies supportDependencies = [ supportAppcompat: "com.android.support:appcompat-v7:${supportLibrary}", ] }

接下来我们在 app 的 build.gradle 中进行调用如下:

apply plugin: 'com.android.application' android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { applicationId "com.example.qiudengjiao.activitytest" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'junit:junit:4.12' compile rootProject.ext.supportDependencies.supportAppcompat }

在 Module 的 build.gradle 中进行调用如下:

apply plugin: 'com.android.library' android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' compile rootProject.ext.supportDependencies.supportAppcompat }

这样我们就完成了使用 Gradle 对项目中 app 下的 build.gradle 和 Module 中的 build.gradle 依赖进行统一配置的解决,以此类推,更多的 Module 也是如此配置,以后需要版本的更改我们只需要去根目录 build.gradle 修改即可

方法二

因为每个人都有自己的配置习惯,这里我们再提供一种配置以供大家参考,这里我们在主项目的根目录下创建 config.gradle 来配置需要的相关配置信息如下:

config.gradle 里面的配置信息:

/** * 在主项目的根目录下创建config.gradle文件 * 在这里单独处理统一依赖问题 * 注意需要在根目录的build.gradle中进行引入 */ ext { android = [ compileSdkVersion: 25, buildToolsVersion: "25.0.2", minSdkVersion : 15, targetSdkVersion : 25 ] //Version supportLibrary = "25.1.0" //supportLibraries dependencies supportDependencies = [ supportAppcompat: "com.android.support:appcompat-v7:${supportLibrary}", supportV4 : "com.android.support:support-v4:${supportLibrary}", suppoutDesign : "com.android.support:design:${supportLibrary}" ] }

然后我们需要在根目录的 build.gradle 中把 config.gradle 引入进来,这里特别注意是在根目录的 build.gradle 中引入
引入的代码为:

apply from: "config.gradle"

引入后的根目录 build.gradle 如下:

//在这里引入config.gradle apply from: "config.gradle" buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }

接下来我们就可以在 Module 中引入使用了,如下:

apply plugin: 'com.android.library' //android配置 def config = rootProject.ext.android //相关库依赖 def librarys = rootProject.ext.supportDependencies android { compileSdkVersion config.compileSdkVersion buildToolsVersion config.buildToolsVersion defaultConfig { minSdkVersion config.minSdkVersion targetSdkVersion config.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' //在这里使用库的依赖 compile librarys.supportAppcompat compile librarys.supportV4 compile librarys.suppoutDesign }

到这里我们就成功的引入到了 Module 的 build.gradle 中,以后每个 Module 中的引入都是这样,实现了和方法一 同样的功能,个人感觉第二种更好一点,大家自己选择吧,毕竟各有所好,好了,到这里就给大家分享完了在项目中使用 Gradle 统一配置依赖,希望对大家有用,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章