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
ได้รับการยกเว้นจากการตรวจสอบในระบบควบคุมเวอร์ชัน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 { // 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
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-08-13 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":"อื่นๆ" }]