Android开发中,由于依赖库升级迭代非常快的原因,一旦有新的更新我们就要修改moudle的版本信息,显得非常麻烦
那么封装一下Gradle的版本号配置就显得十分重要了
本篇将针对Gradle的版本号配置作为讲解,下面内容介绍下个人总结的几种方式
# Android configuration
ANDROID_COMPILE_SDK_VERSION=25
ANDROID_BUILD_TOOLS_VERSION=25.0.2
ANDROID_APPLICATION_ID=zs.xmx
ANDROID_MIN_SDK_VERSION=15
ANDROID_TARGET_SDK_VERSION=25
ANDROID_VERSION_CODE=1
ANDROID_VERSION_NAME=1.0
# Dependencies versions
SUPPORT_LIBRARY_VERSION=25.3.1
//android{...} 调用:
//1. project.ANDROID_BUILD_TOOLS_VERSION
//2. 直接调用 ANDROID_BUILD_TOOLS_VERSION
android {
compileSdkVersion project.ANDROID_COMPILE_SDK_VERSION as int
buildToolsVersion ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
applicationId ANDROID_APPLICATION_ID
minSdkVersion ANDROID_MIN_SDK_VERSION as int
targetSdkVersion ANDROID_TARGET_SDK_VERSION as int
versionCode ANDROID_VERSION_CODE as int
versionName ANDROID_VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
//dependencies{...} 调用:
//用占位符动态更新,注意要改成双引号,别的库同理
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:appcompat-v7:${SUPPORT_LIBRARY_VERSION}"
compile "com.android.support:design:${SUPPORT_LIBRARY_VERSION}"
}
- 建议敏感信息都写在gradle.properties文件,编译apk时,不会打包这个文件
- dependencies{} 用占位符替换后的 complie 要用双引号
- 建议明确版本号,不要写 + 号的形式,省去gradle编译时查找的时间
- gradle.properties文件中不要有中文,可能存在编码问题
- dependencies{}中的同类型依赖库版本应保持一致
- 配置默认是字符串,int类注意用 as int 来转换
如:
ext {
android = [compileSdkVersion: 25,
buildToolsVersion: "25.0.2",
minSdkVersion : 15,
targetSdkVersion : 25,
versionCode : 1,
versionName : "1.0"]
dependencies = ["support-v7" : 'com.android.support:appcompat-v7:25.3.1',
"design" : 'com.android.support:design:25.3.1',
"rx_android" : 'io.reactivex:rxandroid:1.0.1',
"rx_binding" : 'com.jakewharton.rxbinding:rxbinding:0.2.0',
"retrofit" : 'com.squareup.retrofit2:retrofit:2.0.0-beta3',
"retrofit_adapter": 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3']
}
在app的build.gradle中引用config.gradle->在代码最上方添加 apply from: “config.gradle” 进行引用
//在app的build.gradle中引用config.gradle->在代码最上方添加 apply from: "config.gradle" 进行引用
apply from: "config.gradle"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
...
使用rootProject.ext.xxx的形式进行调用
dependencies{…} 使用 rootProject.ext.dependencies[“support-v7”] 进行调用
//使用rootProject.ext.xxx的形式进行调用
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
applicationId "neacy.rxdemo"
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile rootProject.ext.dependencies["support-v7"]
compile rootProject.ext.dependencies["design"]
// RxAndroid
compile rootProject.ext.dependencies["rx_android"]
compile rootProject.ext.dependencies["rx_binding"]
// Retrofit
compile rootProject.ext.dependencies["retrofit"]
compile rootProject.ext.dependencies["retrofit_adapter"]
}
defaultConfig {
//配置 moudle 的 build.gradle 方式,直接定义变量名声明
final Version_Code = '1'
final Version_Name = '1.0'
versionCode Version_Code as int
versionName Version_Name
//配置 gradle.properties 方式
applicationId ANDROID_APPLICATION_ID
minSdkVersion ANDROID_MIN_SDK_VERSION as int
targetSdkVersion ANDROID_TARGET_SDK_VERSION as int
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
// Define versions in a single place
ext {
// SDK And Tools
minSdkVersion = 14
targetSdkVersion = 23
compileSdkVersion = 23
buildToolsVersion = '24.0.2'
//Dependencies
supportLibraryVersion = '23.2.1'
}
apply plugin: 'com.android.application'
//使用 rootProject.ext.compileSdkVersion 调用
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId 'com.mainiway.demo'
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
}
//dependencies{...} 使用占位符 $rootProject.supportLibraryVersion 调用(注意 complie 要修改为双引号)
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:design:$rootProject.supportLibraryVersion"
}
后续有补充的内容,会即时更新,欢迎评论以及关注,谢谢