Google 强烈建议不要将 API 密钥签入版本控制系统。请改为将 API 密钥存储在本地 secrets.properties
文件中,该文件位于项目根目录下,但不在版本控制系统中,然后使用 Android 版 Secrets Gradle 插件读取 API 密钥。
Android 版 Secrets Gradle 插件可从未签入版本控制系统的属性文件中读取 Secret(包括 API 密钥)。然后,此插件会在 Gradle 生成的 BuildConfig
类和 Android 清单文件中,将这些属性公开为变量。
如需查看使用 Android 版 Secrets Gradle 插件访问 API 密钥的完整示例,请参阅设置 Android Studio 项目。
安装和使用
如需在 Google 地图项目中安装 Android 版 Secret Gradle 插件,请执行以下操作:
-
在 Android Studio 中,打开顶级
build.gradle
或build.gradle.kts
文件,并将以下代码添加到buildscript
下的dependencies
元素中。Groovy
buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
-
打开模块级
build.gradle
文件,并将以下代码添加到plugins
元素中。Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- 在模块级
build.gradle
文件中,确保targetSdk
和compileSdk
已设置为 34。 - 保存文件并将项目与 Gradle 同步。
-
在顶级目录中打开
secrets.properties
文件,然后添加以下代码。将YOUR_API_KEY
替换为您的 API 密钥。secrets.properties
不会签入版本控制系统,因此请将您的密钥存储在此文件中。MAPS_API_KEY=YOUR_API_KEY
- 保存文件。
-
在顶级目录(即
secrets.properties
文件所在的同一文件夹)中创建local.defaults.properties
文件,然后添加以下代码。MAPS_API_KEY=DEFAULT_API_KEY
此文件的用途是,在找不到
secrets.properties
文件的情况下,为 API 密钥提供备用位置,以免构建失败。如果您是从省略secrets.properties
的版本控制系统中克隆应用,而您在本地还没有创建secrets.properties
文件来提供 API 密钥,就可能会出现上述情况。 - 保存文件。
-
在
AndroidManifest.xml
文件中,定位到com.google.android.geo.API_KEY
并更新android:value attribute
。如果<meta-data>
标记不存在,请创建该标记作为<application>
标记的子标记。<meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" />
Note:
com.google.android.geo.API_KEY
is the recommended metadata name for the API key. A key with this name can be used to authenticate to multiple Google Maps-based APIs on the Android platform, including the Maps SDK for Android. For backwards compatibility, the API also supports the namecom.google.android.maps.v2.API_KEY
. This legacy name allows authentication to the Android Maps API v2 only. An application can specify only one of the API key metadata names. If both are specified, the API throws an exception. -
In Android Studio, open your module-level
build.gradle
orbuild.gradle.kts
file and edit thesecrets
property. If thesecrets
property does not exist, add it.Edit the properties of the plugin to set
propertiesFileName
tosecrets.properties
, setdefaultPropertiesFileName
tolocal.defaults.properties
, and set any other properties.Groovy
secrets { // Optionally specify a different file name containing your secrets. // The plugin defaults to "local.properties" propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*" }
Kotlin
secrets { // Optionally specify a different file name containing your secrets. // The plugin defaults to "local.properties" propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*" }
后续步骤
- 查看 Android 版 Secrets Gradle 插件的 GitHub 项目页面。
- 参阅设置 Android Studio 项目,了解使用此插件的完整示例。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-13。
[null,null,["最后更新时间 (UTC):2024-08-13。"],[[["\u003cp\u003eSecurely manage your Google Maps API key using the Secrets Gradle Plugin for Android, avoiding storage in version control.\u003c/p\u003e\n"],["\u003cp\u003eThe plugin reads your API key from a local \u003ccode\u003esecrets.properties\u003c/code\u003e file and makes it accessible in your \u003ccode\u003eBuildConfig\u003c/code\u003e and manifest.\u003c/p\u003e\n"],["\u003cp\u003eInstall the plugin by adding it as a dependency and applying it in your Gradle files, then configure it to locate your API key.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003elocal.defaults.properties\u003c/code\u003e file to provide a fallback API key for builds and prevent failures when the \u003ccode\u003esecrets.properties\u003c/code\u003e file is missing.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the plugin's GitHub page and the setup guide for detailed information and a comprehensive example.\u003c/p\u003e\n"]]],[],null,["Google strongly recommends that you not check an [API key](/maps/documentation/android-sdk/get-api-key) into your\nversion control system. Instead, you should store it in a local `secrets.properties` file,\nwhich is located in the root directory of your project but excluded from version control, and then\nuse the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin)\nto read the API key.\n\nThe Secrets Gradle Plugin for Android reads secrets, including the API key, from\na properties file not checked into a version control system. The plugin then exposes those properties\nas variables in the Gradle-generated `BuildConfig` class and in the Android manifest file.\n\nFor a complete example of using the Secrets Gradle Plugin for Android to access an API key,\nsee [Set up an Android Studio project](/maps/documentation/android-sdk/config).\n\n\nInstallation and usage **Note:** See the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) documentation on GitHub for the latest system requirements and installation instructions.\n\nTo install the Secrets Gradle Plugin for Android in your Google Maps project:\n\n1. In Android Studio, open your top-level `build.gradle.kts` or `build.gradle` file and add the following code to the `dependencies` element under `buildscript`. \n\n Kotlin \n\n ```yaml\n buildscript {\n dependencies {\n classpath(\"com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1\")\n }\n }\n ```\n\n Groovy \n\n ```yaml\n buildscript {\n dependencies {\n classpath \"com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1\"\n }\n }\n ```\n2. Open your module-level `build.gradle.kts` or `build.gradle` file and add the following code to the `plugins` element. \n\n Kotlin \n\n ```kotlin\n plugins {\n // ...\n id(\"com.google.android.libraries.mapsplatform.secrets-gradle-plugin\")\n }\n ```\n\n Groovy \n\n ```yaml\n plugins {\n // ...\n id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'\n }\n ```\n3. In your module-level `build.gradle.kts` or `build.gradle` file, ensure that `targetSdk` and `compileSdk` are set to 34.\n4. [Sync your project with Gradle](https://developer.android.com/studio/build#sync-files).\n 5. Open the `secrets.properties` file in your top-level directory, and then add the following code. Replace `YOUR_API_KEY` with your API key. Store your key in this file because `secrets.properties` is excluded from being checked into a version control system. **Note:** If the `secrets.properties` file does not exist, create it in the same folder as the `local.properties` file. \n\n ```scdoc\n MAPS_API_KEY=YOUR_API_KEY\n ```\n6. Create the `local.defaults.properties` file in your top-level directory, the same\n folder as the `secrets.properties` file, and then add the following code.\n\n **Note:** Enter the code as shown. Don't replace `DEFAULT_API_KEY` with your API key. \n\n ```scdoc\n MAPS_API_KEY=DEFAULT_API_KEY\n ```\n\n The purpose of this file is to provide a backup location for the API key if the\n `secrets.properties` file is not found so that builds don't fail. This can happen if\n you clone the app from a version control system which omits `secrets.properties` and\n you have not yet created a `secrets.properties` file locally to provide your\n API key.\n7. In your `AndroidManifest.xml` file, go to `com.google.android.geo.API_KEY` and update the `android:value attribute`. If the `\u003cmeta-data\u003e` tag does not exist, create it as a child of the `\u003capplication\u003e` tag. \n\n ```genshi\n \u003cmeta-data\n android:name=\"com.google.android.geo.API_KEY\"\n android:value=\"${MAPS_API_KEY}\" /\u003e\n ```\n\n **Note:**\n `com.google.android.geo.API_KEY` is the recommended metadata name\n for the API key. A key with this name can be used to authenticate to multiple\n Google Maps-based APIs on the Android platform, including the\n Maps SDK for Android. For backwards compatibility, the API also\n supports the name `com.google.android.maps.v2.API_KEY`. This legacy\n name allows authentication to the Android Maps API v2 only. An application can\n specify only one of the API key metadata names. If both are specified, the API\n throws an exception.\n8. In Android Studio, open your module-level `build.gradle.kts` or\n `build.gradle` file and edit the `secrets` property. If the\n `secrets` property does not exist, add it.\n\n Edit the properties of the plugin to set `propertiesFileName` to\n `secrets.properties`, set `defaultPropertiesFileName` to\n `local.defaults.properties`, and set any other properties. \n\n Kotlin \n\n ```kotlin\n secrets {\n // To add your Maps API key to this project:\n // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.\n // 2. Add this line, where YOUR_API_KEY is your API key:\n // MAPS_API_KEY=YOUR_API_KEY\n propertiesFileName = \"secrets.properties\"\n\n // A properties file containing default secret values. This file can be\n // checked in version control.\n defaultPropertiesFileName = \"local.defaults.properties\"\n }\n \n ```\n\n Groovy \n\n ```groovy\n secrets {\n // To add your Maps API key to this project:\n // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.\n // 2. Add this line, where YOUR_API_KEY is your API key:\n // MAPS_API_KEY=YOUR_API_KEY\n propertiesFileName = \"secrets.properties\"\n\n // A properties file containing default secret values. This file can be\n // checked in version control.\n defaultPropertiesFileName = \"local.defaults.properties\"\n }\n \n ```\n\nWhat's next\n\n- View the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) GitHub project page.\n- View [Set up an Android Studio project](/maps/documentation/android-sdk/config) for a complete example of using the plugin."]]