プロジェクトをセットアップする

このガイドでは、Navigation SDK for Android バージョン 5.0.0 以降を使用するためのビルド構成の要件について説明します。

以下の手順は、Android IDE がインストールされていて、Android 開発に精通していることを前提としています。

Navigation SDK を使用するための最小要件

これらの要件は、Navigation SDK for Android バージョン 5.0.0 以降に適用されます。

  • Navigation SDK を有効にした Google Cloud Console プロジェクト。プロビジョニングについては、Google Maps Platform の担当者にお問い合わせください。

  • アプリでは、Android のバージョンを次のように指定する必要があります。

    • ターゲット バージョンは Android 13(API レベル 33)以降である必要があります。
    • 最小バージョンは Android 6(API レベル 23)以降である必要があります。
  • Navigation SDK でビルドしたアプリを実行するには、Android デバイスが次の要件を満たしている必要があります。

  • 帰属表示とライセンス テキストをアプリに追加する必要があります。

プロジェクトを設定する: Cloud コンソール プロジェクトと Android プロジェクト

アプリを構築またはテストする前に、Cloud Console プロジェクトを作成し、API キーの認証情報を追加する必要があります。Navigation SDK にアクセスするためのプロビジョニングがプロジェクトに必要です。Cloud Console プロジェクト内のすべての鍵には、Navigation SDK に対する同じアクセス権が付与されます。1 つのキーに複数の開発プロジェクトを関連付けることができます。コンソール プロジェクトがすでにある場合は、現在のプロジェクトに鍵を追加できます。

セットアップ方法

  1. お好みのウェブブラウザCloud Console にログインし、Cloud Console プロジェクトを作成します。
  2. Android Studio などの IDE で、Android アプリ開発プロジェクトを作成し、パッケージ名をメモします。
  3. Google Maps Platform の担当者にお問い合わせいただき、Cloud コンソール プロジェクト用の Navigation SDK にアクセスできるようにしてください。
  4. ウェブブラウザの Cloud コンソール ダッシュボードで認証情報を作成し、制限付きの API キーを生成します。
  5. [API キー] ページの [アプリケーションの制限] で [Android アプリ] をクリックします。
  6. [パッケージ名とフィンガープリントを追加する] をクリックして、開発プロジェクトのパッケージ名と、その鍵の SHA-1 フィンガープリントを入力します。
  7. [保存] をクリックします。

プロジェクトに Navigation SDK を追加する

Navigation SDK は Maven から利用できます。開発プロジェクトを作成したら、次のいずれかの方法で SDK をプロジェクトに統合できます。

以下では、google() Maven リポジトリを使用します。これは、Navigation SDK をプロジェクトに追加する最も簡単で推奨される方法です。

  1. Gradle または Maven の構成に次の依存関係を追加します。VERSION_NUMBER プレースホルダは、Navigation SDK for 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>
    

    Maps SDK を使用する依存関係がある場合は、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 を使用できるようにします。

ローカル プロパティを更新する

  • Gradle Scripts フォルダで、local.properties ファイルを開き、android.useDeprecatedNdk=true を追加します。

Gradle ビルド スクリプトを更新する

  • build.gradle (Module:app) ファイルを開き、以下のガイドラインに沿って Navigation SDK の要件を満たす設定を更新し、最適化オプションの設定も検討します。

    Navigation SDK に必要な設定

    1. minSdkVersion を 23 以上に設定します。
    2. targetSdkVersion を 33 以上に設定します。
    3. javaMaxHeapSize を増やす dexOptions 設定を追加します。
    4. 追加のライブラリの場所を設定します。
    5. Navigation SDK の repositoriesdependencies を追加します。
    6. 依存関係のバージョン番号を最新の利用可能なバージョンに置き換えます。

    ビルド時間を短縮するためのオプション設定

    • R8/ProGuard を使用してコード圧縮とリソース圧縮を有効にし、未使用のコードとリソースを依存関係から削除します。R8/ProGuard ステップの実行に時間がかかりすぎる場合は、開発作業で multidex を有効にすることを検討してください。
    • ビルドに含まれる言語翻訳の数を減らす: 開発中に 1 つの言語に対して resConfigs を設定します。最終的なビルドでは、実際に使用する言語用に resConfigs を設定します。デフォルトでは、Gradle には Navigation SDK でサポートされているすべての言語のリソース文字列が含まれています。

    Java8 サポートのための desugar の追加

    • Android Gradle プラグイン 4.0.0 以降を使用してアプリをビルドする場合、このプラグインにより、多数の Java 8 言語 API のサポートが拡張されます。詳しくは、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 プロパティ ファイルをご覧ください。

このタスクを効率化するには、Android 用 Secrets Gradle プラグインの使用をおすすめします。

Android 用 Secrets Gradle プラグインを Google マップ プロジェクトにインストールする手順は以下のとおりです。

  1. Android Studio で最上位レベルの build.gradle または build.gradle.kts ファイルを開き、buildscript の配下にある dependencies 要素に次のコードを追加します。

    Groovy

    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 要素に追加します。

    Groovy

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

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. モジュール レベルの build.gradle ファイルで、targetSdkcompileSdk を 34 に設定します。
  4. ファイルを保存して、プロジェクトを Gradle と同期します
  5. 最上位レベルのディレクトリで secrets.properties ファイルを開き、次のコードを追加します。YOUR_API_KEY は実際の API キーに置き換えてください。secrets.properties はバージョン管理システムにチェックインされないため、このファイルにキーを保存します。
    MAPS_API_KEY=YOUR_API_KEY
  6. ファイルを保存します。
  7. 最上位レベルのディレクトリ(secrets.properties ファイルと同じフォルダ)に local.defaults.properties ファイルを作成し、次のコードを追加します。

    MAPS_API_KEY=DEFAULT_API_KEY

    このファイルの目的は、secrets.properties ファイルがない場合に API キーのバックアップ場所を提供し、ビルドが失敗しないようにすることです。この状況は、secrets.properties を省略したバージョン管理システムからアプリのクローンを作成し、API キーを提供するための secrets.properties ファイルをまだローカルに作成していない場合に発生する可能性があります。

  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 for Android を使用する場合は、アプリの法的通知セクションにアトリビューション テキストとオープンソース ライセンスを含める必要があります。

必要な帰属表示テキストとオープンソース ライセンスは、Navigation SDK for Android の zip ファイルで確認できます。

  • NOTICE.txt
  • LICENSES.txt

Mobility または Fleet Engine Deliveries のお客様の場合

モビリティまたは Fleet Engine Deliveries をご利用のお客様は、モビリティに関するドキュメントで課金についてご確認ください。取引の記録について詳しくは、課金の設定請求対象取引の記録レポート請求対象取引の記録(Android)をご覧ください。