Last active
April 1, 2024 21:48
-
-
Save doridori/544c24509be236c11fd5 to your computer and use it in GitHub Desktop.
Incrementing android VersionCode via gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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