12

How to add date/time stamp to Gradle Android output file name?

Should be like project_v0.5.0_201503110212_public.apk

Already looked at

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332

6 Answers6

22

I'm assuming that you want it in the format you specified, so here's one possible solution.

In your gradle file you can define a new function to get the date time string like you desire:

import java.text.DateFormat
import java.text.SimpleDateFormat

def getDateTime() {
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");

    return df.format(new Date());
}

Then for all variants you can simply run this:

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

Note that this doesn't really output the apk name like you posted, but I guess it's enough to help you out.

Alex
  • 139
  • 9
Fred
  • 16,367
  • 6
  • 50
  • 65
  • 4
    Using this approach generates an APK with a proper timestamp, but somehow Android Studio looks for the wrong name, e.g. the output file name is "my-app-debug-20160309_1649.apk", but AS looks for "my-app-debug-20160309_1648.apk" - off by a minute. Any idea what's going on? – npace Mar 09 '16 at 14:51
  • 1
    I get "Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=0.1}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl." error when syncing gradle – Gavriel Jan 01 '19 at 01:32
7

This code working for me.

    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)
    }
}
DynamicMind
  • 4,240
  • 1
  • 26
  • 43
  • 5
    I get "Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=0.1}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl." error when syncing gradle – Gavriel Jan 01 '19 at 01:32
1

I also add a formatted date to my build. In first place I used some kind of "now" with new Date(), but this leads to trouble when starting the build with Android Studio, like in one of the comments above. I decided to use the timestamp of the latest commit. I found some inspiration here: https://jdpgrailsdev.github.io/blog/2014/10/14/spring_boot_gradle_git_info.html

Adding the timestamp is handled like below:

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

My renaming part looks like this:

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"))
    }
}
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
1

An alternative solution is to set $dateTime property in defaultConfig as shown below:

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

0

This is mine hope to help you

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

You can just add the code below inside the defaultConfig section located in android section.

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

Inspired by enter link description here

Hadi
  • 544
  • 1
  • 8
  • 28