ตั้งค่าโปรเจ็กต์ Android Studio

Navigation SDK มีให้บริการผ่าน Maven หลัง ที่คุณสร้างโครงการพัฒนาขึ้นมา คุณสามารถผสานรวม SDK เข้ากับโปรเจ็กต์ได้โดยใช้ วิธีใดวิธีหนึ่งต่อไปนี้

รายการต่อไปนี้ใช้ที่เก็บ google() ของ Maven ซึ่งใช้งานง่ายที่สุด วิธีที่แนะนำในการเพิ่ม Navigation SDK ลงในโปรเจ็กต์ของคุณ

  1. เพิ่มทรัพยากร Dependency ต่อไปนี้ในการกำหนดค่า Gradle หรือ Maven โดยแทนที่ตัวยึดตำแหน่ง VERSION_NUMBER ด้วยค่า เวอร์ชันที่ต้องการของ Navigation SDK สำหรับ Android

    Gradle

    เพิ่มข้อมูลต่อไปนี้ลงใน build.gradle ระดับโมดูล

    dependencies {
        ...
        implementation 'com.google.android.libraries.navigation:navigation:VERSION_NUMBER'
    }
    

    Maven

    เพิ่มสิ่งต่อไปนี้ใน pom.xml

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.libraries.navigation</groupId>
        <artifactId>navigation</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    
  2. หากคุณมีทรัพยากร Dependency ที่ใช้ Maps SDK คุณต้องยกเว้น Dependency ในทรัพยากร Dependency ที่ประกาศแต่ละรายการที่ต้องใช้ Maps SDK

    Gradle

    เพิ่มรายการต่อไปนี้ใน build.gradle ระดับบนสุด:

    allprojects {
        ...
        // Required: you must exclude the Google Play service Maps SDK from
        // your transitive dependencies. This is to ensure 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>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>
    

กำหนดค่าบิลด์

หลังจากสร้างโปรเจ็กต์แล้ว คุณจะกำหนดการตั้งค่าสำหรับ ในการสร้างและใช้ Navigation SDK อย่างถูกต้อง

อัปเดตพร็อพเพอร์ตี้ในพื้นที่

  • ในโฟลเดอร์ Gradle Scripts ให้เปิดไฟล์ local.properties และเพิ่ม android.useDeprecatedNdk=true

อัปเดตสคริปต์บิลด์ Gradle

  • เปิดไฟล์ build.gradle (Module:app) และใช้หลักเกณฑ์ต่อไปนี้ เพื่ออัปเดตการตั้งค่าให้เป็นไปตาม SDK การนำทางและพิจารณาการตั้งค่า ตัวเลือกการเพิ่มประสิทธิภาพได้อีกด้วย

    การตั้งค่าที่จำเป็นสำหรับ SDK การนำทาง

    1. ตั้งค่า minSdkVersion เป็น 23 ขึ้นไป
    2. ตั้งค่า targetSdkVersion เป็น 33 ขึ้นไป
    3. เพิ่มการตั้งค่า dexOptions ที่จะเพิ่ม javaMaxHeapSize
    4. ตั้งค่าตำแหน่งสำหรับไลบรารีเพิ่มเติม
    5. เพิ่ม repositories และ dependencies สำหรับ SDK การนำทาง
    6. แทนที่หมายเลขเวอร์ชันในทรัพยากร Dependency ด้วยเวอร์ชันล่าสุด เวอร์ชันที่พร้อมใช้งาน

    การตั้งค่าที่ไม่บังคับเพื่อลดเวลาบิลด์

    • เปิดใช้งานการย่อโค้ดและทรัพยากร เล็กลง โดยใช้ R8/ProGuard เพื่อนำโค้ดและทรัพยากรที่ไม่ได้ใช้ออกจากทรัพยากร Dependency ถ้า ขั้นตอน R8/ProGuard ใช้เวลาเรียกใช้นานเกินไป ลองเปิดใช้ multidex สำหรับ งานพัฒนา
    • ลดจำนวนการแปลเป็นภาษาที่รวมอยู่ในบิลด์: ตั้งค่า resConfigs สำหรับ 1 ภาษาระหว่างการพัฒนา สำหรับบิลด์สุดท้าย ตั้งค่า resConfigs สำหรับภาษาที่คุณใช้จริง โดยค่าเริ่มต้น Gradle รวมสตริงทรัพยากรสำหรับทุกภาษาที่รองรับโดย SDK การนำทาง

    เพิ่มการละลายน้ำตาลสำหรับการรองรับ Java8

    • หากกำลังสร้างแอปโดยใช้ปลั๊กอิน Android Gradle 4.0.0 หรือ ที่สูงขึ้น ปลั๊กอินจะขยายการสนับสนุนสำหรับการใช้ภาษา Java 8 จำนวนหนึ่ง API ดู Java 8 desugaring การสนับสนุน เพื่อดูข้อมูลเพิ่มเติม ดูตัวอย่างข้อมูลโค้ดสคริปต์ของบิลด์ด้านล่าง คอมไพล์และตัวเลือกการอ้างอิง

ด้านล่างเป็นตัวอย่างของสคริปต์บิลด์ Gradle สำหรับแอปพลิเคชัน ตรวจสอบ แอปตัวอย่างสำหรับชุดทรัพยากร Dependency ที่อัปเดตแล้ว เป็นเวอร์ชันของ SDK การนำทางที่คุณใช้อยู่อาจนำหน้าไปเล็กน้อยหรือ ที่อยู่เบื้องหลังเอกสารนี้

apply plugin: 'com.android.application'

ext {
    navSdk = "__NAVSDK_VERSION__"
}

android {
    compileSdk 33
    buildToolsVersion='28.0.3'

    defaultConfig {
        applicationId "<your id>"
        // Navigation SDK supports SDK 23 and later.
        minSdkVersion 23
        targetSdkVersion 33
        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 {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    // Navigation SDK for Android and other libraries are hosted on Google's Maven repository.
    google()
}

dependencies {
    // Include the Google Navigation SDK.
    // Note: remember to exclude Google Play service Maps SDK from your transitive
    // dependencies to avoid duplicate copies of the Google Maps SDK.
    api "com.google.android.libraries.navigation:navigation:${navSdk}"

    // Declare other dependencies for your app here.

    annotationProcessor "androidx.annotation:annotation:1.7.0"
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.9'
}

เพิ่มคีย์ API ลงในแอป

ส่วนนี้จะอธิบายวิธีจัดเก็บคีย์ API เพื่อให้ใช้อ้างอิงได้อย่างปลอดภัยโดย แอปของคุณ คุณไม่ควรตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน เราจึงขอแนะนำ จัดเก็บไว้ในไฟล์ secrets.properties ซึ่งอยู่ในไดเรกทอรีรากของ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์ secrets.properties ได้ที่ ไฟล์คุณสมบัติ Gradle

เราขอแนะนำให้คุณใช้ ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android

วิธีติดตั้งปลั๊กอิน Secrets Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps

  1. ใน 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"
        }
    }
    
  2. เปิดไฟล์ 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'
    }
  3. ในไฟล์ build.gradle.kts หรือ build.gradle ระดับโมดูล ให้ตรวจสอบว่า มีการตั้งค่า targetSdk และ compileSdk เป็น 34
  4. บันทึกไฟล์และ ซิงค์โปรเจ็กต์กับ Gradle
  5. เปิดไฟล์ secrets.properties ในไดเรกทอรีระดับบนสุด แล้วเพิ่ม โค้ดต่อไปนี้ แทนที่ YOUR_API_KEY ด้วยคีย์ API จัดเก็บคีย์ของคุณในไฟล์นี้ เนื่องจาก secrets.properties ถูกยกเว้นจากการเช็คอินในการควบคุมเวอร์ชัน ระบบ
    NAV_API_KEY=YOUR_API_KEY
  6. บันทึกไฟล์
  7. สร้างไฟล์ local.defaults.properties ในไดเรกทอรีระดับบนสุด เป็นไฟล์ secrets.properties แล้วเพิ่มโค้ดต่อไปนี้

    NAV_API_KEY=DEFAULT_API_KEY

    วัตถุประสงค์ของไฟล์นี้คือให้ตำแหน่งข้อมูลสำรองสำหรับคีย์ API หาก ไม่พบไฟล์ secrets.properties เพื่อไม่ให้บิลด์ล้มเหลว เหตุการณ์นี้อาจเกิดขึ้นได้หาก คุณโคลนแอปจากระบบควบคุมเวอร์ชันที่ยกเว้น secrets.properties และ คุณยังไม่ได้สร้างไฟล์ secrets.properties ในเครื่องเพื่อระบุ คีย์ API

  8. บันทึกไฟล์
  9. ในไฟล์ 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 คีย์ที่ใช้ชื่อนี้สามารถใช้เพื่อตรวจสอบสิทธิ์กับ API บน Google Maps บนแพลตฟอร์ม Android ซึ่งรวมถึง Navigation SDK สำหรับ Android สำหรับความเข้ากันได้แบบย้อนหลัง API ยัง รองรับชื่อ com.google.android.maps.v2.API_KEY มรดกนี้ ชื่ออนุญาตการตรวจสอบสิทธิ์กับ Android Maps API v2 เท่านั้น แอปพลิเคชันสามารถ ระบุชื่อข้อมูลเมตาของคีย์ API เพียงชื่อเดียว หากระบุไว้ทั้งคู่ API มีข้อยกเว้น

  10. ใน 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"
    
        // 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 {
        // 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.*"
    }
            

ใส่การระบุแหล่งที่มาที่จำเป็นในแอปของคุณ

ถ้าคุณใช้ Navigation SDK สำหรับ Android ในแอปของคุณ คุณจะต้องใส่ ข้อความระบุแหล่งที่มาและใบอนุญาตโอเพนซอร์สเป็นส่วนหนึ่งของประกาศทางกฎหมายของแอป

คุณสามารถดูข้อความระบุแหล่งที่มาและใบอนุญาตโอเพนซอร์สที่จำเป็นใน SDK การนำทางสำหรับไฟล์ ZIP ของ Android:

  • NOTICE.txt
  • LICENSES.txt

หากคุณเป็นลูกค้า Mobility หรือ Fleet Engine Deliveries

หากคุณเป็นลูกค้า Mobility หรือ Fleet Engine Deliveries โปรดดูข้อมูลเกี่ยวกับ การเรียกเก็บเงินในเอกสารประกอบของ Mobility สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ แล้วบันทึกธุรกรรม ตั้งค่าการเรียกเก็บเงิน บันทึกธุรกรรมที่เรียกเก็บเงินได้ การรายงาน และ บันทึกธุรกรรมที่เรียกเก็บเงินได้ (Android)