Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how append date build to versionNameSuffix on gradle

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?

like image 959
Rodrigo Amaro Reveco Avatar asked Oct 04 '13 03:10

Rodrigo Amaro Reveco


People also ask

How do I update build Gradle dependencies?

Go to Android Studio -> Preferences -> Plugins (for Mac) and File -> Settings -> Plugins (for windows) and search “Check for Dependency updates plugin”. Install it and restart android studio. You will be able to see Dependencies tab on the right which will show if any dependency has a new update available.

How do I sync build Gradle files?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

How do I add a dependency in build Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What is up to date in Gradle?

Gradle will determine if a task is up to date by checking the inputs and outputs. For example, your compile task input is the source code. If the source code hasn't changed since the last compile, then it will check the output to make sure you haven't blown away your class files generated by the compiler.


5 Answers

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')
}
like image 163
Rodrigo Amaro Reveco Avatar answered Oct 03 '22 09:10

Rodrigo Amaro Reveco


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()}"      
}
like image 33
Gabriele Mariotti Avatar answered Oct 03 '22 07:10

Gabriele Mariotti


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

import java.text.SimpleDateFormat;
...
like image 40
ivan.panasiuk Avatar answered Oct 03 '22 09:10

ivan.panasiuk


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")
    }
}
like image 40
yogaboy Avatar answered Oct 03 '22 09:10

yogaboy


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
    }
}
like image 30
Jamie Bisotti Avatar answered Oct 03 '22 08:10

Jamie Bisotti