Created
November 23, 2015 13:32
-
-
Save antonshkurenko/31cc22c86656e2862912 to your computer and use it in GitHub Desktop.
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
import java.util.regex.Pattern | |
task('increaseVersionCode') << { | |
def manifestFile = file("src/main/AndroidManifest.xml") | |
def pattern = Pattern.compile("versionCode=\"(\\d+)\"") | |
def manifestText = manifestFile.getText() | |
def matcher = pattern.matcher(manifestText) | |
matcher.find() | |
def versionCode = Integer.parseInt(matcher.group(1)) | |
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"") | |
manifestFile.write(manifestContent) | |
} | |
task('incrementVersionName') << { | |
def manifestFile = file("src/main/AndroidManifest.xml") | |
def patternVersionNumber = Pattern.compile("versionName=\"(\\d+)\\.(\\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 buildVersion = Integer.parseInt(matcherVersionNumber.group(4)) | |
def mNextVersionName = majorVersion + "." + minorVersion + "." + pointVersion + "." + (buildVersion + 1) | |
def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + mNextVersionName + "\"") | |
manifestFile.write(manifestContent) | |
} | |
tasks.whenTaskAdded { task -> | |
if (task.name == 'generateReleaseBuildConfig' || task.name == 'generateDebugBuildConfig') { | |
task.dependsOn 'increaseVersionCode' | |
task.dependsOn 'incrementVersionName' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/a/21951328/4142087