We have a release
and debug
buildType and want to set the versionCode
and versionName
to a constant value for debug
as otherwise, every build repackages the apk, even without code changes.
So we set a fixed default versionCode
and later override it for a specific buildType:
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.gradletest"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
applicationVariants.all { variant ->
if (!variant.buildType.isDebuggable()) {
variant.outputs.each { output ->
output.versionCodeOverride = getAppVersionCode()
output.versionNameOverride = getAppVersionName()
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.debug
debuggable true
}
}
}
while this works for the apk, unfortunately the generated class BuildConfig
always has the default 1/"1.0"
value. We read and show the versionNumber
from within the app as this popular answer suggests.
Apparently BuildConfig.java
is generated at configuration, not at project build, so it can't know the chosen variant. How to handle this?
Our getAppVersionCode()
is containing a timestamp, so every versionCode is different. I tried flipping the statements, so that the debug builds would show a different versionCode and versionName everytime, which would be fine for us.
android {
defaultConfig {
versionCode getAppVersionName()
versionName getAppVersionCode()
}
applicationVariants.all { variant ->
if (variant.buildType.isDebuggable()) {
variant.outputs.each { output ->
output.versionCodeOverride = 1
output.versionNameOverride = "1.0"
}
}
}
}
The reason why we have a fixed debug versionCode
in the first place is that we don't want to rebuild all submodules for every code change. With the second variant, even though we set a fixed versionNumber
for the debug build through output.versionCodeOverride = 1
all submodules are rebuild by Android Studio. Before Android Studio 3.0 this worked but doesn't anymore. Please advise.