Google ממליצה מאוד לא לבדוק מפתח API במערכת בקרת הגרסאות. במקום זאת, יש לאחסן אותו בקובץ secrets.properties
מקומי, שנמצא בספריית הבסיס של הפרויקט, אבל לא נכלל בניהול הגרסאות, ואז להשתמש ב-Secrets Gradle Plugin for Android כדי לקרוא את מפתח ה-API.
הפלאגין Secrets Gradle ל-Android קורא סודות, כולל מפתח ה-API, מקובץ מאפיינים שלא נכנס למערכת לניהול גרסאות. לאחר מכן הפלאגין חושף את המאפיינים האלה כמשתנים במחלקה BuildConfig
שנוצרה על ידי Gradle ובקובץ המניפסט של Android.
דוגמה מלאה לשימוש בפלאגין Secrets Gradle ל-Android כדי לגשת למפתח API, ראו הגדרת פרויקט ב-Android Studio.
התקנה ושימוש
כדי להתקין את הפלאגין Secrets Gradle ל-Android בפרויקט של מפות Google:
-
ב-Android Studio, פותחים את הקובץ
build.gradle
אוbuild.gradle.kts
ברמה העליונה ומוסיפים את הקוד הבא לרכיבdependencies
בקטעbuildscript
.מגניב
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
.מגניב
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
- שומרים את הקובץ.
-
יוצרים את הקובץ
local.defaults.properties
בספרייה ברמה העליונה, באותה תיקייה שבה נמצא הקובץsecrets.properties
, ולאחר מכן מוסיפים את הקוד הבא.MAPS_API_KEY=DEFAULT_API_KEY
מטרת הקובץ הזה היא לספק מיקום גיבוי למפתח ה-API אם הקובץ
secrets.properties
לא נמצא כדי שגרסאות build לא ייכשלו. מצב כזה יכול לקרות אם משכפלים את האפליקציה ממערכת לניהול גרסאות, ללא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.*" }
המאמרים הבאים
- הצג את Secrets Gradle Plugin for Android דף פרויקט GitHub.
- במאמר הגדרת פרויקט Android Studio אפשר לראות דוגמה מלאה לשימוש בפלאגין.
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2024-07-13 (שעון UTC).
[] []