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

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 の desugar のサポートをご覧ください。コンパイル方法と依存関係オプションについては、以下のビルド スクリプト スニペットの例をご覧ください。

以下に、このアプリケーションの 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 はバージョン管理システムにチェックインされないため、このファイルにキーを保存します。
    NAV_API_KEY=YOUR_API_KEY
  6. ファイルを保存します。
  7. 最上位レベルのディレクトリ(secrets.properties ファイルと同じフォルダ)に local.defaults.properties ファイルを作成し、次のコードを追加します。

    NAV_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.*"
    }
            

必要な帰属情報をアプリに含める

アプリで Android 向け Navigation SDK を使用する場合は、アプリの法的通知のセクションに、帰属表示のテキストとオープンソース ライセンスを含める必要があります。

必要な帰属表示テキストとオープンソース ライセンスは、Navigation SDK for Android の zip ファイルに記載されています。

  • NOTICE.txt
  • LICENSES.txt

モビリティ デリバリーまたは Fleet Engine Deliveries のお客様の場合

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