Secrets Gradle eklentisi

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:

  1. Android Studio'da üst düzey build.gradle veya build.gradle.kts dosyanızı açın ve aşağıdaki kodu buildscript altındaki dependencies öğ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")
        }
    }
    
  2. Modül düzeyindeki build.gradle dosyanızı açın ve aşağıdaki kodu plugins öğesine ekleyin.

    Modern

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

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. Modül düzeyindeki build.gradle dosyanızda targetSdk ve compileSdk öğelerinin 34 olarak ayarlandığından emin olun.
  4. Dosyayı kaydedin ve projenizi Gradle ile senkronize edin.
  5. Ü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
  6. Dosyayı kaydedin.
  7. 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 bir secrets.properties dosyası oluşturmadıysanız bu durum görülebilir.

  8. Dosyayı kaydedin.
  9. AndroidManifest.xml dosyanızda com.google.android.geo.API_KEY bölümüne gidip android: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 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.*"
    }
            

Sırada ne var?