2013-10-04 17 views
50

Tôi đang sử dụng Android Studio và tôi cần phải thêm hậu tố vào versionNameSuffix trên tệp build.gradle Android của tôi. Tôi có ba buildTypes khác nhau và tôi chỉ cần thêm datetime để "beta" của tôi phát hành, tập tin thực tế của tôi là:cách chắp thêm ngày xây dựng lên versionNameSuffix trên gradle

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

để thử nghiệm và triển khai tự động, tôi cần để có được một tên phiên bản chính thức như: 0.7.5-beta-build20131004, 0.7.5-beta-build1380855996 hay đại loại thế. Bất kỳ ý tưởng?

Trả lời

118
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() { 
    new Date().format('yyyyMMddHHmmss') 
} 
+0

Làm thế nào để tạo ra apk sau configed các build.gradle? – herbertD

1

Tôi không quen thuộc với Android Studio, nhưng tôi cho rằng Gradle sẽ hoạt động như bình thường. Thêm một cái gì đó như thế này để cấu hình dự án xây dựng của bạn nên làm như lừa:

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

Bạn có thể xác định trong các chức năng tùy chỉnh build.gradle của bạn và các biến.

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

Sau đó, bạn có thể sử dụng nó:

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

hoặc nếu bạn muốn thêm nó vào bạn versionNameSuffix

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

versionNameSuffix đã bị hỏng trong ít nhất một năm – bluehallu

+0

'buildTime' được gạch chân: Các phương thức bên trong không được hỗ trợ. – CoolMind

8

Ngoài ra, đừng quên thêm nhập khẩu theo Gradle dòng đầu tiên:

import java.text.SimpleDateFormat; 
... 
0

Bạn có thể kiểm tra

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

Run gradle: gradle timenow

Xem chi tiết. Đặt nó trên cấp cao nhất xây dựng

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") 
    } 
} 
Các vấn đề liên quan