คู่มือนี้แสดงข้อกำหนดในการกำหนดค่าบิลด์สำหรับการใช้ Navigation SDK สำหรับ Android คำสั่งเหล่านี้จะถือว่าคุณได้ติดตั้ง Android IDE และคุ้นเคยกับการพัฒนาแอป Android
ข้อกำหนดขั้นต่ำสำหรับการใช้ Navigation SDK
ข้อกำหนดเหล่านี้มีผลกับ Navigation SDK สำหรับ Android เวอร์ชัน 4.99 และก่อนหน้า
-
โปรเจ็กต์ที่เปิดใช้ Navigation SDK หากต้องการจัดสรร ให้สอบถาม ตัวแทน Google Maps Platform
แอปของคุณต้องกำหนดเป้าหมายเป็น API ระดับ 30 ขึ้นไป
หากต้องการเรียกใช้แอปที่สร้างด้วย Navigation SDK อุปกรณ์ Android ต้องติดตั้งและเปิดใช้บริการ Google Play
ต้องเพิ่มข้อความระบุแหล่งที่มาและข้อความอนุญาตให้ใช้สิทธิ ลงในแอป
ตั้งค่าโปรเจ็กต์: โปรเจ็กต์ Cloud Console และโปรเจ็กต์ Android
ก่อนที่จะสร้างหรือทดสอบแอปได้ คุณต้องสร้าง โปรเจ็กต์ใน Cloud Console และเพิ่มข้อมูลเข้าสู่ระบบคีย์ API โปรเจ็กต์ ต้องมีการจัดสรรเพื่อเข้าถึง Navigation SDK คีย์ทั้งหมดภายในโปรเจ็กต์คอนโซลระบบคลาวด์จะได้รับสิทธิ์เข้าถึง Navigation SDK เดียวกัน คีย์อาจเชื่อมโยงกับโปรเจ็กต์การพัฒนามากกว่า 1 โปรเจ็กต์ หากมีโปรเจ็กต์คอนโซลอยู่แล้ว คุณจะเพิ่มคีย์ลงในโปรเจ็กต์ปัจจุบันได้
วิธีตั้งค่า
- ลงชื่อเข้าใช้คอนโซลระบบคลาวด์ในเว็บเบราว์เซอร์ที่คุณชื่นชอบ แล้วสร้างโปรเจ็กต์คอนโซลระบบคลาวด์
- ใน IDE เช่น Android Studio ให้สร้างโปรเจ็กต์การพัฒนาแอป Android และจดชื่อแพ็กเกจไว้
- โปรดติดต่อตัวแทนของแพลตฟอร์ม Google Maps เพื่อให้สิทธิ์เข้าถึง Navigation SDK สำหรับโปรเจ็กต์คอนโซลระบบคลาวด์
- ขณะอยู่ในแดชบอร์ด Cloud Console ในเว็บเบราว์เซอร์ ให้สร้างข้อมูลเข้าสู่ระบบเพื่อสร้างคีย์ API ที่มีข้อจำกัด
- ในหน้าคีย์ API ให้คลิกแอป Android ในส่วนข้อจำกัดของแอปพลิเคชัน
- คลิกเพิ่มชื่อแพ็กเกจและลายนิ้วมือ แล้วป้อนชื่อแพ็กเกจของโปรเจ็กต์การพัฒนาและลายนิ้วมือ SHA-1 สำหรับคีย์นั้น
- คลิกบันทึก
เพิ่ม Navigation SDK ลงในโปรเจ็กต์
Navigation SDK พร้อมใช้งานโดยใช้ Maven หรือเป็นแพ็กเกจ AAR หลังจากสร้างโปรเจ็กต์การพัฒนาแล้ว คุณจะผสานรวม SDK เข้ากับโปรเจ็กต์ได้โดยใช้แนวทางใดแนวทางหนึ่งต่อไปนี้
ใช้ Maven สำหรับ Navigation SDK v4.5 ขึ้นไป (แนะนำ)
ตัวอย่างต่อไปนี้ใช้google()
ที่เก็บ Maven ซึ่งเป็นวิธีที่ง่ายที่สุดและ
แนะนำในการเพิ่ม Navigation SDK ลงในโปรเจ็กต์
เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในการกําหนดค่า Gradle หรือ Maven โดยแทนที่ตัวยึดตําแหน่ง
VERSION_NUMBER
ด้วย เวอร์ชันของ Navigation SDK สําหรับ AndroidGradle
เพิ่มโค้ดต่อไปนี้ลงใน
build.gradle
ระดับโมดูลdependencies { ... implementation 'com.google.android.libraries.navigation:navigation:VERSION_NUMBER' }
หากอัปเกรดจากที่เก็บ Maven เดิม โปรดทราบว่าชื่อกลุ่มและชื่ออาร์ติแฟกต์มีการเปลี่ยนแปลง และไม่จำเป็นต้องใช้ปลั๊กอิน
com.google.cloud.artifactregistry.gradle-plugin
อีกต่อไปและเพิ่มโค้ดต่อไปนี้ลงใน
build.gradle
ระดับบนสุดallprojects { ... // Required: you must exclude the Google Play service Maps SDK from // your transitive dependencies to make nsure there won't be // multiple copies of Google Maps SDK in your binary, as the Navigation // SDK already bundles the Google Maps SDK. configurations { implementation { exclude group: 'com.google.android.gms', module: 'play-services-maps' } } }
Maven
เพิ่มโค้ดต่อไปนี้ใน
pom.xml
<dependencies> ... <dependency> <groupId>com.google.android.libraries.navigation</groupId> <artifactId>navigation</artifactId> <version>VERSION_NUMBER</version> </dependency> </dependencies>
หากมีทรัพยากร Dependency ที่ใช้ Maps SDK คุณต้องยกเว้นทรัพยากร Dependency ในทรัพยากร Dependency ที่ประกาศแต่ละรายการซึ่งอิงตาม Maps SDK
<dependencies> <dependency> <groupId>project.that.brings.in.maps</groupId> <artifactId>MapsConsumer</artifactId> <version>1.0</version> <exclusions> <!-- Navigation SDK already bundles Maps SDK. You must exclude it to prevent duplication--> <exclusion> <!-- declare the exclusion here --> <groupId>com.google.android.gms</groupId> <artifactId>play-services-maps</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
ใช้ Maven สำหรับ Navigation SDK ก่อนเวอร์ชัน 4.5 หรือกับ Driver SDK
Navigation SDK จะยังคงพร้อมใช้งานโดยใช้ที่เก็บ Maven ดั้งเดิมตลอดเวอร์ชัน v4 ที่เหลือ นี่คือไลบรารีเดียวกัน
ที่มีการอัปเดตทั้งหมดเหมือนกับเวอร์ชันด้านบน และให้ความเข้ากันได้กับ
Driver SDK และไลบรารีอื่นๆ ในช่วงการเปลี่ยนผ่าน
การใช้การอ้างอิงนี้ต้องเข้าสู่ระบบโปรเจ็กต์ระบบคลาวด์โดยใช้ gcloud
เมื่อคอมไพล์
- ตั้งค่าสภาพแวดล้อมเพื่อเข้าถึงที่เก็บ Maven ของ Google ตามที่อธิบายไว้ใน ส่วนข้อกำหนดเบื้องต้น ของเอกสารประกอบเกี่ยวกับ Consumer SDK การเข้าถึง Navigation SDK จะควบคุมผ่านกลุ่มพื้นที่ทำงาน
เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในการกำหนดค่า Gradle หรือ Maven โดยแทนที่ตัวยึดตำแหน่ง
VERSION_NUMBER
ด้วย เวอร์ชันของ Navigation SDKGradle
เพิ่มโค้ดต่อไปนี้ลงใน
build.gradle
ระดับโมดูลdependencies { ... implementation 'com.google.android.maps:navsdk:VERSION_NUMBER' }
และเพิ่มโค้ดต่อไปนี้ลงใน
build.gradle
ระดับบนสุดallprojects { ... // Required: you must exclude the Google Play service Maps SDK from // your transitive dependencies to make sure there won't be // multiple copies of Google Maps SDK in your binary, as the Navigation // SDK already bundles the Google Maps SDK. configurations { implementation { exclude group: 'com.google.android.gms', module: 'play-services-maps' } } }
Maven
เพิ่มโค้ดต่อไปนี้ใน
pom.xml
<dependencies> ... <dependency> <groupId>com.google.android.maps</groupId> <artifactId>navsdk</artifactId> <version>VERSION_NUMBER</version> </dependency> </dependencies>
หากมีทรัพยากร Dependency ที่ใช้ Maps SDK คุณต้องยกเว้นทรัพยากร Dependency ในทรัพยากร Dependency ที่ประกาศแต่ละรายการซึ่งอิงตาม Maps SDK
<dependencies> <dependency> <groupId>project.that.brings.in.maps</groupId> <artifactId>MapsConsumer</artifactId> <version>1.0</version> <exclusions> <!-- Navigation SDK already bundles Maps SDK. You must exclude it to prevent duplication--> <exclusion> <!-- declare the exclusion here --> <groupId>com.google.android.gms</groupId> <artifactId>play-services-maps</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
ใช้ชุด AAR ที่ดาวน์โหลด (ไม่แนะนำ)
นอกจากนี้ Navigation SDK ยังมีให้บริการในรูปแบบแพ็กเกจ AAR ด้วย หลังจากสร้างโปรเจ็กต์การพัฒนาแล้ว คุณจะผสานรวม SDK ได้ คำแนะนำเหล่านี้ถือว่าคุณใช้ Android Studio สำหรับ IDE
ดาวน์โหลด Navigation SDK เวอร์ชันล่าสุดจาก Google ไดรฟ์ที่แชร์แล้วแตกไฟล์ หากคุณไม่มีสิทธิ์เข้าถึง โปรดติดต่อตัวแทน
ใน Android Studio ให้เปิดโปรเจ็กต์และเพิ่มแพ็กเกจบริการ Google Play โดยใช้ SDK Manager
จากไดเรกทอรีไฟล์ ZIP ให้คัดลอก
libs/google_navigation_navmap.aar
ไปยังไดเรกทอรีapp/libs
ของโปรเจ็กต์เพิ่มโค้ดต่อไปนี้ลงใน
build.gradle
ระดับโมดูลimplementation(name: 'google_navigation_navmap', ext: 'aar')
และเพิ่มโค้ดต่อไปนี้ลงใน
build.gradle
ระดับบนสุดallprojects { ... // Required: you must exclude the Google Play service Maps SDK from // your transitive dependencies to make sure there won't be // multiple copies of Google Maps SDK in your binary, as the Navigation // SDK already bundles the Google Maps SDK. configurations { implementation { exclude group: 'com.google.android.gms', module: 'play-services-maps' } } }
กำหนดค่าบิวด์
หลังจากสร้างโปรเจ็กต์แล้ว คุณจะกำหนดค่าสำหรับการ สร้างและการใช้ Navigation SDK ให้สำเร็จได้
อัปเดตที่พักในพื้นที่
- ในโฟลเดอร์สคริปต์ Gradle ให้เปิดไฟล์
local.properties
แล้วเพิ่มandroid.useDeprecatedNdk=true
อัปเดตสคริปต์บิลด์ Gradle
เปิดไฟล์
build.gradle (Module:app)
และใช้หลักเกณฑ์ต่อไปนี้ เพื่ออัปเดตการตั้งค่าให้เป็นไปตามข้อกำหนดของ Navigation SDK และพิจารณาตั้งค่าตัวเลือกการเพิ่มประสิทธิภาพด้วยการตั้งค่าที่จำเป็นสำหรับ Navigation SDK
- ตั้งค่า
minSdkVersion
เป็น 23 ขึ้นไป - ตั้งค่า
targetSdkVersion
เป็น 30 ขึ้นไป - เพิ่ม
dexOptions
การตั้งค่าที่เพิ่มjavaMaxHeapSize
- ตั้งค่าตำแหน่งสำหรับไลบรารีเพิ่มเติม
- เพิ่ม
repositories
และdependencies
สำหรับ Navigation SDK - แทนที่หมายเลขเวอร์ชันใน Dependencies ด้วยเวอร์ชันล่าสุด ที่มีอยู่
การตั้งค่าที่ไม่บังคับเพื่อลดเวลาในการสร้าง
- เปิดใช้การลดขนาดโค้ดและการลดขนาดทรัพยากร โดยใช้ R8/ProGuard เพื่อนำโค้ดและทรัพยากรที่ไม่ได้ใช้งานออกจากทรัพยากร Dependency หากขั้นตอน R8/ProGuard ใช้เวลานานเกินไป ให้ลองเปิดใช้ Multidex สำหรับงานพัฒนา
- ลดจำนวนคำแปลภาษาที่รวมอยู่ในการสร้าง: ตั้งค่า
resConfigs
สำหรับภาษาเดียวในระหว่างการพัฒนา สําหรับบิลด์สุดท้าย ให้ตั้งค่าresConfigs
สําหรับภาษาที่คุณใช้จริง โดยค่าเริ่มต้น Gradle จะรวมสตริงทรัพยากรสำหรับทุกภาษาที่ Navigation SDK รองรับ
- ตั้งค่า
ด้านล่างเป็นตัวอย่างสคริปต์การสร้าง Gradle สำหรับแอปพลิเคชัน ดูชุดทรัพยากร Dependency ที่อัปเดตแล้วในแอปตัวอย่าง เนื่องจาก Navigation SDK เวอร์ชันที่คุณใช้อาจใหม่กว่าหรือเก่ากว่าเอกสารประกอบนี้เล็กน้อย
apply plugin: 'com.android.application'
apply plugin: 'com.google.cloud.artifactregistry.gradle-plugin'
ext {
androidxVersion = "1.0.0"
lifecycle_version = "1.1.1"
}
android {
compileSdkVersion 30
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "<your id>"
// Navigation SDK supports SDK 23 and later.
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
// Set this to the languages you actually use, otherwise you'll include resource strings
// for all languages supported by the Navigation SDK.
resConfigs "en"
multiDexEnabled true
}
dexOptions {
// This increases the amount of memory available to the dexer. This is required to build
// apps using the Navigation SDK.
javaMaxHeapSize "4g"
}
buildTypes {
// Run ProGuard. Note that the Navigation SDK includes its own ProGuard configuration.
// The configuration is included transitively by depending on the Navigation SDK.
// If the ProGuard step takes too long, consider enabling multidex for development work
// instead.
all {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
// This tells Gradle where to look to find additional libraries - in this case, the
// google_navigation_navmap.aar file.
repositories {
flatDir {
dirs 'libs'
}
google()
// Required for accessing the Navigation SDK on Google's Maven repository.
maven {
url "artifactregistry://us-west2-maven.pkg.dev/gmp-artifacts/transportation"
}
}
dependencies {
// Include the Google Navigation SDK
implementation 'com.google.android.maps:navsdk:4.4.0'
// The included AAR file under libs can be used instead of the Maven repository.
// Uncomment the line below and comment out the previous dependency to use
// the AAR file instead. Make sure that you add the AAR file to the libs directory.
// implementation(name: 'google_navigation_navmap', ext: 'aar')
// These dependencies are required for the Navigation SDK to function
// properly at runtime.
implementation 'org.chromium.net:cronet-fallback:69.3497.100'
// Optional for Cronet users:
// implementation 'org.chromium.net:cronet-api:69.3497.100'
implementation 'androidx.appcompat:appcompat:${androidxVersion}'
implementation 'androidx.cardview:cardview:${androidxVersion}'
implementation 'com.google.android.material:material:${androidxVersion}'
implementation 'androidx.mediarouter:mediarouter:${androidxVersion}'
implementation 'androidx.preference:preference:${androidxVersion}'
implementation 'androidx.recyclerview:recyclerview:${androidxVersion}'
implementation 'androidx.legacy:legacy-support-v4:${androidxVersion}'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.github.bumptech.glide:okhttp-integration:4.9.0'
implementation 'android.arch.lifecycle:common-java8:$lifecycle_version'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.android.datatransport:transport-api:2.2.0'
implementation 'com.google.android.datatransport:transport-backend-cct:2.2.0'
implementation 'com.google.android.datatransport:transport-runtime:2.2.0'
implementation 'joda-time:joda-time:2.9.9'
annotationProcessor 'androidx.annotation:annotation:1.1.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
เพิ่มคีย์ API ลงในแอป
ส่วนนี้อธิบายวิธีจัดเก็บคีย์ API เพื่อให้แอปอ้างอิงได้อย่างปลอดภัย คุณไม่ควรเช็คอินคีย์ API ในระบบควบคุมเวอร์ชัน ดังนั้นเราขอแนะนำให้จัดเก็บไว้ในไฟล์ secrets.properties
ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์ secrets.properties
ได้ที่ไฟล์พร็อพเพอร์ตี้ Gradle
เราขอแนะนำให้ใช้ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android เพื่อให้งานนี้มีประสิทธิภาพมากขึ้น
วิธีติดตั้งปลั๊กอินข้อมูลลับ Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps
-
ใน Android Studio ให้เปิดไฟล์
build.gradle.kts
หรือbuild.gradle
ระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบdependencies
ภายในbuildscript
Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
ดึงดูด
buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
-
เปิดไฟล์
build.gradle.kts
หรือbuild.gradle
ระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบplugins
Kotlin
plugins { // ... id("com.google.android.libraries.mapsplatform.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 คีย์ที่มีชื่อนี้ใช้เพื่อตรวจสอบสิทธิ์กับ Google Maps API หลายรายการในแพลตฟอร์ม Android ได้ ซึ่งรวมถึง Navigation 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 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" }
รวมการระบุแหล่งที่มาที่จำเป็นในแอป
หากใช้ Navigation SDK สำหรับ Android ในแอป คุณต้องระบุ ข้อความระบุแหล่งที่มาและใบอนุญาตโอเพนซอร์สเป็นส่วนหนึ่งของส่วนประกาศทางกฎหมายของแอป
คุณดูข้อความระบุแหล่งที่มาและใบอนุญาตโอเพนซอร์สที่จำเป็นได้ใน ไฟล์ ZIP ของ Navigation SDK สำหรับ Android โดยทำดังนี้
NOTICE.txt
LICENSES.txt