5

We are developing an Android application that uses google maps.

Right now, for development purposes, we're using the key like this:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

And

<string name="google_maps_key">KEY_HERE</string>

For deploying purposes, we will implement a "Bring Your Own Key" approach where each client that buys the product, also put his key to be used.

I know it's weird and somewhat unnecessary, but it is a process from a big company and this decision comes from "the top".

Is there any way that we can have dynamic keys? Like putting a key into a service and consuming it in the app or something like that?

Appreciate any help.

Renan Souza
  • 905
  • 9
  • 25

1 Answers1

5

This is not possible in Google API V2 according to the documentation the API key has to be assigned using the Manifest file.

https://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application

But its possible for MapView.

If you are instantiating a MapView directly from the code, you should pass the Maps API Key in the MapView constructor.

Code:

@Override
protected void onCreate(.....) {
     super.onCreate(....);
     mMapView = new MapView(this, YOUR_MAP_API_KEY); //pass key to MapView Constructor here
     setContentView(mMapView);
  //   ....
}
Raj
  • 2,997
  • 2
  • 12
  • 30
  • Thank you for the answer. The option to use a MapView is definitely viable. Is there any drawback of using the map view? I'll wait for a little to see if there are other answers, otherwise will accept yours soon. – Renan Souza Jul 12 '18 at 17:45
  • 1
    In map v2, Constructor has changed "new MapView(this, YOUR_MAP_API_KEY)" does not work, do you any solution for it. – Shivang Nov 25 '19 at 07:02