Google ไม่แนะนำให้คุณตรวจสอบคีย์ API ใน
ระบบควบคุมเวอร์ชัน แต่ควรจัดเก็บไว้ในไฟล์ secrets.properties
ในเครื่องแทน
ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์ของคุณ แต่ไม่รวมอยู่ในการควบคุมเวอร์ชัน
ใช้ปลั๊กอินข้อมูลลับ Gradle สำหรับ 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
ถูกยกเว้นจากการเช็คอินในการควบคุมเวอร์ชัน ระบบ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}" />
Note:
com.google.android.geo.API_KEY
is the recommended metadata name for the API key. A key with this name can be used to authenticate to multiple Google Maps-based APIs on the Android platform, including the Maps SDK for Android. For backwards compatibility, the API also supports the namecom.google.android.maps.v2.API_KEY
. This legacy name allows authentication to the Android Maps API v2 only. An application can specify only one of the API key metadata names. If both are specified, the API throws an exception. -
In Android Studio, open your module-level
build.gradle
orbuild.gradle.kts
file and edit thesecrets
property. If thesecrets
property does not exist, add it.Edit the properties of the plugin to set
propertiesFileName
tosecrets.properties
, setdefaultPropertiesFileName
tolocal.defaults.properties
, and set any other 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 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" // 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 { // 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" // 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.*" }
ขั้นตอนถัดไป
- ดูปลั๊กอินข้อมูลลับ Gradle สำหรับ Android หน้าโปรเจ็กต์ GitHub
- ดูตั้งค่าโปรเจ็กต์ Android Studio สำหรับตัวอย่างทั้งหมดของการใช้ ปลั๊กอิน
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-08-17 UTC
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"ไม่มีข้อมูลที่ฉันต้องการ" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป" },{ "type": "thumb-down", "id": "outOfDate", "label":"ล้าสมัย" },{ "type": "thumb-down", "id": "translationIssue", "label":"ปัญหาเกี่ยวกับการแปล" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด" },{ "type": "thumb-down", "id": "otherDown", "label":"อื่นๆ" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"เข้าใจง่าย" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"แก้ปัญหาของฉันได้" },{ "type": "thumb-up", "id": "otherUp", "label":"อื่นๆ" }]