I'm trying to setup a nice little versioning script in gradle, android studio where the version name increases every time I make a build, while the version code only increases when I make a release build. Is this possible?
What I think would solve it is checking in the if statement below if it is a release or not. But how can I check if it is a release?
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
def versionPropsFile = file('version.properties')
def code
def name
def Properties versionProps
if (versionPropsFile.canRead()) {
versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
code = versionProps['VERSION_CODE'].toInteger() + 1
name = versionProps['VERSION_NAME'].toInteger() + 1
versionProps['VERSION_CODE']=code.toString()
versionProps['VERSION_NAME']=name.toString()
versionProps.store(versionPropsFile.newWriter(), null)
defaultConfig {
versionCode code
versionName "1.2." + name
minSdkVersion 14
targetSdkVersion 19
}
} else {
throw new GradleException("Could not read version.properties!")
}
signingConfigs {
debug {
...
}
releaseKey {
...
}
}
buildTypes {
debug {
debuggable true
packageNameSuffix ".debug"
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.debug
}
release {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.releaseKey
}
}
lintOptions {
abortOnError false
}
}
I would like something like:
if (release) code = versionProps['VERSION_CODE'].toInteger() + 1
else code = versionProps['VERSION_CODE'].toInteger()
Any suggestions?
So after a few hours of trial and error I figured out a way this can be done.
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
def versionPropsFile = file('version.properties')
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
def value = 0
def runTasks = gradle.startParameter.taskNames
if ('assemble' in runTasks || 'assembleRelease' in runTasks || 'aR' in runTasks) {
value = 1;
}
def code = versionProps['VERSION_CODE'].toInteger() + value
def name = versionProps['VERSION_NAME'].toInteger() + 1
versionProps['VERSION_CODE']=code.toString()
versionProps['VERSION_NAME']=name.toString()
versionProps.store(versionPropsFile.newWriter(), null)
defaultConfig {
versionCode code
versionName "1.2." + name
minSdkVersion 14
targetSdkVersion 19
}
} else {
throw new GradleException("Could not read version.properties!")
}
signingConfigs {
debug {
...
}
releaseKey {
...
}
}
buildTypes {
debug {
...
}
release {
...
}
}
lintOptions {
abortOnError false
}
}
So what I'm doing is to check if I'm doing a assebleRelease task or not. If I am I increase the versionCode with +1.
Hope this helps anyone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With