Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save marcandreappel/722337bee66d04c538ce52c74d983cb4 to your computer and use it in GitHub Desktop.
Save marcandreappel/722337bee66d04c538ce52c74d983cb4 to your computer and use it in GitHub Desktop.
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()
}
}
}
@Martin-Eckleben
Copy link

Martin-Eckleben commented Oct 25, 2023

Thank you so much!

I like to use bundleRelease instead of assembleDebug to increment whenever I build signed release bundle :)

@Martin-Eckleben
Copy link

It doesn't do it consistently and I have no clue why RN - will research a little more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment