Android Studio中统一管理版本号引用配置

版权声明:尊重博主劳动成果,转载务必注明出处,谢谢! https://blog.csdn.net/gao_chun/article/details/58105089

转载注明gao_chun的Blog:http://blog.csdn.net/gao_chun/article/details/58105089


Gradle统一管理版本号引用配置


为了提高项目开发效率,在实际项目开发过程中往往会引入一些开源框架,还有项目中使用的各种Module,当引入Module过多时最好提供一种统一的方式去管理版本号,如:compileSdkVersion、buildToolsVersion、androidTestCompile 等,便于日后对版本号进行维护,此处记录2种方式处理上述问题。推荐方式二和方式三

 

方式一

1.在项目根目录下创建.gradle文件,如:config.gradle

 

 

2.在根目录下的build.gradle文件中引入我们创建的配置文件

 

 

3.config.gradle中文件内容可以自己定义,如下示例:


   
   
  1. ext {
  2. // 用于编译的SDK版本
  3. COMPILE_SDK_VERSION = 23
  4. // 用于Gradle编译项目的工具版本
  5. BUILD_TOOLS_VERSION = "24.0.2"
  6. // 最低支持Android版本
  7. MIN_SDK_VERSION = 14
  8. // 目标版本
  9. TARGET_SDK_VERSION = 23
  10. // 设置是否使用混淆
  11. MINIFY_ENABLED = true
  12. MINIFY_DISABLED = false
  13. // 应用程序包名
  14. APPLICATION_ID = 'com.mainiway.eworkpal'
  15. // Version of "com.android.support:appcompat-v7", refer it as folow:
  16. // compile "com.android.support:appcompat-v7:${APPCOMPAT_VERSION}"
  17. APPCOMPAT_VERSION = '23.2.1'
  18. }
4.在app目录下的build.gradle中使用


   
   
  1. dependencies {
  2. compile fileTree(include: ['*.jar'], dir: 'libs')
  3. compile "com.android.support:cardview-v7:${APPCOMPAT_VERSION}"
  4. compile "com.android.support:appcompat-v7:${APPCOMPAT_VERSION}"
  5. compile "com.android.support:design:${APPCOMPAT_VERSION}"
  6. compile 'com.github.bumptech.glide:glide:3.7.0'
  7. }


方式二(推荐)

1.在根目录下的build.gradle文件下添加 ext{ .... } 中的内容


   
   
  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. repositories {
  4. jcenter()
  5. }
  6. dependencies {
  7. classpath 'com.android.tools.build:gradle:2.2.3'
  8. // NOTE: Do not place your application dependencies here; they belong
  9. // in the individual module build.gradle files
  10. }
  11. }
  12. allprojects {
  13. repositories {
  14. jcenter()
  15. maven { url "https://jitpack.io" }
  16. }
  17. }
  18. task clean(type: Delete) {
  19. delete rootProject.buildDir
  20. }
  21. // Define versions in a single place
  22. ext {
  23. // SDK And Tools
  24. minSdkVersion = 14
  25. targetSdkVersion = 23
  26. compileSdkVersion = 23
  27. buildToolsVersion = '24.0.2'
  28. //Dependencies
  29. supportLibraryVersion = '23.2.1'
  30. }

2.在app目录下build.gradle中使用 $rootProject.supportLibraryVersion


   
   
  1. apply plugin: 'com.android.application'
  2. android {
  3. compileSdkVersion rootProject.ext.compileSdkVersion
  4. buildToolsVersion rootProject.ext.buildToolsVersion
  5. defaultConfig {
  6. applicationId 'com.mainiway.demo'
  7. minSdkVersion rootProject.ext.minSdkVersion
  8. targetSdkVersion rootProject.ext.targetSdkVersion
  9. versionCode 1
  10. versionName "1.0.0"
  11. }
  12. }
  13. dependencies {
  14. compile fileTree(include: ['*.jar'], dir: 'libs')
  15. compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
  16. compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
  17. compile "com.android.support:design:$rootProject.supportLibraryVersion"
  18. }


方式三(推荐)
也可以在根目录下的 build.gradle 文件中声明 ext{ .... }  时, 加上一个变量 var ,如:


   
   
  1. ext {
  2. var = [
  3. // SDK And Tools
  4. minSdkVersion : 14,
  5. targetSdkVersion : 25,
  6. compileSdkVersion : 25,
  7. buildToolsVersion : "25.0.2",
  8. versionName : "1.0.0",
  9. //Dependencies
  10. supportLibraryVersion: "25.2.0"
  11. ]
  12. }

对应的引用方式:


   
   
  1. android {
  2. compileSdkVersion var.compileSdkVersion
  3. buildToolsVersion var.buildToolsVersion
  4. defaultConfig {
  5. applicationId "com.xxx.xxx"
  6. minSdkVersion var.minSdkVersion
  7. targetSdkVersion var.targetSdkVersion
  8. versionCode 1
  9. versionName var.version
  10. }
  11. ......
  12. }
  13. dependencies {
  14. compile "com.android.support:appcompat-v7:$var.supportLibraryVersion"
  15. compile "com.android.support:recyclerview-v7:$var.supportLibraryVersion"
  16. compile "com.android.support:cardview-v7:$var.supportLibraryVersion"
  17. }


或者像这样:


   
   
  1. ext {
  2. minSdkVersion: 14,
  3. targetSdkVersion: 25,
  4. compileSdkVersion: 25,
  5. buildToolsVersion: "25.0.2",
  6. versionName: "1.0.0",
  7. //Dependencies
  8. supportLibraryVersion: "25.2.0"
  9. var = [
  10. SupportV7 : "com.android.support:appcompat-v7:$supportLibraryVersion",
  11. SupportV4 : "com.android.support:support-v4:$supportLibraryVersion",
  12. SupportRecyclerviewV7 : "com.android.support:recyclerview-v7:$supportLibraryVersion"
  13. ]
  14. }

那么对应的引用方式要做一点点变化:


   
   
  1. android {
  2. compileSdkVersion rootProject.compileSdkVersion
  3. buildToolsVersion rootProject.buildToolsVersion
  4. defaultConfig {
  5. applicationId "com.xxx.xxx"
  6. minSdkVersion rootProject.minSdkVersion
  7. targetSdkVersion rootProject.targetSdkVersion
  8. versionCode 1
  9. versionName var.version
  10. }
  11. ... ...
  12. }
  13. dependencies {
  14. compile var.SupportV7
  15. compile var.SupportV4
  16. compile var.SupportRecyclerviewV7
  17. }


选一种适合规范方式,这样一来,管理各个版本号就方便多了。




转自:https://blog.csdn.net/gao_chun/article/details/58105089

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值