Created
June 11, 2018 14:54
-
-
Save marcandreappel/722337bee66d04c538ce52c74d983cb4 to your computer and use it in GitHub Desktop.
Android Studio auto increment build number for version code
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
# 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() | |
} | |
} | |
} |
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
Thank you so much!
I like to use bundleRelease instead of assembleDebug to increment whenever I build signed release bundle :)