Multiple API keys in the same Android project

后端 未结 4 973
既然无缘
既然无缘 2021-01-15 06:26

Is it possible to specify multiple keys for Google Maps Android API in the same code base?

It looks like I have to change the key in manifest file each time I change

相关标签:
4条回答
  • 2021-01-15 07:11

    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

    0 讨论(0)
  • 2021-01-15 07:14

    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

    0 讨论(0)
  • 2021-01-15 07:18
    String debugMapKey   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    String releaseMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
    String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;
    
    MapView mv = new MapView(this, mapKey);
    
    0 讨论(0)
  • 2021-01-15 07:23

    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.

    0 讨论(0)
提交回复
热议问题