In your build.gradle you want to update, I've changed a script to edit the current build.gradle and incrementing the versionCode and versionName.
Add this task to your gradle :
task incrementVersionCode << {
println("Incrementing Version Code...")
def manifestFile = file("build.gradle")
def patternVersionCode = Pattern.compile("versionCode (\\d+)")
def manifestText = manifestFile.getText()
def matcherVersionCode = patternVersionCode.matcher(manifestText)
matcherVersionCode.find()
def mVersionCode = Integer.parseInt(matcherVersionCode.group(1))
def mNextVersionCode = mVersionCode + 1
def manifestContent = matcherVersionCode.replaceAll("versionCode " + mNextVersionCode)
println("> Set versionCode(" + mVersionCode + ") to " + mNextVersionCode);
manifestFile.write(manifestContent)
}
task incrementVersionName << {
println("Incrementing Version Name...")
def manifestFile = file("build.gradle")
def patternVersionNumber = Pattern.compile("versionName \"(\\d+)\\.(\\d+)\\.(\\d+)\"")
def manifestText = manifestFile.getText()
def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
matcherVersionNumber.find()
def majorVersion = Integer.parseInt(matcherVersionNumber.group(1))
def minorVersion = Integer.parseInt(matcherVersionNumber.group(2))
def pointVersion = Integer.parseInt(matcherVersionNumber.group(3))
def mVersionName = majorVersion + "." + minorVersion + "." + pointVersion
def mNextVersionName = majorVersion + "." + minorVersion + "." + (pointVersion + 1)
def manifestContent = matcherVersionNumber.replaceAll("versionName \"" + mNextVersionName + "\"")
println("> Set versionName(" + mVersionName + ") to " + mNextVersionName);
manifestFile.write(manifestContent)
}
Usage
If you want to update version when running ./gradlew bintrayUpload
(for eg, mut it can be the task you want) add this :
bintrayUpload.dependsOn incrementVersionCode
bintrayUpload.dependsOn incrementVersionName