Multiple API keys in the same Android project

删除回忆录丶 提交于 2019-12-01 10:32:55
osrl

I don't think this is what you want to do. You should add both debug and release SHA1 key to API key on Google Developer API Console. Take a look at this answer

I added both keys in the manifest at once. Like this

<?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>

Apparently, this works. Looks like Google code is smart enough to use relevant key automatically.

AFAIK, there is no programmatical form on doing this. For comodity, you can define API keys in strings.xml, and retrieve it from the manifest

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

String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;

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