Multiple API keys for Single Android Studio Project

房东的猫 提交于 2019-12-12 08:17:10

问题


I'm using Google Maps API v2 for android and Google Places API, both have different api-keys to be added to manifest, but when i add both the keys, i got multiple-key error.

Is it possible to add two different keys for two different APIs, if not, then what is the possible work around?

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

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

回答1:


You will want to use gradle with Placeholders.

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="${mapsKey}" />

And in your gradle add

android {
   buildTypes {
    debug {
      manifestPlaceholders = [ mapsKey:"AIzaSyD****************U6QybngOI"]
    }
   }
}



回答2:


I solved the issue by using the same GEO API key for both Google Maps v2 and Google Places API, i-e the Geo api-key can be used for both places api and maps api as well.

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

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



回答3:


To add release and debug keys change your gradle file. You first have to make the API keys in the Google Developer Console.

apply plugin: 'com.android.application'

android {
signingConfigs {

}
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
    applicationId 'com.your.app'
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        manifestPlaceholders = [ mapsKey:"AIzaxxxxxxxxxxxxxxxxxxxvcgdXNA"]
    }
    debug {
        manifestPlaceholders = [ mapsKey:"AIzayyyyyyyyyyyyyyyyyyyyyyyC7NA"]
    }
}
productFlavors {
}
}

dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.google.android.gms:play-services:8.4.0' }




回答4:


You shouldn't use multiple key for this purpose. You just have to Enable Both services for the same API key, from the developers console.I think this answer will guide you through The process.



来源:https://stackoverflow.com/questions/33917437/multiple-api-keys-for-single-android-studio-project

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