ปลั๊กอิน Secrets Gradle

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

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

ดูตัวอย่างที่เสร็จสมบูรณ์ของการใช้ปลั๊กอินข้อมูลลับ 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
    }

    Groovy

    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")
    }

    Groovy

    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 ในการควบคุมเวอร์ชัน
    PLACES_API_KEY=YOUR_API_KEY
  5. สร้างไฟล์ local.defaults.properties ในไดเรกทอรีระดับบนสุด ซึ่งเป็นโฟลเดอร์เดียวกับไฟล์ secrets.properties แล้วเพิ่มโค้ดต่อไปนี้

    PLACES_API_KEY=DEFAULT_API_KEY

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

  6. ใน 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"
    }

    Groovy

    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"
    }

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