Google sangat menyarankan Anda agar tidak melakukan check in kunci API ke dalam sistem kontrol versi Anda. Sebagai gantinya, sebaiknya simpan kunci API dalam file secrets.properties
, yang terletak di direktori utama project, tetapi dikecualikan dari kontrol versi, lalu gunakan Plugin Secrets Gradle untuk Android untuk membaca kunci API tersebut.
Plugin Secrets Gradle untuk Android membaca secret, termasuk kunci API, dari file properti yang tidak di-check in ke dalam sistem kontrol versi. Kemudian, plugin ini akan mengekspos properti tersebut
sebagai variabel dalam class BuildConfig
yang dihasilkan Gradle dan dalam file manifes Android.
Untuk contoh lengkap penggunaan Plugin Secrets Gradle untuk Android dalam mengakses kunci API, lihat Menyiapkan project Android Studio.
Penginstalan dan penggunaan
Untuk menginstal Plugin Secrets Gradle untuk Android di project Google Maps:
-
Di Android Studio, buka file
build.gradle
ataubuild.gradle.kts
tingkat teratas dan tambahkan kode berikut ke elemendependencies
di bagianbuildscript
.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")
}
} - Buka file
build.gradle
tingkat modul dan tambahkan kode berikut ke elemenplugins
.plugins {
// ...
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}plugins {
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
} - Di file
build.gradle
tingkat modul, pastikantargetSdk
dancompileSdk
ditetapkan ke 34. - Simpan file dan sinkronkan project Anda dengan Gradle.
-
Buka file
secrets.properties
di direktori tingkat teratas, lalu tambahkan kode berikut. GantiYOUR_API_KEY
dengan kunci API Anda.MAPS_API_KEY=
YOUR_API_KEY - Simpan file.
-
Di file
AndroidManifest.xml
, bukacom.google.android.geo.API_KEY
, lalu perbaruiandroid:value attribute
. Jika tag<meta-data>
tidak ada, buat tag tersebut sebagai turunan dari tag<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.*"
}
Langkah berikutnya
- Lihat halaman project GitHub Plugin Secrets Gradle untuk Android.
- Lihat Menyiapkan project Android Studio untuk contoh lengkap penggunaan plugin.