設定 Google Cloud 控制台專案

設定並設定 Google Cloud 控制台專案,以便針對 在 Android 裝置上,按照下列步驟操作。

  1. 設定行動應用程式專案
  2. 在應用程式中新增驅動程式 SDK
  3. 在應用程式中加入 API 金鑰
  4. 在應用程式中加入必要的作者資訊

如要進一步瞭解每個步驟,請參閱下列章節。

設定行動應用程式專案

本節說明如何設定驅動程式的行動應用程式專案 將機器學習工作流程自動化

如要設定行動應用程式專案,請按照下列步驟操作:

  1. 如果您沒有 Google Cloud 控制台開發專案和行動專案的 API 金鑰,就必須設定這兩項項目。若需更多資訊,請參閲 建立 Fleet Engine 專案
  2. 在 Google Cloud 控制台中,選取同一個 Google Cloud 控制台專案 您用於 Fleet Engine 的 API 金鑰。
  3. 選取「API 與」並搜尋並啟用 Maps SDK for Android。

在應用程式中新增驅動程式 SDK

您可以從 Google Maven 存放區取得驅動程式 SDK。存放區包含 SDK 的 Project Object Model (.pom) 檔案和 Javadoc。

如要將 Driver SDK 新增至應用程式,請將 Driver SDK 和 Navigation SDK 依附元件新增至 Gradle 或 Maven 設定。

在以下範例中,請將 VERSION_NUMBER 替換為所選 Driver SDK 版本。

Gradle

請將以下內容新增到您的 build.gradle 中:

dependencies {
...
implementation 'com.google.android.libraries.mapsplatform.transportation:transportation-driver:VERSION_NUMBER'
implementation 'com.google.android.libraries.navigation:navigation:5.0.0'
}

Maven

請將以下內容新增至 pom.xml

<dependencies>
  ...
  <dependency>
    <groupId>com.google.android.libraries.mapsplatform.transportation.driver</groupId>
    <artifactId>transportation-driver</artifactId>
    <version>VERSION_NUMBER</version>
  </dependency>
  <dependency>
    <groupId>com.google.android.libraries.navigation</groupId>
    <artifactId>navigation</artifactId>
    <version>5.0.0</version>
  </dependency>
</dependencies>

在應用程式中加入 API 金鑰

在應用程式中新增驅動程式 SDK 後,請新增 API 金鑰。您必須使用 您在設定開發專案時取得的專案 API 金鑰。詳情請參閱「設定行動應用程式專案」。

本節將說明如何儲存 API 金鑰,讓應用程式以更安全的方式參照金鑰。如要進一步瞭解 API 金鑰安全性最佳做法,請參閱 Google 地圖平台的「API 安全性最佳做法」一文。

如要簡化這項工作,您可以參考 GitHub 說明文件 Secrets Gradle Plugin for Android

如要安裝這個外掛程式並儲存 API 金鑰,請按照下列步驟操作:

  1. 開啟根層級的 build.gradle 檔案,然後將下列程式碼加進 buildscript 底下的 dependencies 元素。

    Groovy

    buildscript {
          dependencies {
              // ...
              classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.0"
          }
    }
    

    Kotlin

    buildscript {
        dependencies {
            // ...
            classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.0")
        }
    }
    
  2. 開啟應用程式層級的 build.gradle 檔案,然後將下列程式碼加進 plugins 元素。

    Groovy

    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    

    Kotlin

    id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    
  3. 如果您使用 Android Studio,請將專案與 Gradle 同步處理。詳情請參閱 將專案與 Gradle 同步處理

  4. 在專案層級目錄中開啟 local.properties,然後新增 以下程式碼。然後將 YOUR_API_KEY 替換成您的 API 金鑰。

    MAPS_API_KEY=YOUR_API_KEY

  5. 透過 AndroidManifest.xml 檔案前往 com.google.android.geo.API_KEY 然後更新 android:value 屬性,如下所示:

       <meta-data
           android:name="com.google.android.geo.API_KEY"
           android:value="${MAPS_API_KEY}" />

以下範例顯示範例應用程式的完整資訊清單:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.driverapidemo" >
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <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="${MAPS_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>

在應用程式中加入必要的作者資訊

如果您在應用程式中使用 Driver SDK,則必須加入作者資訊文字與 開放原始碼授權納入應用程式的法律聲明部分。建議你 將作者資訊納入獨立選單項目或關於 選單項目。

您可以在「third_party_licenses.txt」中找到授權資訊該檔案位於 已取消封存的 AAR 檔案

如要瞭解如何這麼做,請參閱「加入開放原始碼聲明」一節 包括開放原始碼通知

後續步驟

宣告依附元件