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

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


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


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

 

方式一

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

 

 

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

 

 

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

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

[html]  view plain  copy
  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{ .... } 中的内容

[html]  view plain  copy
  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.   
  9.         // NOTE: Do not place your application dependencies here; they belong  
  10.         // in the individual module build.gradle files  
  11.     }  
  12. }  
  13. allprojects {  
  14.     repositories {  
  15.         jcenter()  
  16.         maven { url "https://jitpack.io" }  
  17.     }  
  18. }  
  19. task clean(type: Delete) {  
  20.     delete rootProject.buildDir  
  21. }  
  22.   
  23. // Define versions in a single place  
  24. ext {  
  25.   
  26.     // SDK And Tools  
  27.     minSdkVersion = 14  
  28.     targetSdkVersion = 23  
  29.     compileSdkVersion = 23  
  30.     buildToolsVersion = '24.0.2'  
  31.   
  32.     //Dependencies  
  33.     supportLibraryVersion = '23.2.1'  
  34.   
  35. }  

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

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


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

[html]  view plain  copy
  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.   
  13. }  

对应的引用方式:

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


或者像这样:

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

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

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


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




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

猜你喜欢

转载自blog.csdn.net/wangshixu2015/article/details/80391591