ตั้งค่าโครงการของคุณ -- เวอร์ชัน 4.99 และเวอร์ชันก่อนหน้า

คู่มือนี้แสดงข้อกำหนดการกำหนดค่าบิลด์สำหรับการใช้ Navigation SDK สำหรับ Android วิธีการนี้จะถือว่าคุณมี Android IDE ติดตั้งไว้และคุ้นเคยกับการพัฒนาซอฟต์แวร์ Android

ข้อกำหนดขั้นต่ำสำหรับการใช้ Navigation SDK

ข้อกำหนดเหล่านี้มีผลกับ Navigation SDK สำหรับ Android เวอร์ชัน 4.99 และเก่ากว่า

ตั้งค่าโปรเจ็กต์: โปรเจ็กต์ Cloud Console และโปรเจ็กต์ Android

คุณต้องสร้างโปรเจ็กต์ Cloud Console และเพิ่มข้อมูลเข้าสู่ระบบคีย์ API ก่อนจึงจะสร้างหรือทดสอบแอปได้ โปรเจ็กต์ต้องมีการจัดสรรเพื่อเข้าถึง Navigation SDK คีย์ทั้งหมดภายในโปรเจ็กต์ Cloud Console จะได้รับสิทธิ์เข้าถึง Navigation SDK ในแบบเดียวกัน คีย์หนึ่งๆ จะมีโปรเจ็กต์การพัฒนาที่เชื่อมโยงอยู่ได้มากกว่า 1 โปรเจ็กต์ หากมีโปรเจ็กต์คอนโซลอยู่แล้ว คุณเพิ่มคีย์ลงในโปรเจ็กต์ปัจจุบันได้

วิธีการตั้งค่า

  1. ในเว็บเบราว์เซอร์ที่คุณชื่นชอบ ให้ลงชื่อเข้าใช้ Cloud Console แล้วสร้างโปรเจ็กต์ Cloud Console
  2. ใน IDE เช่น Android Studio ให้สร้างโครงการการพัฒนาแอป Android และบันทึกชื่อแพ็กเกจ
  3. ติดต่อตัวแทน Google Maps Platform เพื่อขอสิทธิ์เข้าถึง SDK การนำทางสำหรับโปรเจ็กต์ Cloud Console
  4. ขณะอยู่ในแดชบอร์ด Cloud Console ในเว็บเบราว์เซอร์ ให้สร้างข้อมูลเข้าสู่ระบบเพื่อสร้างคีย์ API ที่มีข้อจำกัด
  5. ในหน้าคีย์ API ให้คลิกแอป Android ในพื้นที่การจำกัดแอปพลิเคชัน
  6. คลิกเพิ่มชื่อแพ็กเกจและลายนิ้วมือ แล้วป้อนชื่อแพ็กเกจของโปรเจ็กต์การพัฒนาและลายนิ้วมือ SHA-1 สำหรับคีย์ดังกล่าว
  7. คลิกบันทึก

เพิ่ม Navigation SDK ลงในโปรเจ็กต์

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

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

  1. เพิ่มทรัพยากร 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. 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>com.google.android.libraries.navigation</groupId>
        <artifactId>navigation</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    

    หากคุณมีทรัพยากร Dependency ที่ใช้ Maps SDK คุณจะต้องยกเว้นทรัพยากร 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 และไลบรารีอื่นๆ ในช่วงการเปลี่ยนผ่าน ในการใช้ทรัพยากร Dependency นี้ คุณจะต้องเข้าสู่ระบบโปรเจ็กต์ระบบคลาวด์ผ่าน gcloud เมื่อคอมไพล์

  1. ตั้งค่าสภาพแวดล้อมเพื่อเข้าถึงที่เก็บ Maven ของ Google ตามที่อธิบายไว้ในส่วนข้อกำหนดเบื้องต้นของเอกสารประกอบของ Consumer SDK การเข้าถึง SDK การนำทางจะควบคุมผ่านกลุ่มพื้นที่ทำงาน
  2. เพิ่มทรัพยากร 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. 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>com.google.android.maps</groupId>
        <artifactId>navsdk</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    

    หากคุณมีทรัพยากร Dependency ที่ใช้ Maps SDK คุณจะต้องยกเว้นทรัพยากร 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>
    

Navigation SDK ยังมีให้บริการเป็นแพ็กเกจ AAR ด้วย หลังจากสร้างโปรเจ็กต์การพัฒนาแล้ว คุณจะผสานรวม SDK ได้ วิธีการเหล่านี้มีไว้สำหรับการใช้ Android Studio สำหรับ IDE

  1. ดาวน์โหลด Navigation SDK เวอร์ชันล่าสุดจาก Google ไดรฟ์ที่แชร์ และแตกไฟล์ ZIP หากไม่มีสิทธิ์เข้าถึง โปรดติดต่อตัวแทน

  2. ใน Android Studio ให้เปิดโปรเจ็กต์และเพิ่มแพ็กเกจบริการ Google Play โดยใช้ SDK Manager

  3. จากไดเรกทอรีไฟล์ ZIP ให้คัดลอก libs/google_navigation_navmap.aar ลงในไดเรกทอรี app/libs ของโปรเจ็กต์

  4. เพิ่มโค้ดต่อไปนี้ลงใน 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. 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'
            }
        }
    }
    

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

หลังจากสร้างโปรเจ็กต์แล้ว คุณจะกำหนดการตั้งค่าสำหรับบิลด์และการใช้ Navigation SDK ที่ประสบความสำเร็จได้

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

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

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

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

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

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

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

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

ด้านล่างนี้เป็นตัวอย่างสคริปต์บิลด์ Gradle สำหรับแอปพลิเคชัน ตรวจสอบแอปตัวอย่างเพื่อดูชุดทรัพยากร Dependency ที่อัปเดตแล้ว เนื่องจากเวอร์ชันของ 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. Ensure 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

เราขอแนะนำให้ใช้ปลั๊กอิน Secrets Gradle สำหรับ Android เพื่อปรับปรุงงานนี้

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

  1. ใน 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")
        }
    }
    
  2. เปิดไฟล์ build.gradle ระดับโมดูล และเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบ plugins

    ดึงดูด

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. ในไฟล์ build.gradle ระดับโมดูล ให้ตรวจสอบว่า targetSdk และ compileSdk ตั้งค่าเป็น 34
  4. บันทึกไฟล์และซิงค์โปรเจ็กต์กับ Gradle
  5. เปิดไฟล์ secrets.properties ในไดเรกทอรีระดับบนสุด แล้วเพิ่มโค้ดต่อไปนี้ แทนที่ YOUR_API_KEY ด้วยคีย์ API ของคุณ เก็บคีย์ของคุณไว้ในไฟล์นี้เนื่องจาก secrets.properties ได้รับการยกเว้นจากการตรวจสอบในระบบควบคุมเวอร์ชัน
    MAPS_API_KEY=YOUR_API_KEY
  6. บันทึกไฟล์
  7. สร้างไฟล์ local.defaults.properties ในไดเรกทอรีระดับบนสุดโดยใช้โฟลเดอร์เดียวกับไฟล์ secrets.properties แล้วเพิ่มโค้ดต่อไปนี้

    MAPS_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}" />

    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 Navigation SDK for Android. For backwards compatibility, the API also supports the name com.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.

  10. In Android Studio, open your module-level build.gradle or build.gradle.kts file and edit the secrets property. If the secrets property does not exist, add it.

    Edit the properties of the plugin to set propertiesFileName to secrets.properties, set defaultPropertiesFileName to local.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.*"
    }
            

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

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

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

  • NOTICE.txt
  • LICENSES.txt