सीक्रेट ग्रेडल प्लग इन

Google का सुझाव है कि आप अपने वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच न करें. इसके बजाय, आपको इसे लोकल secrets.properties फ़ाइल में सेव करना चाहिए, जो आपके प्रोजेक्ट की रूट डायरेक्ट्री में मौजूद होती है. हालांकि, इसे वर्शन कंट्रोल में नहीं रखा जाना चाहिए. इसके बाद, एपीआई पासकोड को पढ़ने के लिए, Android के लिए सीक्रेट ग्रेडल प्लगिन का इस्तेमाल करें.

Android के लिए Secrets Gradle प्लगिन, ऐसी प्रॉपर्टी फ़ाइल से सीक्रेटों को पढ़ता है जिसे वर्शन कंट्रोल सिस्टम में चेक नहीं किया गया था. इसमें, API पासकोड भी शामिल है. इसके बाद, प्लग इन उन प्रॉपर्टी को Gredle से जनरेट की गई BuildConfig क्लास और Android मेनिफ़ेस्ट फ़ाइल में, वैरिएबल के तौर पर दिखाता है.

एपीआई पासकोड को ऐक्सेस करने के लिए, Android के लिए Secrets Gradle प्लगिन का इस्तेमाल करने का पूरा उदाहरण देखने के लिए, Android Studio प्रोजेक्ट सेट अप करना लेख पढ़ें.

इंस्टॉलेशन और इस्तेमाल

अपने Google Maps प्रोजेक्ट में Android के लिए Secrets Gradle प्लग इन इंस्टॉल करने के लिए:

  1. Android Studio में, अपनी टॉप लेवल build.gradle या build.gradle.kts फ़ाइल खोलें और buildscript में जाकर, dependencies एलिमेंट में इस कोड को जोड़ें.

    ग्रूवी

    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. फ़ाइल सेव करें और Gredle के साथ अपना प्रोजेक्ट सिंक करें.
  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 {
        // 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.*"
    }
            

आगे क्या करना है