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
元素中。buildscript {
dependencies {
classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1"
}
}buildscript {
dependencies {
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
}
} -
打开模块级
build.gradle
文件,并将以下代码添加到plugins
元素中。plugins {
// ...
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}plugins {
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
} - 在模块级
build.gradle
文件中,确保targetSdk
和compileSdk
已设置为 34。 - 保存文件并将项目与 Gradle 同步。
-
在顶级目录中打开
secrets.properties
文件,然后添加以下代码。将YOUR_API_KEY
替换为您的 API 密钥。MAPS_API_KEY=
YOUR_API_KEY - 保存文件。
-
在
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}" /> -
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
to the name of your local properties file (eitherlocal.properties
orlocal.defaults.properties
depending on how you created the project), and set any other properties.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 项目,了解使用此插件的完整示例。