搜尋此網誌

2016年6月12日 星期日

【Android】Autoincrement VersionCode with gradle extra properties

ref:
http://stackoverflow.com/a/21405744
https://github.com/commonsguy/cw-omnibus/tree/master/Gradle/HelloVersioning


down voteaccepted
I would like to read the versionCode from an external file
I am sure that there are any number of possible solutions; here is one:
android {
    compileSdkVersion 18
    buildToolsVersion "18.1.0"

    def versionPropsFile = file('version.properties')

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

        versionProps.load(new FileInputStream(versionPropsFile))

        def code = versionProps['VERSION_CODE'].toInteger() + 1

        versionProps['VERSION_CODE']=code.toString()
        versionProps.store(versionPropsFile.newWriter(), null)

        defaultConfig {
            versionCode code
            versionName "1.1"
            minSdkVersion 14
            targetSdkVersion 18
        }
    }
    else {
        throw new GradleException("Could not read version.properties!")
    }

    // rest of android block goes here
}
This code expects an existing version.properties file, which you would create by hand before the first build to have VERSION_CODE=8.

沒有留言: