本指南列出使用 Navigation SDK for Android 的建構設定需求。此處的操作說明假設您已安裝 Android IDE,且熟悉 Android 開發作業。
使用 Navigation SDK 的最低需求
已啟用 Navigation SDK 的 Google Cloud 控制台專案。如要建立帳戶,請洽詢 Google 地圖平台代表。
您的應用程式必須指定 Navigation SDK (16 版) 以上版本。
如要執行使用 Navigation SDK 建構的應用程式,Android 裝置必須安裝並啟用 Google Play 服務。
您必須在應用程式中新增作者資訊和授權文字。
設定專案:Google Cloud 控制台專案和 Android 專案
開始建構或測試應用程式之前,您必須先建立 Cloud 控制台專案,並新增 API 金鑰憑證。專案必須具備佈建功能才能存取 Navigation SDK。Cloud 控制台專案中的所有金鑰都會獲得與 Navigation SDK 相同的存取權。一個金鑰可以有多個相關聯的開發專案。如果您已有控制台專案,就可以將金鑰新增至目前的專案。
設定
- 在您慣用的網路瀏覽器 (例如 Chrome) 中登入 Google Cloud 控制台,並建立 Google Cloud 控制台專案。
- 在 IDE (例如 Android Studio) 中,建立 Android 應用程式開發專案,並記下套件名稱。
- 如需為 Google Cloud 控制台專案提供 Navigation SDK 存取權,請與 Google 地圖平台代表聯絡。
- 在網路瀏覽器的 Google Cloud 控制台資訊主頁上,建立憑證即可產生設有限制的 API 金鑰。
- 在「API 金鑰」頁面中,按一下「應用程式限制」區域中的「Android 應用程式」。
- 按一下「Add the package name and fingerprint」,然後輸入開發專案的套件名稱和該金鑰的 SHA-1 指紋。
- 點按「儲存」。
在應用程式中新增 Navigation SDK
Navigation SDK 可做為基本套件。建立開發專案後,您就可以整合 SDK。以下操作說明假設您在 IDE 中使用 Android Studio。
下載並解壓縮 Navigation SDK ZIP 檔案。
在 Android Studio 中開啟專案,並使用 SDK Manager 新增 Google Play 服務套件。
從 ZIP 檔案目錄,將
libs/google_navigation.aar
複製到專案的app/libs
目錄。
設定建構
建立專案後,您可以調整設定,確保順利建構並使用 Navigation SDK。
更新本機資源
- 在「Gradle Scripts」資料夾中開啟
local.properties
檔案,並新增android.useDeprecatedNdk=true
。
更新 Gradle 建構指令碼
開啟
build.gradle (Module:app)
檔案,並按照下列指南更新設定,以符合 Navigation SDK 的需求,並考慮設定最佳化選項。Navigation SDK 的必要設定
- 將
minSdkVersion
設為 16 以上。 - 為 API 21 設定「targetSDKversion」,將權限處理設定轉移至 API。針對舊版,您必須在應用程式中明確加入權限流程。
- 新增會增加
javaMaxHeapSize
的dexOptions
設定。 - 設定其他程式庫的位置。
- 為 Navigation SDK 新增
repositories
和dependencies
。 - 請將依附元件中的版本號碼替換成最新可用版本。
可縮短建構時間的選用設定
- 將
以下是應用程式的 Gradle 建構指令碼範例。
apply plugin: 'com.android.application'
ext {
supportVersion = "27.1.1"
lifecycle_version = "1.1.1"
}
android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.navigationapidemo"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
resConfigs "en"
}
dexOptions {
// This increases the amount of memory available to the dexer. This is
// required to build apps using the Navigation SDK.
javaMaxHeapSize "4g"
}
buildTypes {
all {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
// Specify where to find additional libraries and the location of google_navigation.aar file.
repositories {
flatDir {
dirs 'libs'
}
google()
}
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
// Include the Google Navigation API library
api(name: 'google_navigation', ext: 'aar')
// These dependencies are required for the Navigation API to function
// properly at runtime.
api "org.chromium.net:cronet-fallback:69.3497.100"
// Optional for Cronet users:
// api "org.chromium.net:cronet-api:69.3497.100"
api "com.android.support:appcompat-v7:${supportVersion}"
api "com.android.support:cardview-v7:${supportVersion}"
api "com.android.support:design:${supportVersion}"
api "com.android.support:mediarouter-v7:${supportVersion}"
api "com.android.support:preference-v7:${supportVersion}"
api "com.android.support:recyclerview-v7:${supportVersion}"
api 'com.github.bumptech.glide:glide:4.9.0'
api 'com.github.bumptech.glide:okhttp-integration:4.9.0'
api "android.arch.lifecycle:common-java8:$lifecycle_version"
api 'com.google.android.datatransport:transport-api:2.2.0'
api 'com.google.android.datatransport:transport-backend-cct:2.2.0'
api 'com.google.android.datatransport:transport-runtime:2.2.0'
api 'joda-time:joda-time:2.9.9'
// The Navigation SDK does not require the
// Places API; however, the demo app for does.
api 'com.google.android.gms:play-services-places:10.2.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
更新應用程式的資訊清單
在「Manifest 資料夾」中開啟
AndroidManifest.xml
檔案。在
<application>
元素中加入 API 金鑰。您必須使用上述步驟說明的 Google Cloud 控制台專案中定義的 API 金鑰。在這個範例中,您將在這個範例的部分資訊清單中,將
YOUR_API_KEY
替換成自己的 API 金鑰:<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/>
下方完整的資訊清單會顯示 API 金鑰的設定,以及啟動時執行的
MainActivity
空白intent
。<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.navigationapidemo" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/_AppTheme" > <meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/> <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ```
在應用程式中加入必要的作者資訊
如果您在應用程式中使用 Android 版 Navigation SDK,請務必在應用程式的法律聲明部分中加入作者資訊文字和開放原始碼授權。
您可以在 Navigation SDK for Android ZIP 檔案中找到必要的歸因文字和開放原始碼授權:
NOTICE.txt
LICENSES.txt
後續步驟
瞭解如何規劃路徑。