ปลั๊กอิน Secrets Gradle

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

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

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

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

วิธีติดตั้งปลั๊กอิน Secrets Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps มีดังนี้

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

    ดึงดูด

    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. เปิดไฟล์ build.gradle ระดับโมดูล และเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบ plugins

    ดึงดูด

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

    Kotlin

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

    PLACES_API_KEY=DEFAULT_API_KEY

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

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

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

    ดึงดูด

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

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