Google ขอแนะนําอย่างยิ่งว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน แต่คุณควรจัดเก็บไว้ในไฟล์ secrets.properties ในพื้นที่ ซึ่งอยู่ในไดเรกทอรีรูทของโปรเจ็กต์แต่ยกเว้นจากการควบคุมเวอร์ชัน จากนั้นใช้ปลั๊กอิน Gradle สำหรับความลับของ Android เพื่ออ่านคีย์ API
ปลั๊กอินข้อมูลลับ Gradle สําหรับ Android จะอ่านข้อมูลลับ ซึ่งรวมถึงคีย์ API จากไฟล์พร็อพเพอร์ตี้ที่ไม่ได้เช็คอินไว้ในระบบควบคุมเวอร์ชัน จากนั้นปลั๊กอินจะแสดงพร็อพเพอร์ตี้เหล่านั้นเป็นตัวแปรในคลาส BuildConfig ที่ Gradle สร้างขึ้น และในไฟล์ Manifest ของ Android
ดูตัวอย่างการใช้งานปลั๊กอินข้อมูลลับ Gradle สําหรับ Android เพื่อเข้าถึงคีย์ API ได้ที่ตั้งค่าโปรเจ็กต์ Android Studio
การติดตั้งและการใช้งาน
วิธีติดตั้งปลั๊กอินข้อมูลลับ Gradle สําหรับ Android ในโปรเจ็กต์ Google Maps
- 
    ใน Android Studio ให้เปิดไฟล์ build.gradle.ktsหรือbuild.gradleระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบdependenciesใต้buildscriptKotlinplugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.jetbrains.kotlin.android) apply false alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.secrets.gradle.plugin) apply false } ดึงดูดbuildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } } 
- 
    เปิดไฟล์ build.gradle.ktsหรือbuild.gradleระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบpluginsKotlinplugins { // ... alias(libs.plugins.secrets.gradle.plugin) } ดึงดูดplugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' } 
- ในไฟล์ build.gradle.ktsหรือbuild.gradleระดับโมดูล ให้ตรวจสอบว่าได้ตั้งค่าtargetSdkและcompileSdkเป็น 34
- ซิงค์โปรเจ็กต์กับ Gradle
- 
    เปิดไฟล์ secrets.propertiesในไดเรกทอรีระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ แทนที่YOUR_API_KEYด้วยคีย์ API ของคุณ เก็บคีย์ไว้ในไฟล์นี้เนื่องจากsecrets.propertiesได้รับการยกเว้นไม่ให้ตรวจสอบในระบบควบคุมเวอร์ชันMAPS_API_KEY=YOUR_API_KEY 
- 
    สร้างไฟล์ local.defaults.propertiesในไดเรกทอรีระดับบนสุด ซึ่งเป็นโฟลเดอร์เดียวกับไฟล์secrets.propertiesแล้วเพิ่มโค้ดต่อไปนี้MAPS_API_KEY=DEFAULT_API_KEY วัตถุประสงค์ของไฟล์นี้คือระบุตำแหน่งสำรองสำหรับคีย์ API ในกรณีที่ไม่พบไฟล์ secrets.propertiesเพื่อให้การสร้างไม่ล้มเหลว กรณีนี้อาจเกิดขึ้นได้หากคุณโคลนแอปจากระบบควบคุมเวอร์ชันที่ไม่ได้ใส่secrets.propertiesและคุณยังไม่ได้สร้างไฟล์secrets.propertiesในเครื่องเพื่อระบุคีย์ API
- 
    ในไฟล์ AndroidManifest.xmlให้ไปที่com.google.android.geo.API_KEYแล้วอัปเดตandroid:value attributeหากไม่มีแท็ก<meta-data>ให้สร้างแท็กดังกล่าวเป็นแท็กย่อยของแท็ก<application><meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" /> หมายเหตุ: com.google.android.geo.API_KEYคือชื่อข้อมูลเมตาที่แนะนําสำหรับคีย์ API คีย์ที่มีชื่อนี้สามารถใช้เพื่อตรวจสอบสิทธิ์กับ API หลายรายการที่ทำงานบนแพลตฟอร์ม Android ซึ่งรวมถึง Maps SDK สำหรับ Android API ยังรองรับชื่อcom.google.android.maps.v2.API_KEYเพื่อความเข้ากันได้แบบย้อนหลังด้วย ชื่อเดิมนี้อนุญาตให้ตรวจสอบสิทธิ์กับ Android Maps API v2 เท่านั้น แอปพลิเคชันจะระบุชื่อข้อมูลเมตาของคีย์ API ได้เพียงชื่อเดียวเท่านั้น หากระบุทั้ง 2 รายการ API จะแสดงข้อยกเว้น
- 
    ใน Android Studio ให้เปิดไฟล์ build.gradle.ktsหรือbuild.gradleระดับโมดูล แล้วแก้ไขพร็อพเพอร์ตี้secretsหากไม่มีพร็อพเพอร์ตี้secretsให้เพิ่มพร็อพเพอร์ตี้นั้นแก้ไขพร็อพเพอร์ตี้ของปลั๊กอินเพื่อตั้งค่า propertiesFileNameเป็นsecrets.properties, ตั้งค่าdefaultPropertiesFileNameเป็นlocal.defaults.propertiesและตั้งค่าพร็อพเพอร์ตี้อื่นๆKotlinsecrets { // 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" } ดึงดูด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" } 
ขั้นตอนถัดไป
- ดูหน้าโปรเจ็กต์ Secrets Gradle Plugin for Android ใน GitHub
- ดูตั้งค่าโปรเจ็กต์ Android Studio เพื่อดูตัวอย่างการใช้งานปลั๊กอินอย่างละเอียด