管理谷歌地图API密钥与摇篮在Android的工作室密钥、摇篮、工作室、地图

2023-09-04 03:01:25 作者:笑拥孤独

我知道摇篮是强大的,我想管理API密钥开发/谷歌地图produciton

目前,我总是需要手动注释一行,并取消注释等,使其工作。有没有一种方法来自动执行的摇篮与一些自定义的发布配置?

 <! - 图形页面V2 API  - >
<使用库机器人:名称=com.google.android.maps/>
<元数据的android:NAME =com.google.android.maps.v2.API_KEY机器人:值=[MY_DEV_KEY]/>
&所述;! -  PROD
<元数据的android:NAME =com.google.android.maps.v2.API_KEY机器人:值=[MY_PROD_KEY]/>
 - >
 

解决方案

由于您使用的摇篮,你可以做到以下几点:

build.gradle

 安卓{
  .. .. ...
    buildTypes {
       调试{
          resValue串,google_maps_api_key,[你的开发KEY]
       }
       推出 {
           resValue串,google_maps_api_key,[您PROD KEY]
       }
    }
  }
 
Android 百度地图开发详解

和在 AndroidManifest.xml中

 <元数据
            机器人:名称=com.google.android.maps.v2.API_KEY
            机器人:值=@字符串/ google_maps_api_key/>
 

此方式,你只能有一个AndroidManifest.xml中,你根据你的生成类型的设定值。希望这有助于。

I know Gradle is powerful and I would like to manage the API keys for development/produciton of Google Maps

Currently I always need to manually comment one line and uncomment the other to make it work. Is there a way to do it automatically in Gradle with some custom release configuration ?

<!-- MapView v2 API -->
<uses-library android:name="com.google.android.maps" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_DEV_KEY]" />
<!-- PROD
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_PROD_KEY]" />
-->

解决方案

Since you are using gradle you can do the following:

build.gradle

android {
  .. .. ...
    buildTypes {
       debug {
          resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
       }
       release {
           resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"
       }
    }
  }

And in your AndroidManifest.xml

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

This way you only have one AndroidManifest.xml and you set value based on your build type. Hope this helps.

 
精彩推荐
图片推荐