在 Android Studio 中使用 Gradle 管理 Google Maps API 密钥 [英] Manage Google Maps API Key with Gradle in Android Studio

查看:32
本文介绍了在 Android Studio 中使用 Gradle 管理 Google Maps API 密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Gradle 很强大,我想管理用于开发/生产 Google 地图的 API 密钥

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

目前我总是需要手动注释一行并取消注释另一行以使其工作.有没有办法在 Gradle 中使用一些自定义发布配置自动执行此操作?

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]" />
-->

推荐答案

由于您使用的是 gradle,您可以执行以下操作:

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]"
       }
    }
  }

并在您的 AndroidManifest.xml

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

这样你就只有一个 AndroidManifest.xml 并且你可以根据你的构建类型设置值.希望这会有所帮助.

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

这篇关于在 Android Studio 中使用 Gradle 管理 Google Maps API 密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆