2016-02-15 42 views
5

更具體地說,我有幾個構建CONFIGS:如何實現gradle代碼版本自動遞增?

signingConfigs { 
    debug { 
     keyAlias '' 
     keyPassword '' 
     storeFile file('') 
    } 
    release { 
     keyAlias '' 
     keyPassword '' 
     storeFile file('') 
     storePassword '' 
    } 
} 
.... 
defaultConfig { 
    applicationId "" 
    minSdkVersion 21 
    targetSdkVersion 23 
    versionCode code 
} 

我想gradle這個每一次「釋放」運行時爲自動增量代碼版本。

我有什麼至今:

def code = 1; 

//Get all the gradle task names being run 
List<String> runTasks = gradle.startParameter.getTaskNames(); 

for (String item : runTasks) { 

    //Get the version.properties file. Its our custom file for storing a code version, please don't remove it 
    def versionPropsFile = file('version.properties') 

    def Properties versionProps = new Properties() 

    //This will prevent the gradle from exploding when there's no file yet created 
    if (versionPropsFile.exists()) 
     versionProps.load(new FileInputStream(versionPropsFile)) 

    //It will insert the "0" version in case the file does not exist 
    code = (versionProps['VERSION_CODE'] ?: "0").toInteger() 

    if (item.contains("release")) { 
     // If we're building up on Jenkins, increment the version strings 
     code++ 

     versionProps['VERSION_CODE'] = code.toString() 

     //It will overwrite the file even if it doesn't exist 
     versionProps.store(versionPropsFile.newWriter(), null) 
    } 
} 

問題:

我似乎無法進去if (item.contains("release"))。它總是虛假的,但我絕對可以看到Gradle運行這個taks。我怎樣才能解決它或至少在控制檯輸出由gradle運行的所有任務(他們的名字)?

+1

這是一個重複http://stackoverflow.com/questions/23516090/android-studio-gradle-version-increment?rq=1? – k3b

+0

@ k3b感謝您指出這一點。在這篇文章中接受的解決方案不適合我。讓我再檢查一次。 –

回答

-2

混帳能有所幫助,請點擊此鏈接:Automatic versioning and increment using Git tags and Gradle

android { 
    defaultConfig { 
    ... 
     // Fetch the version according to git latest tag and "how far are we from last tag" 
     def longVersionName = "git -C ${rootDir} describe --tags --long".execute().text.trim() 
     def (fullVersionTag, versionBuild, gitSha) = longVersionName.tokenize('-') 
     def(versionMajor, versionMinor, versionPatch) = fullVersionTag.tokenize('.') 

     // Set the version name 
     versionName "$versionMajor.$versionMinor.$versionPatch($versionBuild)" 

     // Turn the version name into a version code 
     versionCode versionMajor.toInteger() * 100000 + 
       versionMinor.toInteger() * 10000 + 
       versionPatch.toInteger() * 1000 + 
       versionBuild.toInteger() 

     // Friendly print the version output to the Gradle console 
     printf("\n--------" + "VERSION DATA--------" + "\n" + "- CODE: " + versionCode + "\n" + 
       "- NAME: " + versionName + "\n----------------------------\n") 
    ... 
    } 
} 
+0

而不是張貼鏈接,發佈在這裏的實際內容。鏈接可能會下降。鏈接應僅用於參考。 – Mangesh

0

試試這個。我在我的所有應用程序中都使用它,並且工作正常。首先,在/ app /文件夾中創建一個version.properties文件。該文件應該如下所示。 此處VERSION_CODE表示您的應用中的versionCode字段。當VERSION_NAME在您的版本名稱中指示次要補丁時,這應該始終增加。 (例如x.x.12)。

/app/version.properties

VERSION_NAME=0  
VERSION_CODE=0 

然後在你的模塊級,的build.gradle文件中defaultConfig塊添加以下代碼。這會在每次發佈版本後將版本代碼和版本名稱增加1。 (基本上,當assambleRelease gradle這個任務的執行。如果您按照您的要求有不同的生成類型更改任務名稱。)

/app/build.gradle

//Version code increment 
    def versionPropsFile = file('version.properties') 
    if (versionPropsFile.canRead()) { 
     //load the version.properties file 
     def Properties versionProps = new Properties() 
     versionProps.load(new FileInputStream(versionPropsFile)) 

     /** 
     * get the name of currently running task 
     */ 
     def runTasks = gradle.startParameter.taskNames 

     /** 
     * Value to increment in minor version & version code. 
     */ 
     def incrementCount = 0 

     //Build version code and build type logic. 
     if (':storeFinder:assembleRelease' in runTasks) { 

      //if it is Production build package increment the version code by 1. 
      incrementCount = 1; 
     } 

     //generate new version code 
     def code = versionProps['VERSION_CODE'].toInteger() + incrementCount 
     def minorPatch = versionProps['VERSION_NAME'].toInteger() + incrementCount 

     //write new versionCode/version name suffix back to version.properties 
     versionProps['VERSION_CODE'] = code.toString() 
     versionProps['VERSION_NAME'] = minorPatch.toString() 
     versionProps.store(versionPropsFile.newWriter(), null) 

     //here version code is decided by above code. 
     //noinspection GroovyAssignabilityCheck 
     versionCode code 
     versionName "1.0." + minorPatch; //e.g. 1.0.61 

    } else { 
     //version.properties file not found. 
     throw new GradleException("Could not read version.properties! Copy that to /app folder from version control.") 
    } 
1

我實現這個問題的:

我有一個版本的文件,其中包含版本號。從這個文件我們得到的版本。當構建包含任務「publishRelease」(它可以是任何其他任務)時,我們增加版本文件中的版本。 我喜歡這種溶劑,因爲它保持了編碼邏輯的defaultConfig清潔。

的version.properties

VERSION_CODE=45 

,並在機器人的配置部分

defaultConfig { 
    applicationId "..." 
    minSdkVersion 16 
    targetSdkVersion 23 
    versionCode getVersion() 
    versionName "0.2." + versionCode 
} 

和getVersion()

def getVersion() { 
    def versionPropertiesFile = file('version.properties') 
    def appVersion = -1; 

    if (versionPropertiesFile.canRead()) { 
     def Properties versionProps = new Properties() 

     versionProps.load(new FileInputStream(versionPropertiesFile)) 

     appVersion = versionProps['VERSION_CODE'].toInteger() 

     def runTasks = gradle.startParameter.taskNames 
     if ('publishRelease' in runTasks) { 
      print("Increase version to " + appVersion + '\n') 

      appVersion += 1 

      versionProps['VERSION_CODE'] = appVersion.toString() 
      versionProps.store(versionPropertiesFile.newWriter(), null) 
     } 

    } else { 
     throw new GradleException("Could not read version.properties!") 
    } 

    return appVersion; 
} 
0

採取以下從Google員工Plaid應用代碼尼克布徹它真的很乾淨。

apply plugin: 'com.android.application' 

// query git for the SHA, Tag and commit count. Use these to automate versioning. 
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim() 
def gitTag = 'git describe --tags'.execute([], project.rootDir).text.trim() 
def gitCommitCount = 100 + Integer.parseInt('git rev-list --count HEAD'.execute([], project.rootDir).text.trim()) 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId 'net.hadifar.dope' 
     minSdkVersion 14 
     targetSdkVersion 23 
     versionName gitTag 
     versionCode gitCommitCount 
     buildConfigField "String", "GIT_SHA", "\"${gitSha}\"" 

    } 

    lintOptions { 
     abortOnError false 
    } 

    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_7 
     targetCompatibility JavaVersion.VERSION_1_7 
    } 

    buildTypes { 
     release { 
      minifyEnabled false 
      shrinkResources true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    productFlavors {} 
} 
ext { 
    supportLibVersion = '23.3.0' 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 

    compile "com.android.support:appcompat-v7:${supportLibVersion}" 
} 
+0

此代碼不會通過每個發行版本增加版本。它隨着每次提交而增加。但您可以修改以適應您的需求。 – Amir