Skip to content

Instantly share code, notes, and snippets.

@Stormwind99
Last active July 8, 2021 10:08
Show Gist options
  • Save Stormwind99/f71b3ff34a76090f5a9bf4b057bf14dd to your computer and use it in GitHub Desktop.
Save Stormwind99/f71b3ff34a76090f5a9bf4b057bf14dd to your computer and use it in GitHub Desktop.
Gradle automated build number incrementing
def versionFile = file('version.properties')
ext.buildnum = new Properties([BUILD_NUMBER: "0"]);
if (versionFile.isFile()) {
versionFile.withReader { buildnum.load(it) }
}
def buildNumber = Integer.toString(Integer.parseInt(buildnum.BUILD_NUMBER) + 1)
buildnum.BUILD_NUMBER = buildNumber
task('saveBuildNumber') << {
versionFile.withWriter { buildnum.store(it, "Automated build number increase") }
}
tasks.whenTaskAdded { task ->
if (task.name == 'build') {
task.dependsOn 'saveBuildNumber'
}
}
#Automated build number increase
#Thu Jul 05 19:21:05 CDT 2018
BUILD_NUMBER=1
@Ananthakr
Copy link

Ananthakr commented Jan 24, 2020

In Line 9, << operator has been deprecated in gradle 4.0, so please replace it with doLast(Ref. https://stackoverflow.com/questions/55793095/could-not-find-method-leftshift-for-arguments-after-updating-studio-3-4)

Also, it would be nice if you add the way to read the values in build.gradle

Thanks for sharing :)

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