Reputation: 15831
I need to create auto-increment version mechanism via Gradle. For this I use this manual and create two files
#Fri May 20 11:04:03 EEST 2016
VERSION_BUILD=3
buildTypes {
debug{
versionNameSuffix 'debug';
}
release {
def versionPropsFile = file('version.properties')
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
versionProps['VERSION_BUILD'] = versionBuild.toString()
versionProps.store(versionPropsFile.newWriter(), null)
versionNameSuffix = versionBuild;
} else {
throw new GradleException("Could not read version.properties!")
}
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
The my problem is : VERSION_BUILD
increments each time when I build project. Not a Run
, just a build. And don't matter I Run project or create a signed APK.
The my goal - increment version only when I generate signed APK. How I can do that?
Upvotes: 0
Views: 1068
Reputation: 15831
Finally, I found solution.
List<String> runTasks = gradle.startParameter.getTaskNames();
for (String item : runTasks) {
if (item.contains("assemble") && item.contains("Release")) {
// this is a release task
}
}
Upvotes: 5
Reputation: 5839
From your gradle file,everytime you run a rel build you sign the app. So Why not use versionCode instead of VERSION_BUILD, that way it will only happen when you build.
Upvotes: 0