Multiple Google api keys in manifest file (Android)

▼魔方 西西 提交于 2020-01-16 03:40:30

问题


I am trying to add two api keys i.e Google places api key and google maps api key. But I am not able to add both at same time to manifest file.How can I achieve the same.Any help would be appreciated. Thanks in advance.

The error I am getting is

Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.geo.API_KEY in the element of AndroidManifest.xml

I am including api keys like this

 <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="xxxxx" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="xxxx"/>

回答1:


Use following meta-data in your manifest. there is no need to create another api key for places api if you already have api key for google map. If you are using both Maps & Places Api in your application then you only need to specify geo api key.and just enable Places Api.

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="api_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />



回答2:


Rather than adding two different API keys, for the same project enable both keys and copy down the same key as follows for single project which you want currently

<!-- Goolge Maps API Key -->
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaS******************WDaKCEHP" />    

<!-- Google Places API Key -->
<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="AIzaS******************WDaKCEHP" />  



回答3:


Whats your Error log saying

Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.geo.API_KEY in the element of AndroidManifest.xml

Just remove

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="xxxx"/>

Make sure you have below permissions . Enough for Map Showing .

  <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/common_google_api_key" />

    <uses-library
        android:name="com.google.android.maps"
        android:required="false" />

Read

Getting error "java.lang.RuntimeException: Unable to start activity" in my app



来源:https://stackoverflow.com/questions/36028760/multiple-google-api-keys-in-manifest-file-android

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