Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: add date/time to gradle output apk filename

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

  • how append date build to versionNameSuffix on gradle
  • How to pass arguments from command line to gradle
like image 705
Paul Verest Avatar asked Mar 12 '15 09:03

Paul Verest


4 Answers

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)
    }
}
like image 53
DynamicMind Avatar answered Nov 06 '22 00:11

DynamicMind


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"))
    }
}
like image 30
Thomas R. Avatar answered Nov 06 '22 02:11

Thomas R.


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.

like image 20
Fred Avatar answered Nov 06 '22 00:11

Fred


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

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

like image 1
Kipngetich Yegon Avatar answered Nov 06 '22 01:11

Kipngetich Yegon