Wtyczka Gradle obiektów tajnych

Zdecydowanie odradzamy sprawdzanie klucza interfejsu API w systemu kontroli wersji. Przechowuj go w lokalnym pliku secrets.properties, który znajduje się w katalogu głównym projektu, ale jest wykluczony z kontroli wersji, a następnie używaj wtyczki Gradle obiektów tajnych dla Androida odczytuje klucz interfejsu API.

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

Pełny przykład wykorzystania wtyczki Gradle obiektów tajnych na Androida w celu uzyskania dostępu do klucza interfejsu API: Więcej informacji znajdziesz w artykule o konfigurowaniu projektu w Android Studio.

Instalacja i użytkowanie

Aby zainstalować wtyczkę Gradle obiektów tajnych na Androida w projekcie Map Google:

  1. W Android Studio otwórz build.gradle lub build.gradle.kts najwyższego poziomu. i dodaj ten kod do elementu dependencies pod 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 poniższy kod do sekcji plugins.

    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 oraz compileSdk mają wartość 34.
  4. Zapisz plik i zsynchronizować projekt z Gradle.
  5. Otwórz plik secrets.properties w katalogu najwyższego poziomu, a następnie dodaj tego kodu. Zastąp YOUR_API_KEY swoim kluczem interfejsu API. Przechowuj klucz w tym pliku ponieważ secrets.properties nie jest sprawdzany w kontroli wersji systemu.
    MAPS_API_KEY=YOUR_API_KEY
  6. Zapisz plik.
  7. Utwórz plik local.defaults.properties w katalogu najwyższego poziomu, tak samo jako plik secrets.properties, a potem dodaj podany niżej kod.

    MAPS_API_KEY=DEFAULT_API_KEY

    Ten plik ma na celu udostępnienie zapasowej lokalizacji klucza interfejsu API, jeśli plik Nie znaleziono pliku secrets.properties, więc kompilacje nie zawierają błędów. Może się tak zdarzyć, jeśli sklonujesz aplikację z systemu kontroli wersji, który pomija secrets.properties oraz nie utworzyłeś jeszcze lokalnie pliku secrets.properties, który umożliwi przesyłanie klucz interfejsu API.

  8. Zapisz plik.
  9. W pliku AndroidManifest.xml otwórz 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 {
        // 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.*"
    }
            

Co dalej?