I'd like to apply different VersionCode to make apk file.
For debug only fix it to 1
, and for release whatever number specified in defaultConfig.
Below code gives mypackage-release-1.apk
file as assembleRelease artifact, which is not expected. I expected mypackage-release-10111.apk
for that.
why the line debug { defaultConfig.versionCode=1 }
affects assembleRelease artifact?
defaultConfig {
versionCode 10111
versionName '2.5.4'
minSdkVersion 10
targetSdkVersion 21
}
signingConfigs {
debug {
project.ext.loadSign = false
defaultConfig.versionCode = 1 // Why this value applied to assembleRelease?
}
release {
project.ext.loadSign = true
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionCode + ".apk"))
}
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
Me too, but I think
defaultConfig.versionCode
was set whenbuild.gradle
be compiling. It's global static variable, and assigned at compiletime, not runtime.I think we can intercept gradle task execution, and modify
defaultConfig.versionCode
at runtime.After goooooooogle, I found this one works for me: https://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2
Late on the party...
The entire gradle file evaluated before any task execution, so you are basically changing the default
versionCode
while declaringdebug
configs. There is no direct way to resetversionCode
frombuildType
, but the link on the other answer do the trick by declaring a task on build variants.To use with Flavors:
Here's an updated version: