Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Studio auto increment build number for version code
# First create the file version.properties with value VERSION_BUILD=0
# In app/build.gradle:
android {
def versionPropsFile = file('version.properties')
def versionBuild
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionProps['VERSION_BUILD'].toInteger()
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
ext.autoIncrementBuildNumber = {
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
versionProps['VERSION_BUILD'] = versionBuild.toString()
versionProps.store(versionPropsFile.newWriter(), null)
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
}
# Add / change this part in defaultConfig for the versionCode
defaultConfig {
versionCode versionBuild
}
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(assembleDebug)) {
autoIncrementBuildNumber()
} else if (taskGraph.hasTask(assembleRelease)) {
autoIncrementBuildNumber()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.