【发布时间】:2015-05-14 09:49:47
【问题描述】:
如何在 Gradle Android 输出文件名中添加日期/时间戳?
应该类似于 project_v0.5.0_201503110212_public.apk
已经看过了
【问题讨论】:
标签: android gradle android-gradle-plugin
如何在 Gradle Android 输出文件名中添加日期/时间戳?
应该类似于 project_v0.5.0_201503110212_public.apk
已经看过了
【问题讨论】:
标签: android gradle android-gradle-plugin
您可以在位于android 部分的defaultConfig 部分中添加以下代码。
setProperty("archivesBaseName", "yourData-$versionName " + (new Date().format("HH-mm-ss")))
【讨论】:
另一种解决方案是在defaultConfig 中设置$dateTime 属性,如下所示:
defaultConfig {
setProperty("archivesBaseName", "Appname-$dateTime-v$versionName")
}
【讨论】:
我假设您希望它采用您指定的格式,所以这是一种可能的解决方案。
在您的 gradle 文件中,您可以定义一个新函数来获取所需的日期时间字符串:
import java.text.DateFormat
import java.text.SimpleDateFormat
def getDateTime() {
DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
return df.format(new Date());
}
然后对于所有变体,您可以简单地运行:
android {
//...
buildTypes {
//...
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + getDateTime() + ".apk"))
}
}
}
}
请注意,这并不会像您发布的那样真正输出 apk 名称,但我想这足以帮助您。
【讨论】:
这段代码对我有用。
applicationVariants.all { variant ->
variant.outputs.each { output ->
def project = "Your App Name"
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType = variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def date = new Date();
def formattedDate = date.format('ddMMyy_HHmm')
def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
output.outputFile = new File(output.outputFile.parent, newApkName)
}
}
【讨论】:
我还在我的构建中添加了一个格式化的日期。首先,我在new Date() 中使用了某种“现在”,但这会在使用 Android Studio 开始构建时导致麻烦,就像在上面的一个 cmets 中一样。我决定使用最新提交的时间戳。我在这里找到了一些灵感:https://jdpgrailsdev.github.io/blog/2014/10/14/spring_boot_gradle_git_info.html
添加时间戳的处理方式如下:
def getLatestCommitTimeStamp() {
def revision = 'git rev-list --max-count 1 --timestamp HEAD'.execute().text.trim()
def gitCommitMillis = java.util.concurrent.TimeUnit.SECONDS.toMillis(revision.split(' ').first() as long)
return new Date(gitCommitMillis).format("_HH.mm.ss_dd-MM-yyyy", TimeZone.getTimeZone('Europe/Berlin'))
}
我的重命名部分如下所示:
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def lastCommitFormattedDate = getLatestCommitTimeStamp()
variant.outputs.each { output ->
def alignedOutputFile = output.outputFile
def unalignedOutputFile = output.packageApplication.outputFile
// Customise APK filenames (to include build version)
if (variant.buildType.zipAlignEnabled) {
// normal APK
output.outputFile = new File(alignedOutputFile.parent, alignedOutputFile.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + "-${gitSha}" + lastCommitFormattedDate + ".apk").replace("-" + variant.buildType.name, "").replace(project.name, "otherName"))
}
// 'unaligned' APK
output.packageApplication.outputFile = new File(unalignedOutputFile.parent, unalignedOutputFile.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + "-${gitSha}" + lastCommitFormattedDate + ".apk").replace("-" + variant.buildType.name, "").replace(project.name, "otherName"))
}
}
【讨论】:
这是我的希望对你有所帮助
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(
(String) file.parent,
(String) file.name.replace(
file.name,
// alter this string to change output file name
"APigLive_Android_" + variant.name + "_" + variant.versionName + "_" + releaseTime() + ".apk"
)
)
}
}
def releaseTime(){
return new Date().format("MM:dd:HH:mm", TimeZone.getTimeZone("GMT"))
}
【讨论】: