ปลั๊กอิน Secrets Gradle

Google ขอแนะนำอย่างยิ่งว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน แต่ให้จัดเก็บไว้ในไฟล์ secrets.properties ในเครื่อง ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์แต่ไม่รวมอยู่ในการควบคุมเวอร์ชัน จากนั้นใช้ปลั๊กอิน Secrets Gradle สำหรับ Android เพื่ออ่านคีย์ API

ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android จะอ่านข้อมูลลับ ซึ่งรวมถึงคีย์ API จากไฟล์พร็อพเพอร์ตี้ที่ไม่ได้ตรวจสอบในระบบควบคุมเวอร์ชัน จากนั้นปลั๊กอินจะแสดงพร็อพเพอร์ตี้เหล่านั้นเป็นตัวแปรในคลาส BuildConfig ที่ Gradle สร้างขึ้นและในไฟล์ Manifest ของ Android

ดูตัวอย่างที่สมบูรณ์ของการใช้ปลั๊กอิน Secrets Gradle สำหรับ Android เพื่อเข้าถึงคีย์ API ได้ที่ตั้งค่าโปรเจ็กต์ Android Studio

การติดตั้งและการใช้งาน

วิธีติดตั้งปลั๊กอินข้อมูลลับ Gradle สำหรับ Android และจัดเก็บคีย์ API

  1. ใน Android Studio ให้เปิดไฟล์ build.gradle.kts หรือ build.gradle ระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในบล็อก plugins

    Kotlin

    plugins {
        // ...
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false
    }

    ดึงดูด

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
    }
  2. เปิดไฟล์ build.gradle.kts หรือ build.gradle ระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบ plugins

    Kotlin

    plugins {
        id("com.android.application")
        // ...
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }

    ดึงดูด

    plugins {
        id 'com.android.application'
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }
  3. หากใช้ Android Studio ให้ซิงค์โปรเจ็กต์กับ Gradle
  4. เปิดไฟล์ secrets.properties ในไดเรกทอรีระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ แทนที่ YOUR_API_KEY ด้วยคีย์ API ของคุณ อย่าลืมเพิ่ม secrets.properties ลงในไฟล์ .gitignore (หรือยกเว้นจากระบบควบคุมเวอร์ชัน) เพื่อไม่ให้คุณตรวจสอบคีย์ API ในการควบคุมเวอร์ชัน
    MAPS_API_KEY=YOUR_API_KEY
  5. สร้างไฟล์ local.defaults.properties ในไดเรกทอรีระดับบนสุด ซึ่งเป็นโฟลเดอร์เดียวกับไฟล์ secrets.properties แล้วเพิ่มโค้ดต่อไปนี้

    MAPS_API_KEY=DEFAULT_API_KEY

    วัตถุประสงค์ของไฟล์นี้คือการระบุตำแหน่งสำรองสำหรับคีย์ API หากไม่พบไฟล์ secrets.properties เพื่อไม่ให้บิลด์ล้มเหลว กรณีนี้อาจเกิดขึ้นหากคุณโคลนแอปจากระบบควบคุมเวอร์ชันที่ละเว้น secrets.properties และคุณยังไม่ได้สร้างไฟล์ secrets.properties ในเครื่องเพื่อระบุคีย์ API

  6. ในไฟล์ AndroidManifest.xml ให้ไปที่ com.google.android.geo.API_KEY แล้วอัปเดตแอตทริบิวต์ android:value ดังนี้
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="${MAPS_API_KEY}" />
        

    หมายเหตุ: com.google.android.geo.API_KEY เป็นชื่อข้อมูลเมตาที่แนะนำสำหรับคีย์ API คีย์ที่มีชื่อนี้สามารถใช้เพื่อตรวจสอบสิทธิ์กับ API ที่ใช้ Google Maps หลายรายการในแพลตฟอร์ม Android รวมถึง Maps SDK สำหรับ Android API ยังรองรับชื่อ com.google.android.maps.v2.API_KEY เพื่อให้เข้ากันได้แบบย้อนหลังด้วย ชื่อเดิมนี้อนุญาตให้ตรวจสอบสิทธิ์กับ Android Maps API v2 เท่านั้น แอปพลิเคชันสามารถระบุชื่อข้อมูลเมตาของคีย์ API ได้เพียงชื่อเดียวเท่านั้น หากระบุทั้ง 2 อย่าง API จะแสดงข้อยกเว้น

  7. ใน Android Studio ให้เปิดไฟล์ build.gradle.kts หรือ build.gradle ระดับโมดูล แล้วแก้ไขพร็อพเพอร์ตี้ secrets หากไม่มีพร็อพเพอร์ตี้ secrets ให้เพิ่มพร็อพเพอร์ตี้ดังกล่าว

    แก้ไขพร็อพเพอร์ตี้ของปลั๊กอินเพื่อตั้งค่า propertiesFileName เป็น "secrets.properties", ตั้งค่า defaultPropertiesFileName เป็น "local.defaults.properties" และตั้งค่าพร็อพเพอร์ตี้อื่นๆ

    Kotlin

    secrets {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root 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"
    }

    ดึงดูด

    secrets {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root 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"
    }

ขั้นตอนถัดไป