before
android.applicationVariants.all {
variant ->
variant.outputs.all {
def currentTime = new Date().format("MMdd_HHmmss")
//这里修改apk文件名
outputFileName = "xxxx-${variant.name}-${variant.versionName}-${variant.versionCode}-${currentTime}.apk"
}
}
after
androidComponents.onVariants { variant ->
val currentTime = SimpleDateFormat("MMdd_HHmmss", Locale.US).format(Date())
variant.outputs.forEach { output ->
if (output is com.android.build.api.variant.impl.VariantOutputImpl) {
// 过滤掉文件名中的非法字符
val safeVersionName = output.versionName.get().replace(Regex("[<>:\"/\\\\|?*]"), "_")
val safeVariantName = variant.name.replace(Regex("[<>:\"/\\\\|?*]"), "_")
val safeVersionCode = output.versionCode.orNull ?: 0 // 确保是 Int 类型
output.outputFileName = "xxxx-${safeVariantName}-${safeVersionName}-${safeVersionCode}-${currentTime}.apk"
}
}
}