项目根目录中创建一个gradle文件,名字随便起,也可以不创建直接写在项目的build.gradle文件中。
1.在gradle文件中定义变量并赋值:
例如我在项目根目录创建globalconfig.gradle文件,内容如下:
ext {
//签名文件目录
storageFilePath = [
releaseSignStoreFile: '/Users/***/tools/keystore/***/***',
zhangsanSignStoreFile : '/Users/***/tools/keystore/lsjk/lsjkkeystore',
lisiSignStoreFile : '/Users/***/tools/keystore/lejianbang/lejianbangkeystore',
wangwuSignStoreFile : '/Users/***/tools/keystore/xingyi/xingyikeystore',
]
//打包版本配置
isTest = false
isRelease = false
// Sdk and tools
buildToolsVersion = '23.0.3'
compileSdkVersion = 24
minSdkVersion = 16
targetSdkVersion = 22
//版本号
versionCode = 61
versionName = "2.5.7"
javaVersion = JavaVersion.VERSION_1_7
// App dependencies version
supportLibraryVersion = "24.2.1"
constraintVersion = "1.0.2"
multidexVersion = "1.0.1"
//第三方库版本
arouterApiVersion = "1.3.1"
annotationProcessor = "1.1.4"
xutilsVersion = "3.5.0"
glideVersion = "3.7.0"
gsonVersion = "2.8.2"
fragmentationVersion = "1.1.9"
loggerVersion = "2.1.1"
rxjavaVersion = '1.1.0'
rxandroidVersion = '1.1.0'
okhttpVersion = '3.9.1'
okhttploggingVersion = '3.8.1'
retrofitVersion = '2.3.0'
}
2.在app module的build.gradle中使用变量:
appVersionCode = rootProject.ext.versionCode
appVersionName = rootProject.ext.versionName
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
signingConfigs {
releaseSignStoreFile{
keyAlias 'release'
keyPassword '12334'
storeFile file(rootProject.ext.storageFilePath.releaseSignStoreFile)
storePassword '12345'
}
zhangsanSignStoreFile {
keyAlias 'zhangsan'
keyPassword '11111'
storeFile file(rootProject.ext.storageFilePath.zhangsanSignStoreFile )
storePassword '22222'
}
lisiSignStoreFile {
keyAlias 'lisi'
keyPassword '212323'
storeFile file(rootProject.ext.storageFilePath.lisiSignStoreFile)
storePassword '123123123'
}
wangwuSignStoreFile {
keyAlias 'wangwu'
keyPassword '3234'
storeFile file(rootProject.ext.storageFilePath.wangwuSignStoreFile )
storePassword '3243423'
}
}
dependencies {
compile "io.reactivex:rxandroid:${rootProject.ext.rxandroidVersion}"
compile "io.reactivex:rxjava:${rootProject.ext.rxjavaVersion}"
}
也可以省去rootProject,在build.gradle中增加如下代码导入配置即可:
apply from: '../globalconfig.gradle'
如果上述变量直接定义在app的build.gradle中这句导入也不需要了。