Google, sürüm kontrol sisteminizde bir API anahtarı kontrol etmemenizi önemle tavsiye eder. Bunun yerine, projenizin kök dizininde bulunan ancak sürüm kontrolü dışında tutulan yerel bir secrets.properties
dosyasında depolamanız ve ardından API anahtarını okumak için Android için Secrets Gradle Plugin'i kullanmanız gerekir.
Android için Secrets Gradle Eklentisi, sürüm kontrol sisteminde kontrol edilmemiş bir özellikler dosyasından API anahtarı da dahil olmak üzere gizli anahtarları okur. Eklenti daha sonra bu özellikleri Gradle tarafından oluşturulan BuildConfig
sınıfında ve Android manifest dosyasında değişkenler olarak gösterir.
API anahtarına erişmek üzere Android için Secrets Gradle Eklentisini kullanmaya ilişkin tam bir örnek için Android Studio projesi oluşturma bölümüne bakın.
Yükleme ve kullanım
Google Haritalar projenize Android için Secrets Gradle Eklentisi'ni yüklemek için:
-
Android Studio'da üst düzey
build.gradle
veyabuild.gradle.kts
dosyanızı açın ve aşağıdaki kodubuildscript
altındakidependencies
öğesine ekleyin.Modern
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") } }
-
Modül düzeyindeki
build.gradle
dosyanızı açın ve aşağıdaki koduplugins
öğesine ekleyin.Modern
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- Modül düzeyindeki
build.gradle
dosyanızdatargetSdk
vecompileSdk
öğelerinin 34 olarak ayarlandığından emin olun. - Dosyayı kaydedin ve projenizi Gradle ile senkronize edin.
-
Üst düzey dizininizde
secrets.properties
dosyasını açın, ardından aşağıdaki kodu ekleyin.YOUR_API_KEY
öğesini API anahtarınızla değiştirin.secrets.properties
, sürüm kontrol sistemine kontrol edilemiyor. Bu nedenle anahtarınızı bu dosyada saklayın.MAPS_API_KEY=YOUR_API_KEY
- Dosyayı kaydedin.
-
local.defaults.properties
dosyasını üst düzey dizininizde,secrets.properties
dosyasıyla aynı klasörde olacak şekilde oluşturun ve aşağıdaki kodu ekleyin.MAPS_API_KEY=DEFAULT_API_KEY
Bu dosyanın amacı,
secrets.properties
dosyası bulunamazsa derlemelerin başarısız olmaması için API anahtarı için yedek konum sağlamaktır. Uygulamayısecrets.properties
içermeyen bir sürüm kontrol sisteminden klonlarsanız ve henüz API anahtarınızı sağlamak için yerel olarak birsecrets.properties
dosyası oluşturmadıysanız bu durum görülebilir. - Dosyayı kaydedin.
-
AndroidManifest.xml
dosyanızdacom.google.android.geo.API_KEY
bölümüne gidipandroid:value attribute
değerini güncelleyin.<meta-data>
etiketi yoksa bunu<application>
etiketinin alt öğesi olarak oluşturun.<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.*" }
Sırada ne var?
- Secrets Gradle Plugin for Android GitHub proje sayfasını görüntüleyin.
- Eklentiyi kullanmayla ilgili tam bir örnek için Android Studio projesi oluşturma bölümüne bakın.
Aksi belirtilmediği sürece bu sayfanın içeriği Creative Commons Atıf 4.0 Lisansı altında ve kod örnekleri Apache 2.0 Lisansı altında lisanslanmıştır. Ayrıntılı bilgi için Google Developers Site Politikaları'na göz atın. Java, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2024-07-13 UTC.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"İhtiyacım olan bilgiler yok" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Çok karmaşık / çok fazla adım var" },{ "type": "thumb-down", "id": "outOfDate", "label":"Güncel değil" },{ "type": "thumb-down", "id": "translationIssue", "label":"Çeviri sorunu" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Örnek veya kod sorunu" },{ "type": "thumb-down", "id": "otherDown", "label":"Diğer" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Anlaması kolay" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Sorunumu çözdü" },{ "type": "thumb-up", "id": "otherUp", "label":"Diğer" }]