Wtyczka Gradle obiektów tajnych

Zdecydowanie odradzamy sprawdzanie klucza interfejsu API w systemie kontroli wersji. Zamiast tego zapisz ją w lokalnym pliku secrets.properties, który znajduje się w katalogu głównym projektu, ale jest wykluczony z kontroli wersji, a następnie użyj wtyczki do obsługi obiektów tajnych w Gradle na Androida, aby odczytać klucz interfejsu API.

Wtyczka Gradle obiektów tajnych na Androida odczytuje obiekty tajne, w tym klucz interfejsu API, z pliku właściwości niezameldowanego w systemie kontroli wersji. Następnie udostępnia te właściwości jako zmienne w klasie BuildConfig wygenerowanej przez Gradle i w pliku manifestu Androida.

Pełny przykład użycia wtyczki Gradle dla obiektów tajnych na Androida do uzyskania dostępu do klucza interfejsu API znajdziesz w artykule Konfigurowanie projektu Android Studio.

Instalacja i użytkowanie

Aby zainstalować wtyczkę Secrets Gradle na Androida w projekcie Map Google:

  1. W Android Studio otwórz plik build.gradle lub build.gradle.kts najwyższego poziomu i dodaj ten kod do elementu dependencies w sekcji buildscript.

    Zakręcony

    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. Otwórz plik build.gradle na poziomie modułu i dodaj do elementu plugins poniższy kod.

    Zakręcony

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

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. W pliku build.gradle na poziomie modułu sprawdź, czy targetSdk i compileSdk mają wartość 34.
  4. Zapisz plik i zsynchronizuj projekt z Gradle.
  5. Otwórz plik secrets.properties w katalogu najwyższego poziomu i dodaj ten kod. Zastąp YOUR_API_KEY swoim kluczem interfejsu API. Zapisz klucz w tym pliku, ponieważ adres secrets.properties nie jest rejestrowany w systemie kontroli wersji.
    MAPS_API_KEY=YOUR_API_KEY
  6. Zapisz plik.
  7. Utwórz plik local.defaults.properties w katalogu najwyższego poziomu, w tym samym folderze co plik secrets.properties, a następnie dodaj poniższy kod.

    MAPS_API_KEY=DEFAULT_API_KEY

    Ten plik służy do podania lokalizacji kopii zapasowej klucza interfejsu API na wypadek, gdyby nie udało się znaleźć pliku secrets.properties, co pozwoli uniknąć błędów kompilacji. Może się tak zdarzyć, jeśli sklonujesz aplikację z systemu kontroli wersji, który pomija element secrets.properties, i nie utworzyłeś jeszcze lokalnie pliku secrets.properties zawierającego Twój klucz interfejsu API.

  8. Zapisz plik.
  9. W pliku AndroidManifest.xml przejdź do com.google.android.geo.API_KEY i zaktualizuj android:value attribute. Jeśli tag <meta-data> nie istnieje, utwórz go jako element podrzędny tagu <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.*"
    }
            

Co dalej