6

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?

user4386126
  • 1,205
  • 5
  • 17
  • 32

3 Answers3

10

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Great answer. @user4386126 More details on this solution are available [here](http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies) – Phil Jan 21 '16 at 16:44
  • Thank you. This is awesome, looks really easy. Much easier than I expected. So, under my `src` folder I would have something like this: `src/flavor1/`, `src/flavor1Debug/` and `src/flavor1Release/`? `src/flavor1/` would hold all the code for flavor1 and in `src/flavor1Debug/` I would just insert `res/values/strings.xml` and add api key which I want to override? – user4386126 Jan 21 '16 at 16:47
  • 1
    @user4386126: You can do it that way. A slight simplification is to pick one set (e.g., release keys) that go in the main flavor sourceset, so you only need two sourcesets per flavor, not three. – CommonsWare Jan 21 '16 at 16:50
  • @CommonsWare, if create `src/flavor1/` and `src/flavor1Release/`, and want to create 2 same xml values files, I'll receive a warning that file already exists. – user4386126 Jan 22 '16 at 17:35
  • @user4386126: Hmmm... that's surprising. I thought that the full variant would override the flavor-only variant, which would override `main`. Perhaps I am mistaken. Regardless, your earlier plan (with three sourcesets) should work. – CommonsWare Jan 22 '16 at 17:37
  • @CommonsWare, unfortunately not. I can't even create `values` file to hold my string as `values` directory already exists in `src/flavor1/res`. I hope I don't need to delete `src/flavor1/` and create only `src/flavor1Debug/` and `src/flavor1/Release`. Hmm... – user4386126 Jan 22 '16 at 19:29
1

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}" />
Pedro Teran
  • 1,200
  • 3
  • 17
  • 43
1

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:

enter image description here

Then for the source set, select the release or debug version of your productFlavor:

enter image description here

meda
  • 45,103
  • 14
  • 92
  • 122
  • Thanks meda, I am doing that already. The problem which I encountered is to have separate debug and release keys for each flavor. – user4386126 Jan 22 '16 at 19:31
  • @user4386126 you can add `strings.xml` for debug or release of each productFlavor – meda Jan 22 '16 at 19:47
  • Thanks but how can I accomplish that? :) If I want to create `values` folder for `flavor1Release`, I receive a warning that `values` folder already exists because I already have it in my `flavor1/res/` folder. – user4386126 Jan 22 '16 at 19:54
  • 1
    @user4386126 I added screenshot – meda Jan 22 '16 at 20:00