Dynamically change the debug / release manifest key for each flavor

I use one service where you want to set the API key in the AndroidManifestfollowing way:

    <meta-data
        android:name="service_api_key"
        android:value="@string/my_api_key" />

The problem is that I have several variations of my application, and I need to configure different API keys for each flavor. Each flavor should have different API keys for debugging 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 do this?

+4
source share
3 answers

What would be the recommended way to do this?

# 1: sourcesets (src/flavor1Debug/, src/flavor1Release/ ..), API, , src/main/.

№2: res/values/strings.xml, my_api_key.

№3: .

+8

, , - ...

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}" />
+1

gradle string.xml .

, Android , .

, .

EDIT:

enter image description here

Flavor:

enter image description here

+1
source

Source: https://habr.com/ru/post/1625361/


All Articles