-
-
Save lifuzu/7797669 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
android { | |
defaultConfig { | |
versionName "actionbar" | |
} | |
signingConfigs { | |
release { | |
storeFile file("android.keystore") | |
... | |
} | |
} | |
sourceSets { | |
main { | |
manifest.srcFile 'AndroidManifest.xml' | |
res.srcDirs = ['res'] | |
assets.srcDirs = ['assets'] | |
} | |
} | |
buildTypes { | |
debug { | |
versionNameSuffix "dev" | |
} | |
release { | |
signingConfig signingConfigs.release | |
} | |
} | |
buildToolsVersion "17.0" | |
compileSdkVersion 17 | |
} | |
def getVersionCode(manifestLocation, increment = false) { | |
def manifestFile = file(manifestLocation) | |
if (!manifestFile.exists()) { | |
return -1 | |
} | |
def pattern = Pattern.compile("versionCode=\"(\\d+)\"") | |
def manifestText = manifestFile.getText() | |
def matcher = pattern.matcher(manifestText) | |
matcher.find() | |
def versionCode = Integer.parseInt(matcher.group(1)) | |
if (increment) { | |
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"") | |
manifestFile.write(manifestContent) | |
} | |
return versionCode | |
} | |
task('increaseVersionCode') << { | |
getVersionCode("AndroidManifest.xml", true) | |
} | |
tasks.whenTaskAdded { task -> | |
if (task.name == 'generateReleaseBuildConfig') { | |
task.dependsOn 'increaseVersionCode' | |
} | |
} | |
android.applicationVariants.each { variant -> | |
def apk = variant.outputFile | |
def newManifest = variant.processManifest.manifestOutputFile | |
def versionCode = getVersionCode(newManifest.path) | |
if (versionCode == -1) { | |
return | |
} | |
def newName | |
if (variant.buildType.versionNameSuffix) { | |
newName = "Acme-v${versionCode}-${android.defaultConfig.versionName}-${variant.buildType.versionNameSuffix}.apk" | |
} else { | |
newName = "Acme-v${versionCode}-${android.defaultConfig.versionName}.apk" | |
} | |
//noinspection GroovyAssignabilityCheck | |
variant.outputFile = new File(apk.parentFile, newName) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment