2013-10-04 12 views
50

Sto utilizzando Android Studio e devo aggiungere un suffisso a versionNameSuffix sul mio file build.gradle per Android. Ho tre diverse buildTypes e ho solo bisogno di aggiungere il datetime al mio rilascio "beta", il mio file reale è:come aggiungere la data build a versionNameSuffix sul gradle

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

per il test e deploy automatico, ho bisogno di ottenere un versionName finale come: 0.7.5-beta-build20131004, 0.7.5-beta-build1380855996 o qualcosa di simile. Qualche idea?

risposta

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

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

abbreviato:

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

come generare apk dopo ConfigEd il build.gradle? – herbertD

1

Non ho familiarità con Android Studio, ma presumo che Gradle si comporti come al solito. L'aggiunta di qualcosa di simile alla configurazione del progetto di costruzione dovrebbe fare il trucco:

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

È possibile definire nelle vostre funzioni personalizzate build.gradle e variabili.

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

Quindi si può utilizzare:

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

o se si desidera aggiungere in voi versionNameSuffix

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

versionNameSuffix è stato interrotto per almeno un anno – bluehallu

+0

'buildTime' è sottolineato: i metodi interni non sono supportati. – CoolMind

8

Inoltre, non dimenticare di aggiungere l'importazione come prima linea Gradle:

import java.text.SimpleDateFormat; 
... 
0

È possibile testare

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

Run Gradle: TimeNow Gradle

vedere i dettagli. Posizionarlo sul livello superiore costruire

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 
} 
1
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") 
    } 
} 
Problemi correlati