Created
March 14, 2017 22:12
-
-
Save taketime/bda0e93e79d9323e7d218894e9ef5543 to your computer and use it in GitHub Desktop.
Gradle config for Auto-incrementing a build number/versionCode. Look at bumpBuildNumber()
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 { | |
compileSdkVersion 25 | |
buildToolsVersion "25.0.2" | |
defaultConfig { | |
applicationId "com.example.coolproject" | |
minSdkVersion 19 | |
targetSdkVersion 25 | |
versionCode bumpBuildNumber() // Increments a build number; uses gradle.properties for storage | |
versionName "0.0.4" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
debug { | |
applicationVariants.all { variant -> | |
buildAPK(variant, defaultConfig, 'debug') | |
} | |
} | |
} | |
} | |
def buildAPK(variant, defaultConfig, buildType) { | |
variant.outputs.each { output -> | |
def file = output.packageApplication.outputFile | |
def date = new Date().format('YYYY-MM-dd') | |
def fileName = defaultConfig.applicationId + '-' + defaultConfig.versionName + '-(' + defaultConfig.versionCode + ')-'+ buildType + '-' + date + '.apk' | |
output.packageApplication.outputFile = new File(file.parent, fileName); | |
} | |
} | |
// Update version number, so we get a build named versionName (build number) example: 0.0.4 (27) | |
// Intermediate build number storage is in gradle.properties as buildNumber | |
// updates gradle.properties buildNumber += 1; inserts buildNumber = 0 if prop wasn't present | |
// returns String of gradle.properties buildNumber + 1; | |
def bumpBuildNumber() { | |
def propsFile = file(System.getProperty('user.dir') + '/gradle.properties'); | |
if (propsFile.canRead() && propsFile.canWrite()) { | |
def Properties props = new Properties() | |
props.load(new FileInputStream(propsFile)) | |
def nextBuild = props.buildNumber | |
if (nextBuild == null) { | |
nextBuild = 0 | |
} else { | |
nextBuild = nextBuild.toInteger() + 1 | |
} | |
props.buildNumber = nextBuild.toString() | |
props.store(propsFile.newWriter(), null) | |
return nextBuild | |
} else { | |
throw new GradleException('Cannot read gradle.properties') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment