如何在gradle上附加日期构建到versionNameSuffix

Rod*_*eco 65 android gradle android-studio build.gradle android-gradle-plugin

我正在使用Android Studio,我需要在Android build.gradle文件的后面添加一个后缀到versionNameSuffix.我有三种不同的buildTypes,我只需要将日期时间附加到我的"beta"版本,我的实际文件是:

defaultConfig {
    versionCode 14
    versionName "0.7.5"
    minSdkVersion 9
    targetSdkVersion 18
}
buildTypes {
    beta {
        packageNameSuffix ".beta"
        versionNameSuffix "-beta"
        signingConfig signingConfigs.debug
    }
    ....
}
Run Code Online (Sandbox Code Playgroud)

对于测试和自动部署,我需要获得最终版本名称0.7.5-beta-build20131004,如:0.7.5-beta-build1380855996或类似的东西.有任何想法吗?

Rod*_*eco 153

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

def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmmss')
    return formattedDate
}
Run Code Online (Sandbox Code Playgroud)

凝结:

def getDate() {
    return new Date().format('yyyyMMddHHmmss')
}
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 36

您可以在build.gradle中定义自定义函数和变量.

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())
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用它:

android {
    defaultConfig {
       versionName "${versionMajor}-beta-build-${buildTime()}"
    }
}
Run Code Online (Sandbox Code Playgroud)

或者如果你想在你的versionNameSuffix中添加它

beta {
    versionNameSuffix "-beta-build-${buildTime()}"      
}
Run Code Online (Sandbox Code Playgroud)


iva*_*iuk 9

另外,不要忘记将导入添加为Gradle第一行:

import java.text.SimpleDateFormat;
...
Run Code Online (Sandbox Code Playgroud)

  • 或避免悲伤并使用 Date().format("yyyyMMdd'T'HHmmssZ") (2认同)

yog*_*boy 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")
    }
}
Run Code Online (Sandbox Code Playgroud)