developer tip

gradle의 versionNameSuffix에 날짜 빌드를 추가하는 방법

optionbox 2020. 11. 16. 08:07
반응형

gradle의 versionNameSuffix에 날짜 빌드를 추가하는 방법


Android Studio를 사용 중이며 Android build.gradle 파일의 versionNameSuffix에 접미사를 추가해야합니다. 세 가지 서로 다른 buildType이 있고 "베타"릴리스에 datetime 만 추가하면됩니다. 실제 파일은 다음과 같습니다.

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

테스트 및 자동 배포, 나는 같은 최종 versionName을 얻을해야합니다 0.7.5-beta-build20131004, 0.7.5-beta-build1380855996또는 그런 일. 어떤 아이디어?


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 사용자 정의 함수 및 변수에서 정의 할 수 있습니다.

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

또는 versionNameSuffix에 추가하려면

beta {
    versionNameSuffix "-beta-build-${buildTime()}"      
}

또한 가져 오기를 Gradle 첫 번째 줄로 추가하는 것을 잊지 마십시오.

import java.text.SimpleDateFormat;
...

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

Android Studio에 익숙하지 않지만 Gradle이 정상적으로 작동한다고 가정합니다. 빌드 프로젝트 구성에 다음과 같이 추가하면 트릭을 수행 할 수 있습니다.

allProjects {
    gradle.taskGraph.whenReady { taskGraph ->
        versionNameSuffix += '-build' + // Java/Groovy code to produce the timestamp formatted the way you want
    }
}

테스트 할 수 있습니다.

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

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
}

참고URL : https://stackoverflow.com/questions/19172565/how-append-date-build-to-versionnamesuffix-on-gradle

반응형