Managing Google Maps API keys

*爱你&永不变心* 提交于 2019-12-03 16:12:55

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.

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.

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.

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.

Romain

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.

Try something like this:

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

String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;

MapView mv = new MapView(this, mapKey);

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

DoronK

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

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