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

 更新时间:2018年01月12日 09:48:20   作者:QDJdeveloper  
本篇文章主要介绍了详解Android 使用 Gradle 统一配置依赖管理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

在介绍使用 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 统一配置依赖,希望对大家有用,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android 自定义标题栏背景

    Android 自定义标题栏背景

    最近在做android项目,需要做一个自定义的标题栏(操作栏)。去网上找了很多demo,发现都有很多问题。例如使用自定义的style。下面来分享下个人最终的解决方案吧
    2016-01-01
  • Android仿微信拍摄短视频

    Android仿微信拍摄短视频

    本文主要对Android仿微信拍摄短视频的实现方法进行介绍,其功能设置为类似于微信,点击开始拍摄,设置最长拍摄时间。具有很好的参考价值,需要的朋友一起来看下吧
    2016-12-12
  • Android 实现永久性开启adb 的root权限

    Android 实现永久性开启adb 的root权限

    这篇文章主要介绍了Android 实现永久性开启adb 的root权限,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • android简单自定义View实现五子棋

    android简单自定义View实现五子棋

    这篇文章主要为大家详细介绍了android简单自定义View实现五子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android-App增量更新的使用姿势

    Android-App增量更新的使用姿势

    增量更新根据字面理解就是下载增加的那部分来达到更新的目获取旧的Apk安装包的签名和已合并成新的Apk安装包的签名,对比签名是否一致当你下载差异文件时,可以让服务器给你返回新的Apk合并成功后文件的md5,当你合并成功后,通过校验文件的md5值,达到校验文件完整性。
    2016-04-04
  • Android自定义View之绘制圆形头像功能

    Android自定义View之绘制圆形头像功能

    这篇文章主要介绍了Android自定义View之绘制圆形头像功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • android中实现背景图片颜色渐变方法

    android中实现背景图片颜色渐变方法

    这篇文章主要介绍了android中实现背景图片颜色渐变方法,本文直接使用配置文件实现了这个效果,需要的朋友可以参考下
    2015-05-05
  • Android中再按一次退出提醒实现的两种方法

    Android中再按一次退出提醒实现的两种方法

    今天小编就为大家分享一篇关于Android中再按一次退出提醒实现的两种方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • RecyclerView上拉加载封装代码

    RecyclerView上拉加载封装代码

    这篇文章主要为大家详细介绍了RecyclerView上拉加载封装代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android动态加载布局实现技巧介绍

    Android动态加载布局实现技巧介绍

    通过使用LayoutInflater 每次点击按钮时候去读取布局文件,然后找到布局文件里面的各个VIEW 操作完VIEW 后加载进我们setContentView 方面里面的要放的布局文件里面,每次动态加载文件必需调用 removeAllViews方法,清除之前的加载进来的View
    2022-12-12

最新评论