在 Android Studio 中使用 Gradle 管理 Google Maps API 密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26124309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:04:36  来源:igfitidea点击:

Manage Google Maps API Key with Gradle in Android Studio

androidgradleandroid-studiokeygoogle-maps-android-api-2

提问by Hrk

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

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

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 ?

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

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

回答by bond

Since you are using gradle you can do the following:

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

build.gradle

构建.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

在你的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.

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

回答by IlyaEremin

You can achieve this with manifest placeholder feature: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-Placeholder-support

您可以使用清单占位符功能实现此目的:http: //tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-Placeholder-support

in build.gradle file:

在 build.gradle 文件中:

buildTypes {
    debug {
        manifestPlaceholders = [ google_map_key:"your_dev_key"]
    }
    release {
        manifestPlaceholders = [ google_map_key:"prod_key"]
    }
}

and then in manifest:

然后在清单中:

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

That's exact thing for different keys for different flavors and this is cleaner solution than using string resources.

对于不同风格的不同键来说,这是完全正确的,这是比使用字符串资源更干净的解决方案。

P.S. On the other hand I would better consider getting api keys from backend and do not hardcode them on the client. This is more secure and more flexible approach.

PS 另一方面,我最好考虑从后端获取 api 密钥,并且不要在客户端上对它们进行硬编码。这是更安全、更灵活的方法。

回答by Okas

In Android Studio (checked with version 0.8.11) you can add Google Maps Activity (New->Google->Google Maps Activity) to your project and Android studio will generate necessary files for you, you only have to insert your keys. There are also instructions generated. Look for google_maps_api.xml files in your debug/res/values/ and release/res/values folders.

在 Android Studio(检查版本 0.8.11)中,您可以将 Google Maps Activity(New->Google->Google Maps Activity)添加到您的项目中,Android Studio 将为您生成必要的文件,您只需要插入您的密钥。也有指令生成。在您的 debug/res/values/ 和 release/res/values 文件夹中查找 google_maps_api.xml 文件。

回答by Scott Barta

In Android Studio, there's the concept of build types and flavors, and you can use these to get what you need. Build types are different versions of the app that are functionally identical but may differ in debugging code. By default, all Android Gradle projects have debug and release build types.

在 Android Studio 中,有构建类型和风格的概念,您可以使用它们来获得所需的内容。构建类型是功能相同但调试代码可能不同的应用程序的不同版本。默认情况下,所有 Android Gradle 项目都有调试和发布构建类型。

Flavors are versions of your app that are functionally different; you can have free and paid, for example. By default your Android Gradle projects don't have any flavors, but you can add them.

Flavors 是功能不同的应用程序版本;例如,你可以有免费和付费。默认情况下,您的 Android Gradle 项目没有任何风格,但您可以添加它们。

Build types and flavors are combined (into what's called a variant) when you do a build; in this example, you can have freeDebug, freeRelease, paidDebug, and paidRelease builds.

当您进行构建时,构建类型和风格被组合在一起(成为所谓的变体);在此示例中,您可以使用 freeDebug、freeRelease、paidDebug 和paidRelease 构建。

The build system lets you easily override a number of things in each type/flavor/variant; one of the things you can do is to override parts of the AndroidManifest.xml file. The build system merges together the different eligible bits of manifests into one master manifest when it builds a particular variant.

构建系统可让您轻松覆盖每种类型/风味/变体中的许多内容;您可以做的一件事是覆盖 AndroidManifest.xml 文件的部分内容。构建系统在构建特定变体时将清单的不同合格位合并到一个主清单中。

With that background in hand, in your case you might want to have a different API key in the debug version of your app vs. the release version. The debug version is what you'll use in your day-to-day development, debugging and testing, and the release version is what you'd deploy to users.

有了这个背景,在您的情况下,您可能希望在应用程序的调试版本与发布版本中使用不同的 API 密钥。调试版本是您将在日常开发、调试和测试中使用的版本,而发布版本是您将部署给用户的版本。

To do this, do notput the Google Maps API key in the main app's AndroidManifest.xml file in src/main; instead, add two new folders, src/debugand src/releaseand add stubAndroidManifest.xml files there. Don't include full information in those new manifests, but only what's unique about what's needed for that particular variant. Your source files will look like this:

为此,请勿将 Google Maps API 密钥放在主应用程序的 AndroidManifest.xml 文件中src/main;相反,添加两个新的文件夹,src/debugsrc/release添加存根的AndroidManifest.xml文件存在。不要在这些新清单中包含完整信息,而只包含该特定变体所需内容的独特之处。您的源文件将如下所示:

Screenshot of project directory structure showing multiple manifest files

Screenshot of project directory structure showing multiple manifest files

Your src/debug/AndroidManifest.xmlfile will contain this:

您的src/debug/AndroidManifest.xml文件将包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_DEV_KEY]" />
</manifest>

and src/release/AndroidManifest.xmlwill have this:

并且src/release/AndroidManifest.xml都会有这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_PROD_KEY]" />
</manifest>

To reiterate, don't put any API key in the src/main/AndroidManifest.xmlfile.

重申一下,不要在src/main/AndroidManifest.xml文件中放置任何 API 密钥。

If for some reason you don't want to use build types to differentiate you could set up dev and prod flavors and split it that way instead; the manifest overriding works in the same way.

如果由于某种原因您不想使用构建类型来区分,您可以设置 dev 和 prod 风格并以这种方式拆分;清单覆盖以相同的方式工作。