คู่มือนี้แสดงข้อกำหนดในการกำหนดค่าบิลด์สำหรับการใช้ 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 และเพิ่มข้อมูลเข้าสู่ระบบคีย์ API โปรเจ็กต์ ต้องมีการจัดสรรเพื่อเข้าถึง Navigation SDK คีย์ทั้งหมดภายในโปรเจ็กต์คอนโซลระบบคลาวด์จะได้รับสิทธิ์เข้าถึง Navigation SDK เดียวกัน คีย์อาจเชื่อมโยงกับโปรเจ็กต์การพัฒนามากกว่า 1 โปรเจ็กต์ หากมีโปรเจ็กต์คอนโซลอยู่แล้ว คุณจะเพิ่มคีย์ลงในโปรเจ็กต์ปัจจุบันได้
วิธีตั้งค่า
- ลงชื่อเข้าใช้คอนโซล Cloud ในเว็บเบราว์เซอร์ที่คุณชื่นชอบ แล้วสร้างโปรเจ็กต์คอนโซล Cloud
- ใน IDE เช่น Android Studio ให้สร้างโปรเจ็กต์การพัฒนาแอป Android และจดชื่อแพ็กเกจไว้
- โปรดติดต่อตัวแทน Google Maps Platform เพื่อให้สิทธิ์เข้าถึง 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 สําหรับ Android- Gradle- เพิ่มโค้ดต่อไปนี้ลงใน - 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 SDK- Gradle- เพิ่มโค้ดต่อไปนี้ลงใน - 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ภายในbuildscriptKotlinplugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.jetbrains.kotlin.android) apply false alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.secrets.gradle.plugin) apply false } ดึงดูดbuildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } } 
- 
    เปิดไฟล์ build.gradle.ktsหรือbuild.gradleระดับโมดูล แล้วเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบpluginsKotlinplugins { // ... alias(libs.plugins.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และตั้งค่าพร็อพเพอร์ตี้อื่นๆKotlinsecrets { // 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