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 มีดังนี้
-
ใน 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") } }
-
เปิดไฟล์
build.gradle
ระดับโมดูล และเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบplugins
ดึงดูด
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- ในไฟล์
build.gradle
ระดับโมดูล ให้ตรวจสอบว่าtargetSdk
และcompileSdk
ตั้งค่าเป็น 34 - บันทึกไฟล์และซิงค์โปรเจ็กต์กับ Gradle
-
เปิดไฟล์
secrets.properties
ในไดเรกทอรีระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ แทนที่YOUR_API_KEY
ด้วยคีย์ API ของคุณ เก็บคีย์ของคุณไว้ในไฟล์นี้เนื่องจากsecrets.properties
ได้รับการยกเว้นจากการตรวจสอบในระบบควบคุมเวอร์ชันPLACES_API_KEY=YOUR_API_KEY
- บันทึกไฟล์
-
สร้างไฟล์
local.defaults.properties
ในไดเรกทอรีระดับบนสุดโดยใช้โฟลเดอร์เดียวกับไฟล์secrets.properties
แล้วเพิ่มโค้ดต่อไปนี้PLACES_API_KEY=DEFAULT_API_KEY
วัตถุประสงค์ของไฟล์นี้คือการระบุตำแหน่งข้อมูลสำรองสำหรับคีย์ API หากไม่พบไฟล์
secrets.properties
เพื่อให้บิลด์ทำงานไม่สำเร็จ กรณีนี้อาจเกิดขึ้นหากคุณโคลนแอปจากระบบควบคุมเวอร์ชันซึ่งละเว้นsecrets.properties
และยังไม่ได้สร้างไฟล์secrets.properties
ในเครื่องเพื่อระบุคีย์ API - บันทึกไฟล์
-
ใน 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.*" }
ขั้นตอนถัดไป
- ดูหน้าปลั๊กอิน Secrets Gradle สำหรับ Android ที่โปรเจ็กต์ GitHub
- ดูตัวอย่างการใช้ปลั๊กอินได้ที่ตั้งค่าโปรเจ็กต์ Android Studio