您可透過 Maven 取得 Navigation SDK。建立開發專案後,您可以使用下列其中一種方法將 SDK 整合至專案。
使用 Maven 為 Navigation SDK (建議)
以下將使用 google()
Maven 存放區,這是最簡單且
。
將下列依附元件新增至 Gradle 或 Maven 設定,並將
VERSION_NUMBER
預留位置替換為所需的 Navigation SDK for 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>
如果有任何依附元件使用 Maps SDK,請務必排除 所宣告依附元件中,每個需要用到 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)
檔案,然後按照下列規範操作 更新設定,以符合 Navigation SDK 並考慮設定 以及最佳化選項Navigation SDK 的必要設定
- 將
minSdkVersion
設為 23 以上。 - 將
targetSdkVersion
設為 33 以上。 - 新增會調高
javaMaxHeapSize
的dexOptions
設定。 - 設定其他程式庫的位置。
- 將
repositories
和dependencies
加入 Navigation SDK。 - 將依附元件中的版本號碼替換為最新版 可用的版本。
縮短建構時間的選用設定
- 啟用程式碼縮減和資源 縮減 使用 R8/ProGuard 從依附元件中移除未使用的程式碼和資源。如果 R8/ProGuard 步驟的執行時間過長,請考慮為開發工作啟用 multidex。
- 減少版本中包含的語言翻譯數量:在開發期間為一種語言設定
resConfigs
。針對最終版本,請為實際使用的語言設定resConfigs
。根據預設 包含以下項目的相關資源字串: Navigation SDK。
新增對 Java8 支援的脫糖功能
- 如果您是使用 Android Gradle 外掛程式 4.0.0 或 外掛程式,則外掛程式支援使用多種 Java 8 語言 相互整合請參閱 Java 8 脫糖程序 支援團隊 瞭解詳情如要瞭解如何操作,請參閱下方的建構指令碼程式碼片段範例 「編譯」和「依附元件」選項
- 將
以下提供應用程式的 Gradle 建構指令碼範例。查看 更新依附元件組合的範例應用程式,做為 您使用的 Navigation 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 屬性檔案的說明。
建議您使用 Secrets Gradle Plugin for Android 來簡化這項工作。
如要在 Google 地圖專案中安裝 Secrets Gradle Plugin for Android,請按照下列步驟操作:
-
在 Android Studio 中開啟頂層的
build.gradle.kts
或build.gradle
檔案, 然後將下列程式碼加進buildscript
下方的dependencies
元素。Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
Groovy
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") }
Groovy
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
不會登錄在版本管控系統中,因此請將金鑰儲存至該檔案。NAV_API_KEY=YOUR_API_KEY
- 儲存檔案。
-
在頂層目錄 (與
secrets.properties
檔案相同的資料夾) 中建立local.defaults.properties
檔案,然後加入下列程式碼。NAV_API_KEY=DEFAULT_API_KEY
如果找不到
secrets.properties
檔案,這個檔案便可做為 API 金鑰的備份位置,以確保建置程序不會失敗。如果您從略過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 金鑰的建議中繼資料名稱。具備這個名稱的金鑰可用來驗證 Android 平台上的多個 Google 地圖相關 API,包括 Navigation SDK for Android。為了兼顧回溯相容性,API 也會 支援名稱com.google.android.maps.v2.API_KEY
。這個舊版 名稱僅允許對 Android Maps 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" // 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.*" }
Groovy
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.*" }
在應用程式中加入必要的作者資訊
如果您在應用程式中使用 Android 版 Navigation SDK,則必須在 將作者資訊文字和開放原始碼授權納入應用程式的法律聲明中 專區。
如需必要的作者資訊文字和開放原始碼授權,請前往「 Navigation SDK for Android ZIP 檔案:
NOTICE.txt
LICENSES.txt
如果您是 Mobility 或 Fleet Engine Deliveries 客戶
如果您是 Mobility 或 Fleet Engine Deliveries 的客戶,請進一步瞭解 。如要進一步瞭解 錄製交易,請參閱 設定帳單, 記錄可計費交易; 報表,以及 記錄可計費交易 (Android)。