45

I'm using Google Maps v2 API in my project. In Google Maps v2 the debug/release API key is defined in AndroidManifest.xml. I have seen the link but in that map key is defined in a xml layout file not in AndroidManifest.xml. So can I define both debug and release keys for my project in AndroidManifest.xml?

I want something like this in AndroidManifest.xml:

If debug mode:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/debug_map_api_key"/>

If release mode:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/release_map_api_key"/>
Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
  • 2
    http://stackoverflow.com/questions/13878249/how-to-configure-android-map-sdk-v2-to-use-different-keys-for-production-and-dev – Divya Motiwala Jun 17 '13 at 05:55
  • I get the idea from this link. Thanks – Kirit Vaghela Jun 17 '13 at 06:02
  • @KiritVaghela hey kirit how you doing my friend? remember you solved my listview problem when i was developing cheatbook? but those suckers terminated my developer account – JRE.exe Sep 05 '14 at 16:05
  • @JRE.exe do they provide specific reason for this termination? if so, post it here. – Kirit Vaghela Sep 06 '14 at 04:56
  • It takes 3 app suspensions to get your account terminated (no matter simultaneous or after some time) so first violation was description (which was not my fault) i wrote proper description with spaces but it removed all the spaces on its own leading to suspension. second is intellectual property (that's my only fault because i never new about such thing). third one was i made a chat app and it got suspended because of inappropriate behavior of users (how stupid of google for doing that). i write to them 3 times but they never replied. now planning to sell my phone for IOS account and app's fee – JRE.exe Sep 06 '14 at 05:24
  • reason they provided is multiple violations of T&C. it was terminated this year in march when i was making 60k INR per month and that was just starting.. – JRE.exe Sep 06 '14 at 05:26
  • Easy and quickest solution. http://stackoverflow.com/a/39265698/1348522 – Zumry Mohamed Nov 08 '16 at 10:15

4 Answers4

69

Using build.gradle

buildTypes {

    debug {
        buildConfigField("String", "map_api_key", "\"your debug map api key here\"")
    }
    release {
        buildConfigField("String", "map_api_key", "\"your release map api key here\"")
    }
}

I solved this issue using this steps:

In Google Developer API Console

  1. Click on Create New Android key...
  2. In cmd.exe/Terminal: keytool -list -v -keystore mystore.keystore
  3. Password: android
  4. Now enter SHA1 key;package name for debug and press enter
  5. Enter SHA1 key;package name for release
  6. Click on Create

Now use this API key your project

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/common_map_api_key"/>
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
32

One of the Best way to use build.gradle file in latest Android 5.0 and Android 6.0, Android 9+ (API 20, 21,22,23, 24, 25,26, 27 28, 29)

Well you can use them simply without creating product flavors in gradle. This is another example we can achieve via Gradle. You can achieve it with two simple steps.

  • Add custom value to manifestplaceholders build.gradle file.

See below

buildTypes {
    debug {
        manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"]
    }

    release {
        manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"]
    }
}
  • Edit manifest file like below.

part of my manifest file

  <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="${mapApiKeyValue}" />

This solution works for the latest Android 5.0 and Android 6.0 (API 20, 21,22,23)


Updated on 5/3/2018 for Xamarin Form and Xamarin Native Apps

Open AssemblyInfo.cs in Android Project and addd the following code

#if DEBUG
   [assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "DebugKey123123123")]
#else
   [assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "ReleaseKey123123123")]
#endif

To Check the AndroidManifest file, goto obj/Debug/android folder and open the manifest file and check the meta info,

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="DebugKey123123123" />
Community
  • 1
  • 1
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
16

For organizations that need to maintain separate keys, you can place them in separate directories in Android Studio. Make sure the subdirectory of src you use matches a flavor or buildType

From Building Your Project with Gradle:

To build each version of your app, the build system combines source code and resources from:

src/main/ - the main source directory (common to all variants)
src/<buildType>/ - the build type source directory
src/<flavorName>/ - the flavor source directory

In projectroot/yourapp/build.gradle:

buildTypes {
    debug {
        runProguard false
        debuggable true

    }
    release {
        runProguard true
        debuggable false
        ...
    }

In projectroot/yourapp/src/main/AndroidManifest.xml:

...
<application
    android:name=".MyApplication"
    android:theme="@style/Theme">
<!-- Don't put your key here -->
...

In projectroot/yourapp/src/debug/AndroidManifest.xml, fully qualify the name of the app.

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:name="com.hipmunk.android.HipmunkApplication">
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="yourdebugkey" />
    </application>
</manifest>

In projectroot/yourapp/src/release/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:name="com.hipmunk.android.HipmunkApplication">
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="release key" />
    </application>
</manifest>
Robert Karl
  • 7,598
  • 6
  • 38
  • 61
8

Since you are using gradle you can do the following:

build.gradle

android {
  .. .. ...
    buildTypes {
       debug {
          resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
       }
       release {
           resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"
       }
    }
  }

And in your AndroidManifest.xml

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_api_key"/>

This way you only have one AndroidManifest.xml and you set value based on your build type. Hope this helps.

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111