Trình bổ trợ Secrets Gradle

Bạn không nên kiểm tra khoá API trong hệ thống quản lý phiên bản. Thay vào đó, bạn nên lưu trữ nó trong tệp secrets.properties cục bộ, nằm trong thư mục gốc của dự án nhưng bị loại trừ khỏi hệ thống quản lý phiên bản và sau đó hãy dùng trình bổ trợ Secrets Gradle cho Android để đọc khoá API.

Trình bổ trợ Secrets Gradle cho Android đọc các khoá bí mật, bao gồm cả khoá API, từ tệp thuộc tính chưa được kiểm tra trong hệ thống quản lý phiên bản. Sau đó, trình bổ trợ này sẽ hiển thị các thuộc tính đó dưới dạng các biến trong lớp BuildConfig do Gradle tạo và trong tệp kê khai Android.

Để xem ví dụ đầy đủ về cách sử dụng Trình bổ trợ Gradle Secrets cho Android để truy cập vào một khoá API, hãy xem bài viết Thiết lập dự án Android Studio.

Cài đặt và sử dụng

Cách cài đặt trình bổ trợ Secrets Gradle cho Android trong dự án Google Maps:

  1. Trong Android Studio, hãy mở build.gradle hoặc build.gradle.kts cấp cao nhất rồi thêm mã sau vào phần tử dependencies trong buildscript.

    Groovy

    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. Mở tệp build.gradle ở cấp mô-đun rồi thêm mã sau vào tệp Phần tử plugins.

    Groovy

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

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. Trong tệp build.gradle cấp mô-đun, hãy đảm bảo rằng targetSdkcompileSdk được đặt thành 34.
  4. Lưu tệp và đồng bộ hoá dự án với Gradle.
  5. Mở tệp secrets.properties trong thư mục cấp cao nhất của bạn, sau đó thêm sau đây. Thay thế YOUR_API_KEY bằng khoá API. Lưu trữ khoá của bạn trong tệp này vì secrets.properties không được kiểm tra trong phần kiểm soát phiên bản hệ thống.
    MAPS_API_KEY=YOUR_API_KEY
  6. Lưu tệp.
  7. Tạo tệp local.defaults.properties trong thư mục cấp cao nhất cũng bằng cách này làm tệp secrets.properties, rồi thêm mã sau.

    MAPS_API_KEY=DEFAULT_API_KEY

    Mục đích của tệp này là cung cấp vị trí sao lưu cho khoá API nếu Không tìm thấy tệp secrets.properties để các bản dựng không bị lỗi. Điều này có thể xảy ra nếu bạn sao chép ứng dụng từ một hệ thống quản lý phiên bản, trong đó bỏ qua secrets.properties và bạn chưa tạo tệp secrets.properties cục bộ để cung cấp Khoá API.

  8. Lưu tệp.
  9. Trong tệp AndroidManifest.xml, chuyển đến com.google.android.geo.API_KEY rồi cập nhật android:value attribute. Nếu thẻ <meta-data> không tồn tại, hãy tạo thẻ này làm thẻ con của Thẻ <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.*"
    }
            

Các bước tiếp theo