Sebastien
Sebastien

Reputation: 4295

How to add multiple Google Maps API configs in a multi-flavor app

I have a multi-flavor Android app because I need my dev, qa and prod applications with different IDs to connect to different Firebase projects.

flavorDimensions "env"
productFlavors {
    dev {
        dimension "env"
        applicationId "com.myapp.debug"
    }
    qa {
        dimension "env"
        applicationId "com.myapp.beta"
    }
    prod {
        dimension "env"
        applicationId "com.myapp"
    }
}

I also added 3 versions of google_maps_api.xml in the respective source folders like so:

enter image description here

In the dev version, everything works smoothly, the maps shows OK. But when I release the app in closed alpha after generating a signed version with the release/qa variant, the map doesn't show in the app.

Since I'm still a newbie in Android, I don't know what I might have missed or even how I can troubleshoot this issue? Is there an easy way to see the logs of the qa version released in closed alpha? Am I missing something obvious? Where should I find the google_maps_api.xml file in the signed APK, so that I can check that the right one is in there?

Upvotes: 6

Views: 2119

Answers (3)

Kapil Sahu
Kapil Sahu

Reputation: 599

I was able to use different Maps API keys for different flavor as follow

Step 1: Add your different keys in manifestPlaceholders in app/build.gradle

  flavorDimensions "default"
    productFlavors {
        dev {
            dimension "default"
            manifestPlaceholders.google_maps_key = "Dev API key here"
        }
        staging {
            dimension "default"
            manifestPlaceholders.google_maps_key = "Staging API key here"
        }
    }

Step 2: In the AndroidManifest.xml inside the application tag use this code to refer API key as per the flavor

<application    
<meta-data android:name="com.google.android.geo.API_KEY"
                android:value="${google_maps_key}"/>

</application>

Upvotes: 4

Sebastien
Sebastien

Reputation: 4295

I think I figured out what I missed. I opted in to Google Play App signing, so I'm signing my app with an upload key, and then Google signs it with a deploy key that it keeps. And I found this piece of documentation, that says I have to get my SHA-1 fingerprint from Google and use this in my Google Maps API key. So I exported that SHA-1 and I pasted it into my Google Maps API key configuration and now it seems to work fine.

Upvotes: 1

MJM
MJM

Reputation: 5301

Remove your google_maps_key from google_map_api.xml file and declare it in gradle file as mentions below

flavorDimensions "env"
    productFlavors {
        dev {
            dimension "env"
            applicationId "com.myapp.debug"
            resValue 'string', 'google_maps_key', 'google_maps_key_for_dev'  // update your actual key
        }
        qa {
            dimension "env"
            applicationId "com.myapp.beta"
              resValue 'string', 'google_maps_key', 'google_maps_key_for_qa'  // update your actual key
        }
        prod {
            dimension "env"
            applicationId "com.myapp"
              resValue 'string', 'google_maps_key', 'google_maps_key_for_prod' // update your actual key
        }
    }

You have to generate new map key for each applicationId.

Upvotes: 0

Related Questions