Secrets Gradle eklentisi

Google, sürüm kontrol sistemi. Bunun yerine, dosyayı yerel bir secrets.properties dosyasında depolamalısınız. sürüm kontrolünden hariç tutulan ancak projenizin kök dizininde yer alan ve Android için Secrets Gradle Eklentisi'ni kullanın. API anahtarını okumanız gerekir.

Android için Secrets Gradle Plugin, API anahtarı da dahil olmak üzere gizli anahtarları sürüm kontrol sistemine kontrol edilmemiş bir özellikler dosyası. Eklenti, daha sonra bu özellikleri Gradle tarafından oluşturulan BuildConfig sınıfında ve Android manifest dosyasında değişken olarak gösterilir.

Bir API anahtarına erişmek üzere Android için Secrets Gradle eklentisini kullanmanın tam bir örneği için: Android Studio projesi oluşturma başlıklı makaleyi inceleyin.

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 dosyasını seçin ve aşağıdaki kodu altındaki dependencies öğesine ekleyin buildscript.

    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 öğesi.

    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, 34 olarak ayarlandı.
  4. Dosyayı kaydedin ve projenizi Gradle ile senkronize edin.
  5. secrets.properties dosyasını en üst düzey dizininizde açın ve şunu ekleyin: aşağıdaki kodu kullanabilirsiniz. YOUR_API_KEY kısmını API anahtarınızla değiştirin. Anahtarınızı bu dosyada depolayın secrets.properties, sürüm kontrolüne kontrol edilmekten hariç tutulduğundan bahsedeceğim.
    MAPS_API_KEY=YOUR_API_KEY
  6. Dosyayı kaydedin.
  7. Üst düzey dizininizde local.defaults.properties dosyasını oluşturun. klasörünü secrets.properties dosyası olarak kaydedin, ardından aşağıdaki kodu ekleyin.

    MAPS_API_KEY=DEFAULT_API_KEY

    Bu dosyanın amacı, Derlemelerin başarısız olmaması için secrets.properties dosyası bulunamadı. Bu durum, uygulamayı, secrets.properties ve sağlamak için henüz yerel olarak bir secrets.properties dosyası oluşturmadınız API anahtarı.

  8. Dosyayı kaydedin.
  9. AndroidManifest.xml dosyanızda şuna gidin: com.google.android.geo.API_KEY ve android:value attribute öğesini güncelleyin. <meta-data> etiketi mevcut değilse bunu <application> etiketi.
    <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?