פלאגין של Secrets Gradle

Google ממליצה מאוד לא לבדוק מפתח API ב במערכת לניהול גרסאות. במקום זאת, כדאי לאחסן אותו בקובץ secrets.properties מקומי, שנמצא בתיקיית השורש של הפרויקט אבל לא נכלל בניהול הגרסאות, שימוש בפלאגין של Secrets Gradle ל-Android כדי לקרוא את מפתח ה-API.

הפלאגין Secrets Gradle ל-Android קורא סודות, כולל מפתח ה-API, החל מ- קובץ מאפיינים שלא נבדק במערכת לניהול גרסאות. לאחר מכן הפלאגין חושף את המאפיינים האלה בתור משתנים במחלקה BuildConfig שנוצרה על ידי Gradle, ובקובץ המניפסט של Android.

הנה דוגמה מלאה לשימוש בפלאגין של Secrets Gradle ל-Android כדי לגשת למפתח API: מידע נוסף זמין במאמר הגדרת פרויקט Android Studio.

התקנה ושימוש

כדי להתקין את הפלאגין Secrets Gradle ל-Android בפרויקט במפות Google:

  1. ב-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")
        }
    }
    
  2. פותחים את הקובץ build.gradle ברמת המודול ומוסיפים את הקוד הבא אל רכיב plugins.

    מגניב

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. בקובץ build.gradle ברמת המודול, מוודאים שהערכים targetSdk הערך של compileSdk מוגדר ל-34.
  4. שומרים את הקובץ ואז לסנכרן את הפרויקט עם Gradle
  5. פותחים את הקובץ secrets.properties בספרייה שברמה העליונה, ואז מוסיפים את באמצעות הקוד הבא. מחליפים את YOUR_API_KEY במפתח ה-API שלכם. אחסון המפתח בקובץ הזה כי secrets.properties לא נכלל בבדיקה לניהול גרסאות המערכת.
    MAPS_API_KEY=YOUR_API_KEY
  6. שומרים את הקובץ.
  7. יוצרים את הקובץ local.defaults.properties בספרייה שברמה העליונה, אותה בתיקייה בתור הקובץ secrets.properties, ואז מוסיפים את הקוד הבא.

    MAPS_API_KEY=DEFAULT_API_KEY

    מטרת הקובץ הזה היא לספק מיקום לגיבוי של מפתח ה-API אם קובץ secrets.properties לא נמצא כדי שגרסאות ה-build לא ייכשלו. הדבר יכול להתרחש אם משכפלים את האפליקציה ממערכת לניהול גרסאות שמושמטת מ-secrets.properties עדיין לא יצרת קובץ secrets.properties באופן מקומי כדי לספק מפתח API.

  8. שומרים את הקובץ.
  9. בקובץ 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 name com.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.

  10. In Android Studio, open your module-level build.gradle or build.gradle.kts file and edit the secrets property. If the secrets property does not exist, add it.

    Edit the properties of the plugin to set propertiesFileName to secrets.properties, set defaultPropertiesFileName to local.defaults.properties, and set any other properties.

    Groovy

    secrets {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.
        // 2. Add this line, where YOUR_API_KEY is your API key:
        //        MAPS_API_KEY=YOUR_API_KEY
        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 {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.
        // 2. Add this line, where YOUR_API_KEY is your API key:
        //        MAPS_API_KEY=YOUR_API_KEY
        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.*"
    }
            

המאמרים הבאים