【发布时间】:2013-05-12 10:43:56
【问题描述】:
是否可以在同一代码库中为 Google Maps Android API 指定多个键?
看起来我每次更改密钥库时都必须更改清单文件中的密钥。这不是很方便,恕我直言,如果您需要测试使用密钥形式调试和发布密钥库的应用程序签名。
【问题讨论】:
标签: android android-studio api-key
是否可以在同一代码库中为 Google Maps Android API 指定多个键?
看起来我每次更改密钥库时都必须更改清单文件中的密钥。这不是很方便,恕我直言,如果您需要测试使用密钥形式调试和发布密钥库的应用程序签名。
【问题讨论】:
标签: android android-studio api-key
我同时在清单中添加了 both 键。像这样
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
..
android:versionCode="1"
android:versionName="1.0" >
<!-- RELEASE key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my-release-keu" />
<!-- DEBUG key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my-debug-key" />
</application>
</manifest>
显然,这行得通。看起来谷歌代码足够聪明,可以自动使用相关密钥。
【讨论】:
我不认为这是你想要做的。您应该在 Google Developer API 控制台上将调试和发布 SHA1 密钥添加到 API 密钥。看看this answer
【讨论】:
AFAIK,这样做没有编程形式。对于商品,您可以在 strings.xml 中定义 API 密钥,并从清单中检索它
【讨论】:
String debugMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String releaseMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;
MapView mv = new MapView(this, mapKey);
【讨论】: