Android build.gradle实现打包动态配置apk名称
//定义方法,获取打包时间
def releaseTime(){
return new Date().format("yyyyMMddHHmm")
}
//定义全局变量
ext{
isTestUrl = "false"
}
//获取环境信息,测试环境则apk名称中添加“_测试环境”标识,正式环境无标识
def runMode(){
if (isTestUrl == "true"){
return "_测试环境"
}else {
return ""
}
}
android {
defaultConfig {
...
}
signingConfigs {
release {
...
}
debug {
...
}
}
buildTypes {
release {
...
//在java中,使用BuildConfig.IS_TEST_URL获取布尔值
buildConfigField "boolean", "IS_TEST_URL", "$isTestUrl"
}
debug {
...
buildConfigField "boolean", "IS_TEST_URL", "$isTestUrl"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
android.applicationVariants.all {
variant ->
variant.outputs.all {
//在这里修改apk文件名: APP_测试环境_release_v1.0.1_c2_202004221706.apk
outputFileName = "APP_NAME${runMode()}_${variant.buildType.name}_v${variant.versionName}_c${variant.versionCode}_${releaseTime()}.apk"
}
}
}
dependencies {
...
}