如何在Gradle中将日期构建附加到versionNameSuffix?

92

我正在使用Android Studio,我需要在我的Android build.gradle文件中的versionNameSuffix后添加一个后缀。我有三种不同的构建类型,只需要将日期时间附加到我的“beta”版本。我的实际文件是:

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

为了测试和自动部署,我需要获取一个最终的版本名称,类似于:0.7.5-beta-build201310040.7.5-beta-build1380855996或类似的东西。有什么想法吗?

7个回答

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

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

精简版:

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

如何在配置了build.gradle文件后生成apk? - herbertD
生成 APK 的步骤是什么? - Bruno Sosa Fast Tag

39
你可以在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())
}

那么你可以使用它:
android {
    defaultConfig {
       versionName "${versionMajor}-beta-build-${buildTime()}"
    }
}

或者,如果您想将其添加到版本名称后缀中。
beta {
    versionNameSuffix "-beta-build-${buildTime()}"      
}

versionNameSuffix 至少已经损坏了一年。 - bluehallu
1
“buildTime”被下划线标记:不支持内部方法。 - CoolMind
你如何导入Date()? - simpleuser

9
此外,不要忘记在Gradle的第一行添加import。
import java.text.SimpleDateFormat;
...

2
避免烦恼,使用Date().format("yyyyMMdd'T'HHmmssZ")。 - kbro

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

3
这是针对 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
        }
    }
}

您可以将创建日期提取到一个函数中:
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"))

1
我不熟悉Android Studio,但我会假设Gradle的行为与通常一样。将类似以下内容添加到您的项目构建配置中应该可以解决问题:
allProjects {
    gradle.taskGraph.whenReady { taskGraph ->
        versionNameSuffix += '-build' + // Java/Groovy code to produce the timestamp formatted the way you want
    }
}

0

你可以进行测试

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

运行Gradle:gradle timenow

查看详细信息。将其放置在顶层构建中

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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接