92

I am using Android Studio and I need to append a suffix to the versionNameSuffix on my Android build.gradle file. I have three different build types and I only need to append the datetime to my "beta" release. My actual file is:

defaultConfig {
    versionCode 14
    versionName "0.7.5"
    minSdkVersion 9
    targetSdkVersion 18
}
buildTypes {
    beta {
        packageNameSuffix ".beta"
        versionNameSuffix "-beta"
        signingConfig signingConfigs.debug
    }
    ....
}

For testing and automatic deploy, I need to get a final versionName like: 0.7.5-beta-build20131004, 0.7.5-beta-build1380855996 or something like that. Any ideas?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Rodrigo Amaro Reveco
  • 4,932
  • 5
  • 22
  • 33

7 Answers7

199
beta {
    packageNameSuffix ".beta"
    versionNameSuffix "-beta" + "-build" + getDate()
    signingConfig signingConfigs.debug
}

def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmmss')
    return formattedDate
}

Condensed:

def getDate() {
    return new Date().format('yyyyMMddHHmmss')
}
exploitr
  • 843
  • 1
  • 14
  • 27
Rodrigo Amaro Reveco
  • 4,932
  • 5
  • 22
  • 33
39

You can define in your build.gradle custom functions and variables.

def versionMajor = 3

def buildTime() {
    def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'") // you can change it
    df.setTimeZone(TimeZone.getTimeZone("UTC"))
    return df.format(new Date())
}

Then you can use it:

android {
    defaultConfig {
       versionName "${versionMajor}-beta-build-${buildTime()}"
    }
}

or if you want to add it in you versionNameSuffix

beta {
    versionNameSuffix "-beta-build-${buildTime()}"      
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
9

Also, do not forget to add import as Gradle first line:

import java.text.SimpleDateFormat;
...
ivan.panasiuk
  • 1,239
  • 16
  • 20
5
for simple one row solution define this property above android section 

final BUILD_DATE = new Date().format('yyyy_MM_dd_HHmm')

and then 

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId APPLICATION_ID
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.compileSdkVersion
        versionName GIT_TAG_NAME
        versionCode GIT_COMMIT_COUNT
        setProperty("archivesBaseName",`enter code here` "com-appname-$BUILD_DATE-$versionName")
    }
}
yogaboy
  • 51
  • 1
  • 5
2

This is for Kotlin DSL (build.gradle.kts):

// At the top of the file
import java.time.LocalDate
import java.time.format.DateTimeFormatter.*

// ...

android {
    buildTypes {
        getByName("debug") { // OR simply  debug {  in newer versions of Android Gradle Plugin (AGP)
            val date = LocalDate.now().toString()
            // OR val date = LocalDate.now().format(ISO_LOCAL_DATE)
            // OR val date = LocalDate.now().format(ofPattern("yyyy-MM-dd"))
            versionNameSuffix = date
        }
    }
}

You could also extract the date creation to a function:

android {
    buildTypes {
        getByName("debug") {
            versionNameSuffix = generateDate()
        }
    }
}

fun generateDate() = LocalDate.now().toString()
// OR fun generateDate() = LocalDate.now().format(ISO_LOCAL_DATE)
// OR fun generateDate() = LocalDate.now().format(ofPattern("yyyy-MM-dd"))
Mahozad
  • 18,032
  • 13
  • 118
  • 133
1

I'm not familiar with Android Studio, but I'll assume Gradle behaves as it normally does. Adding something like this to your build project configuration should do the trick:

allProjects {
    gradle.taskGraph.whenReady { taskGraph ->
        versionNameSuffix += '-build' + // Java/Groovy code to produce the timestamp formatted the way you want
    }
}
030
  • 10,842
  • 12
  • 78
  • 123
Jamie Bisotti
  • 2,605
  • 19
  • 23
0

You can test

task timenow {
    println(new Date().getTime())
}

Run gradle: gradle timenow

See details. Place it on the top-level build

ext {
    configuration = [
            appName          : "vBulletin",
            applicationId    : "com.vbulletin",
            minSdkVersion    : 14,
            targetSdkVersion : 19,
            compileSdkVersion: 19,
            versionCode      : 6,
            versionName      : "1.3.6",
            buildToolsVersion: "25.0.0",
    ]

}

task createBrand {
    appConfig.applicationId = appConfig.applicationId + ".${brand}"
    appConfig.versionCode = new Date().getTime()
    appConfig.versionName = version
}
Tuan Nguyen
  • 2,542
  • 19
  • 29