1.在module的build.gradle的android節點下添加以下內容
applicationVariants.all {
variant ->
variant.outputs.all {
output ->
def outputFile = output.outputFile
def fileName
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {
fileName = "app_v${defaultConfig.versionName}_${builderTime()}_release.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "app_v${defaultConfig.versionName}_${builderTime()}_debug.apk"
}
outputFileName = fileName
}
}
}
2.其中獲取時間的方法
定義在根節點(與android節點同級)
def builderTime() {
return new Date().format("yyyyMMdd_HH-mm", TimeZone.getTimeZone("GMT+8"))
}
3.AS3.0以下配置打包文件名
applicationVariants.all {
variant ->
variant.outputs.each {
output ->
def outputFile = output.outputFile
def fileName
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {
fileName = "app_v${defaultConfig.versionName}_${builderTime()}_release.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "app_v${defaultConfig.versionName}_${builderTime()}_debug.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
此配置在AS3.0以上會報錯,錯誤爲:Cannot set the value of read-only property 'outputFile'
對比需要修改的地方: