Android:添加日期/时间以gradle输出apk文件名

问题描述 投票:8回答:4

如何在Gradle Android输出文件名中添加日期/时间戳?

应该像project_v0.5.0_201503110212_public.apk

已经看过了

android gradle android-gradle
4个回答
12
投票

我假设你想要它以你指定的格式,所以这是一个可能的解决方案。

在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名称,但我想这足以帮助您。


2
投票

这段代码对我有用。

    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)
    }
}

0
投票

这是我的希望,帮助你

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"))
}

0
投票

我还为我的构建添加了格式化日期。首先,我使用new Date()的某种“现在”,但这在使用Android Studio开始构建时会遇到麻烦,就像上面的评论之一一样。我决定使用最新提交的时间戳。我在这里找到了一些灵​​感: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"))
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.