I was trying to do white labeling for my app but I am stuck at a point where I need to use configuration based on buildType + flavor combination.
i.e let’s assume I have 2 buildTypes (debug & release) and 2 flavors (flavor1 & flavor2), I would like to use 4 different keys based on the combination as follows:
- flavor1 + debug = key1
- flavor1 + release = key2
- flavor2 + debug = key3
- flavor2 + release = key4
how do I achieve the same?
below is my build.gradle file.
buildTypes {
getByName("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = ".debug"
buildConfigField("boolean", "DEBUG_MODE", "true")
}
getByName("release") {
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
buildConfigField("boolean", "DEBUG_MODE", "false")
}
}
flavorDimensions("version")
productFlavors {
create("flavor1") {
setDimension("version")
}
create("flavor2") {
setDimension("version")
versionCode = 1
versionName = "1.0"
}
}
@iaaqibhussain Can you please help with this when you get a chance? Thank you - much appreciated! :]
Hi,
I don’t clearly understand your question that what you mean by keys. if that are some hardcoded keys you want to assign what you can do is update your gradle code to this:
productFlavors {
animaldom { ext {
variable = [debug: "key-1", release: "key-2"]
} }
}
catsPlanet {
dimension 'version'
applicationId "com.raywenderlich.android.catsPlanet"
ext {
variable = [debug: "key-3", release: "key-4"]
}
}
}
`
Under it also add:
applicationVariants.all { variant ->
def flavor = variant.productFlavors[0]
variant.buildConfigField "String", "KEY", "\"${flavor.variable[variant.buildType.name]}\""
}
Now to access those keys anywhere in your code.
BuildConfig.KEY //Depending on your flavor you will get your key here
Hope this helped. :]
Thank you!
This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!