Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing Google Maps API keys

My application uses a large number of MapView screens and I'm trying to figure out how to manage the API key between the debug environment and production. Apparently, there is no way to change the debug application key in Eclipse so I must use a debug map API key in that environment. Conversely, there is no way to export a package for beta testing without a production application key so I must change the map API key in every view in order to create a working package.

My first idea was to do this:

All MapView.xml files have this:

android:apiKey="@string/googleMapsAPIKey"

And then in strings.xml I put this:

<string name="googleMapsPIKey">@string/debugGoogleMapsAPIKey</string>
<string name="debugGoogleMapsAPIKey">TheMagicKeyString</string>

If this worked, it would allow me to change a single line in strings.xml and all the MapViews would get updated in the rebuild. But it didn't work. I guess strings.xml can't make references into itself. Any other ideas?

Thanks

like image 767
user409849 Avatar asked Dec 06 '22 01:12

user409849


1 Answers

I recommend a simpler approach that involves taking advantage of an unlikely, but more specific asset folder to house your debug key. To set this up, create a file res/values/maps-apikey.xml with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="maps_apikey">[productionApiKey]</string>
</resources>

Then each developer locally will add a file derived from this one in res/values-v1/maps-apikey.xml where their debug API key is provided. At runtime, the Android system will favor the more specific version res/values-v1/maps-apikey.xml for all versions of Android (since all are at least on API level 1 or higher).

Source control systems like git and svn allow you to add an "ignore" file which directs the tools to ignore this res/values-v1/maps-apikey.xml file so that each developer does not accidentally commit it to the repository. To do this for git, simply add a file res/.gitignore which contains only the line values-v1 then commit this file.

To release your software, simply delete the res/values-v1/maps-apikey.xml file before exporting a release. The resulting APK will then reference the version in res/values/maps-apikey.xml which is your correct production key.

like image 70
Josh Guilfoyle Avatar answered Dec 16 '22 09:12

Josh Guilfoyle