I am using one service where it's required to setup API key in AndroidManifest
like this:
<meta-data
android:name="service_api_key"
android:value="@string/my_api_key" />
The problem with this is that I have a couple of flavors of my app and I need to setup different API keys for each flavor. Each flavor needs to have different API key for debug and release:
flavor1
- debug key: key1
- release key: key2
flavor2
- debug key: key3
- release key: key4
flavor3
- debug key: key5
- release key: key6
What would be the recommended way to accomplish this?
What would be the recommended way to accomplish this?
Step #1: Create sourcesets for each build variant (src/flavor1Debug/
, src/flavor1Release/
, etc.) for which you need a different API key than whatever you have in src/main/
.
Step #2: In each sourceset, have a res/values/strings.xml
file that contains that build variant's value for my_api_key
.
Step #3: Beer.
not tested but I imagine you could try something like this...
applicationVariants.all {variants ->
variant.productFlavors.all { flavor ->
flavorChoosed = " ${flavor.name}"
}
}
release {
switch(flavorChoosed){
case "flavor1":
resValue "string", "flavorId", apiKeyRealeseFlavor1
break
.....
}
}
debug{
switch(flavorChoosed){
case "flavor1":
resValue "string", "flavorId", apiKeyDebugFlavor1
break
.....
}
}
<meta-data
android:name="service_api_key"
android:value="${flavorId}" />
For each Product Flavor you have added to your gradle
file you should add a string.xml
resource file for values.
When you switch your build variant, Android studio will be smart enough to grab the matching value for your build.
If you dont specify one, then it defaults to the main one.
EDIT:
Then for the source set, select the release or debug version of your productFlavor:
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