In Android Studio, there's the concept of build types and flavors, and you can use these to get what you need. Build types are different versions of the app that are functionally identical but may differ in debugging code. By default, all Android Gradle projects have debug and release build types.
Flavors are versions of your app that are functionally different; you can have free and paid, for example. By default your Android Gradle projects don't have any flavors, but you can add them.
Build types and flavors are combined (into what's called a variant) when you do a build; in this example, you can have freeDebug, freeRelease, paidDebug, and paidRelease builds.
The build system lets you easily override a number of things in each type/flavor/variant; one of the things you can do is to override parts of the AndroidManifest.xml file. The build system merges together the different eligible bits of manifests into one master manifest when it builds a particular variant.
With that background in hand, in your case you might want to have a different API key in the debug version of your app vs. the release version. The debug version is what you'll use in your day-to-day development, debugging and testing, and the release version is what you'd deploy to users.
To do this, do not put the Google Maps API key in the main app's AndroidManifest.xml file in src/main
; instead, add two new folders, src/debug
and src/release
and add stub AndroidManifest.xml files there. Don't include full information in those new manifests, but only what's unique about what's needed for that particular variant. Your source files will look like this:
Your src/debug/AndroidManifest.xml
file will contain this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_DEV_KEY]" />
</manifest>
and src/release/AndroidManifest.xml
will have this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_PROD_KEY]" />
</manifest>
To reiterate, don't put any API key in the src/main/AndroidManifest.xml
file.
If for some reason you don't want to use build types to differentiate you could set up dev and prod flavors and split it that way instead; the manifest overriding works in the same way.