Home > Enterprise >  Android build gradle file version code auto increment
Android build gradle file version code auto increment

Time:09-14

How can I automatically increase the version number in the android build gradle file every time I get a release package

CodePudding user response:

you can look this article :

Auto-increment versionCode using Gradle

CodePudding user response:

This is rather useless when building locally, because you'll never be able to reproduce the build, despite knowing the build number. While the build number will always be provided when building in a CI environment, alike Jenkins or GitHub Actions, Cloud Build, etc. For example, one just has to pick it up with System.getenv() (where the variable name may vary):

/** $BUILD_NUMBER only exists when building with Jenkins */
if (System.getenv("BUILD_NUMBER") != null ) {
    project.ext.set('buildNumber', new Integer(System.getenv("BUILD_NUMBER")))
} else {
    project.ext.set('buildNumber', 0)
}

CodePudding user response:

You can add Version details in the version.properties file. But, I recommended you can update the version code and version number manual because each time you build it increased by one. One more thing with that if you want to release a major update then you also need to update manually. In conclusion, the manually update version provides more controls on your updates.

compileSdkVersion 25
buildToolsVersion "25.0.2"

def versionPropsFile = file('version.properties')

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

    versionProps.load(new FileInputStream(versionPropsFile))
    def name = versionProps['VERSION_NAME']
    def code = versionProps['VERSION_CODE'].toInteger()   1
    versionProps['VERSION_CODE']=code.toString()
    versionProps.store(versionPropsFile.newWriter(), null)

    defaultConfig {
        applicationId "com.bsupits.example.autoincrement"
        versionName name
        versionCode code
        minSdkVersion 15
        targetSdkVersion 25
    }
}
else {
    throw new GradleException("Could not read version.properties!")
}
  • Related