Created
November 23, 2017 09:13
-
-
Save tranquan/d6f9dd88058154e1b30519d9e5aa1406 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
/** | |
* Auto increment versionCode for every build | |
* read version.properties in android root directory and auto increment build number on release | |
*/ | |
def readAndIncrementVersionCode() { | |
def isRelease = false | |
List<String> runTasks = gradle.startParameter.getTaskNames(); | |
for (String item : runTasks) { | |
if (item.contains("assemble") && item.contains("Release")) { | |
isRelease = true; | |
} | |
} | |
def versionFile = new File(project.rootDir, 'version.properties') | |
// read | |
def version = new Properties() | |
def stream | |
try { | |
stream = new FileInputStream(versionFile) | |
version.load(stream) | |
} catch (FileNotFoundException ignore) { | |
println "version.properties not found" | |
} finally { | |
if (stream != null) stream.close() | |
} | |
def versionCode = version['versionCode'] ? version['versionCode'] as int : 0 | |
// increment by 1 in release | |
if (isRelease) { | |
versionCode += 1 | |
version['versionCode'] = versionCode.toString() | |
// write | |
def outStream = new FileOutputStream(versionFile) | |
try { | |
version.store(outStream, null) | |
} finally { | |
stream.close() | |
} | |
} | |
println "version code is now " + versionCode | |
return versionCode | |
} | |
/** | |
* compileSdkVersion, buildToolsVersion, minSdkVersion, targetSdkVersion | |
* should be matched with 0-scripts/configure_gradle.py | |
*/ | |
android { | |
... | |
defaultConfig { | |
applicationId "..." | |
minSdkVersion 19 | |
targetSdkVersion 20 | |
versionCode readAndIncrementVersionCode() | |
versionName "1.0.0" | |
ndk { | |
abiFilters "armeabi-v7a", "x86" | |
} | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment