Skip to content

Instantly share code, notes, and snippets.

@mente
Created July 31, 2013 15:33
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mente/6123060 to your computer and use it in GitHub Desktop.
Save mente/6123060 to your computer and use it in GitHub Desktop.
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