Google ขอแนะนำอย่างยิ่งว่าอย่าตรวจสอบคีย์ APIใน
ระบบควบคุมเวอร์ชัน แต่ให้เก็บไว้ในไฟล์
secrets.properties ในเครื่อง ซึ่งอยู่ในไดเรกทอรีรากของ
โปรเจ็กต์แต่ไม่รวมอยู่ในการควบคุมเวอร์ชัน จากนั้นใช้
ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android
เพื่ออ่านคีย์ API
ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android จะอ่านข้อมูลลับ ซึ่งรวมถึงคีย์ API จากไฟล์พร็อพเพอร์ตี้ที่ไม่ได้ตรวจสอบในระบบควบคุมเวอร์ชัน จากนั้นปลั๊กอินจะแสดงพร็อพเพอร์ตี้เหล่านั้นเป็นตัวแปรในคลาส BuildConfig ที่ Gradle สร้างขึ้นและในไฟล์ Manifest ของ Android
ดูตัวอย่างที่เสร็จสมบูรณ์ของการใช้ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android เพื่อเข้าถึงคีย์ API ได้ที่ ตั้งค่าโปรเจ็กต์ Android Studio
การติดตั้งและการใช้งาน
วิธีติดตั้งปลั๊กอินข้อมูลลับ Gradle สำหรับ Android และจัดเก็บคีย์ API
-
ใน Android Studio ให้เปิดไฟล์
build.gradle.ktsหรือbuild.gradleระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในบล็อกpluginsKotlin
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 }
-
เปิดไฟล์
build.gradle.ktsหรือbuild.gradleระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบpluginsKotlin
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' }
- หากใช้ Android Studio, ให้ซิงค์โปรเจ็กต์กับ Gradle
-
เปิดไฟล์
secrets.propertiesในไดเรกทอรีระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ แทนที่YOUR_API_KEYด้วยคีย์ API อย่าลืมเพิ่มsecrets.propertiesลงใน.gitignoreไฟล์ (หรือยกเว้นจากระบบควบคุมเวอร์ชัน) เพื่อไม่ให้ตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน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ดังนี้<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 จะแสดงข้อยกเว้น -
ใน 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" }
ขั้นตอนถัดไป
- ดูหน้าโปรเจ็กต์ Secrets Gradle Plugin for Android ใน GitHub
- ดูตัวอย่างที่เสร็จสมบูรณ์ของการใช้ปลั๊กอินได้ที่ ตั้งค่าโปรเจ็กต์ Android Studio สำหรับ