【问题标题】:Android: add date/time to gradle output apk filenameAndroid:将日期/时间添加到 gradle 输出 apk 文件名
【发布时间】:2015-05-14 09:49:47
【问题描述】:

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

应该类似于 project_v0.5.0_201503110212_public.apk

已经看过了

【问题讨论】:

    标签: android gradle android-gradle-plugin


    【解决方案1】:

    您可以在位于android 部分的defaultConfig 部分中添加以下代码。

    setProperty("archivesBaseName", "yourData-$versionName " + (new Date().format("HH-mm-ss")))
    

    灵感来自enter link description here

    【讨论】:

      【解决方案2】:

      另一种解决方案是在defaultConfig 中设置$dateTime 属性,如下所示:

      defaultConfig {
              setProperty("archivesBaseName", "Appname-$dateTime-v$versionName")
          }
      
      

      【讨论】:

        【解决方案3】:

        我假设您希望它采用您指定的格式,所以这是一种可能的解决方案。

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

        【讨论】:

        • 使用这种方法会生成一个带有正确时间戳的 APK,但不知何故 Android Studio 会查找错误的名称,例如输出文件名为“my-app-debug-20160309_1649.apk”,但 AS 会查找“my-app-debug-20160309_1648.apk” - 关闭一分钟。知道发生了什么吗?
        • 我得到“无法为 ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=0.1 设置只读属性 'outputFile' 的值com.android.build.gradle.internal.api.ApkVariantOutputImpl 类型的}}。”同步 gradle 时出错
        【解决方案4】:

        这段代码对我有用。

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

        【讨论】:

        • 我得到“无法为 ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=0.1 设置只读属性 'outputFile' 的值com.android.build.gradle.internal.api.ApkVariantOutputImpl 类型的}}。”同步 gradle 时出错
        【解决方案5】:

        我还在我的构建中添加了一个格式化的日期。首先,我在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"))
            }
        }
        

        【讨论】:

          【解决方案6】:

          这是我的希望对你有所帮助

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

          【讨论】:

            猜你喜欢
            • 2011-07-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-14
            • 1970-01-01
            • 2018-03-11
            • 1970-01-01
            • 2016-12-27
            相关资源
            最近更新 更多