المكوّن الإضافي Secrets Gradle

تنصح Google بشدة بعدم التحقّق من مفتاح واجهة برمجة التطبيقات في نظام التحكم في الإصدار. بدلاً من ذلك، يجب تخزينها في ملف secrets.properties محلي. الموجود في الدليل الجذري لمشروعك ولكن يتم استبعاده من التحكم في الإصدار، ثم استخدام المكوّن الإضافي Secrets Gradle لأجهزة Android لقراءة مفتاح واجهة برمجة التطبيقات.

يقرأ المكوّن الإضافي السري لنظام Gradle المتوافق مع Android الأسرار، بما في ذلك مفتاح واجهة برمجة التطبيقات، من ملف خصائص لم يتم فحصه في نظام التحكم في الإصدار. بعد ذلك يعرض المكون الإضافي هذه الخصائص كمتغيرات في فئة BuildConfig التي تم إنشاؤها من خلال Gradle وفي ملف بيان Android.

للحصول على مثال كامل لاستخدام المكوّن الإضافي Secrets Gradle Plugin for Android للوصول إلى مفتاح واجهة برمجة التطبيقات، يُرجى الاطّلاع على مقالة إعداد مشروع على "استوديو Android".

التثبيت والاستخدام

لتثبيت مكوّن Secrets Gradle الإضافي لنظام التشغيل Android في مشروعك على "خرائط Google"، اتّبِع الخطوات التالية:

  1. في "استوديو Android"، افتح build.gradle أو build.gradle.kts المستوى الأعلى. وإضافة التعليمة البرمجية التالية إلى العنصر dependencies ضمن buildscript

    Groovy

    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

    Groovy

    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 بمفتاح واجهة برمجة التطبيقات الخاص بك. تخزين مفتاحك في هذا الملف لأنّه تم استبعاد secrets.properties من التحقق من عنصر تحكّم الإصدار .
    MAPS_API_KEY=YOUR_API_KEY
  6. احفظ الملف.
  7. أنشئ الملف local.defaults.properties في دليل المستوى الأعلى، بالطريقة نفسها كملف secrets.properties، ثم أضف الرمز التالي.

    MAPS_API_KEY=DEFAULT_API_KEY

    الغرض من هذا الملف هو توفير موقع احتياطي لمفتاح واجهة برمجة التطبيقات إذا كان لم يتم العثور على ملف secrets.properties، لذا لا تفشل عمليات الإنشاء. يمكن أن يحدث هذا إذا استنساخ التطبيق من نظام تحكم في الإصدار يحذف secrets.properties لم تنشئ بعد ملف secrets.properties محليًا لتقديم مفتاح واجهة برمجة التطبيقات.

  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.*"
    }
            

الخطوات التالية