Skip to content

Instantly share code, notes, and snippets.

@doridori
Last active April 1, 2024 21:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save doridori/544c24509be236c11fd5 to your computer and use it in GitHub Desktop.
Save doridori/544c24509be236c11fd5 to your computer and use it in GitHub Desktop.
Incrementing android VersionCode via gradle
//in your root build.gradle
//--------- Version Increment ----------//
//can call this like `./gradlew incrementVersionCode build`
task incrementVersionCode {
description = "Increments the version code in the version.properties file"
doLast {
File 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)
println "VERSION_CODE incremented to "+code
} else {
throw new GradleException("Could not read version.properties!")
}
}
}
//can call this from your android DSL
int getIncrementingVersionCode() {
def versionPropsFile = file('version.properties')
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
def code = versionProps['VERSION_CODE'].toInteger()
return code
} else {
throw new GradleException("Could not read version.properties!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment