API キーをバージョン管理システムにチェックインしないことが強く推奨されます。代わりに、プロジェクトのルート ディレクトリにある(ただしバージョン管理からは除外される)ローカルの secrets.properties
ファイルに保存し、API キーの読み取りには Android 用 Secrets Gradle プラグインを使用します。
Android 用 Secrets Gradle プラグインは、バージョン管理システムにチェックインされていないプロパティ ファイルから、API キーなどのシークレットを読み取ります。プラグインは、これらのプロパティを、Gradle で生成された BuildConfig
クラスと Android マニフェスト ファイルの変数として公開します。
Android 用 Secrets Gradle プラグインを使用して API キーにアクセスする詳細な例については、Android Studio プロジェクトを設定するをご覧ください。
インストールと使用
Android 用 Secrets Gradle プラグインを Google マップ プロジェクトにインストールする手順は以下のとおりです。
-
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.*"
}
次のステップ
- GitHub で Android 用 Secrets Gradle プラグインのプロジェクト ページを表示します。
- プラグインの詳細な使用例については、Android Studio プロジェクトをセットアップするをご覧ください。