Managing Google Maps API keys

时光总嘲笑我的痴心妄想 提交于 2019-12-09 13:37:58

问题


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


回答1:


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.




回答2:


you are doing the right thing but not the right way I think. Declare your string in strings.xml like this :

<string name="googleMapsAPIKey">TheMagicKeyString</string>
<!-- You can add this at the end as comment to keep a copy of another key for instance, to exchange it between debug and production-->

Note that you didn't give the same name at your 2 strings... One is called debug and not the other.




回答3:


I'm using maven and the maven-android-plugin, with a res-overlay directory that is only used in the release configuration.

Consequently, I have a res/values/google_api_key.xml file containing the debug key and a res-overlay-production/values/google_api_key.xml file containing the production key, and the latter overrides the former in the production release.

I'm also using Eclipse ADT, but this doesn't know about the release configuration, so it's happy to see the debug key and I can use the Android XML editors to set up my map views.




回答4:


I'm using maven-android-plugin and I'm using a sneaky token replacement mechanism to do this.

I'm using the "morseflash" sample, and added this to the maven-resources plugin configuration:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>resources</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
      <delimiters>
        <delimiter>${*}</delimiter>
        <delimiter>&gt;08*&lt;</delimiter>
      </delimiters>
    </configuration>
</plugin>

The resources plugin allows you to define weird delimiters for search/replace. The default delimiter is ${*}, but I added >*< so it would match XML element content. (Actually I used >08*<; I'll explain why in a moment.)

Then, in my strings.xml, I wrote this:

<string name="googleMapsAPIKey">08DEBUGKEYABCDEFBLAHBLAHBLAH</string>

And defined a Maven property in the release profile, like this:

<profile>
    <id>release</id>
    <!-- via this activation the profile is automatically used when the release is done with the maven release
    plugin -->
    <activation>
        <property>
            <name>performRelease</name>
            <value>true</value>
        </property>
    </activation>
    <properties>
        <DEBUGKEYABCDEFBLAHBLAHBLAH>&gt;08RELEASEKEYABCDEFBLAHBLAHBLAH&lt;</DEBUGKEYABCDEFBLAHBLAHBLAH>
    </properties>
    <-- ... -->
</profile>

That creates a Maven property named after the debug Maps key, whose value is the release debug key.

Unfortunately, Maven properties are not allowed to start with numbers. My debug key and my release key both started with 08Wfj... So I used the delimiter >08*< and made sure to include >08 in my replacement string.




回答5:


I think this is a good way to do it! With Sephy's correction it should work perfectly.

On top of that, if you want to switch keys automatically between the debug & release builds, you can have a look at this post I just wrote: http://blog.cuttleworks.com/2011/02/android-dev-prod-builds/. It helped me stop worrying about configuration when building for different environments.




回答6:


Try something like this:

String debugMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String releaseMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;

MapView mv = new MapView(this, mapKey);



回答7:


You can set custom debug keystore in Prefs window -> Android->Build.




回答8:


In the google developer console you can create one key for multiple sha1 codes https://stackoverflow.com/a/13881624/1433372



来源:https://stackoverflow.com/questions/3442545/managing-google-maps-api-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!