プロジェクトを設定する -- バージョン 4.99 以前

このガイドでは、Navigation SDK for Android を使用するためのビルド構成の要件について説明します。この手順は、Android IDE がインストール済みで、Android 開発に精通していることを前提としています。

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

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

  • Google Cloud コンソール

    Navigation SDK が有効になっているプロジェクト。プロビジョニングについては、Google Maps Platform の担当者にお問い合わせください。

  • アプリは API レベル 30 以上を対象にする必要があります。

  • Navigation SDK で構築されたアプリを実行するには、Android デバイスに Google Play 開発者サービスがインストールされ、有効になっている必要があります。

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

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

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

設定するには

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

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

Navigation SDK は、Maven を使用するか、AAR バンドルとして入手できます。開発プロジェクトを作成したら、次のいずれかの方法で SDK を統合できます。

Navigation SDK v4.5 以降で Maven を使用する(推奨)

次の例では、Navigation SDK をプロジェクトに追加する最も簡単でおすすめの方法である google() Maven リポジトリを使用しています。

  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 to make nsure 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>
    

v4.5 より前の Navigation SDK または Driver SDK で Maven を使用する

Navigation SDK は、v4 バージョンの残りの期間中、元の Maven リポジトリを使用して引き続き利用できます。これは上記のバージョンと同じライブラリで、同じアップデートがすべて含まれています。移行期間中は Driver SDK や他のライブラリとの互換性も維持されます。この依存関係を使用するには、コンパイル時に gcloud を使用してクラウド プロジェクトにログインする必要があります。

  1. Consumer SDK ドキュメントの前提条件セクションの説明に沿って、Google の Maven リポジトリにアクセスするように環境を設定します。Navigation SDK へのアクセスは、ワークスペース グループを通じて制御されます。
  2. Gradle または Maven の構成に次の依存関係を追加します。VERSION_NUMBER プレースホルダは Navigation SDK のバージョンに置き換えてください。

    Gradle

    モジュール レベルの build.gradle に以下を追加します。

    dependencies {
    ...
    implementation 'com.google.android.maps:navsdk:VERSION_NUMBER'
    }
    

    トップレベルの build.gradle に次のコードを追加します。

    allprojects {
    ...
    // Required: you must exclude the Google Play service Maps SDK from
    // your transitive dependencies to make sure 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.maps</groupId>
         <artifactId>navsdk</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>
    

ダウンロードした AAR バンドルを使用する(非推奨)

Navigation SDK は、AAR バンドルとしても提供されています。開発プロジェクトを作成したら、SDK を統合できます。この手順では、IDE として Android Studio を使用することを前提としています。

  1. 共有の Google ドライブから最新バージョンの Navigation SDK をダウンロードして解凍します。アクセス権がない場合は、担当者にお問い合わせください。

  2. Android Studio でプロジェクトを開き、SDK Manager を使用して Google Play 開発者サービス パッケージを追加します

  3. zip ファイルのディレクトリから、libs/google_navigation_navmap.aar をプロジェクトの app/libs ディレクトリにコピーします。

  4. モジュール レベルの build.gradle に以下を追加します。

    implementation(name: 'google_navigation_navmap', ext: 'aar')
    

    トップレベルの build.gradle に次のコードを追加します。

    allprojects {
       ...
       // Required: you must exclude the Google Play service Maps SDK from
       // your transitive dependencies to make sure 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'
           }
       }
    }
    

ビルドを構成する

プロジェクトを作成したら、Navigation SDK のビルドと使用を成功させるための設定を構成できます。

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

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

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

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

    Navigation SDK の必須設定

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

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

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

以下に、アプリの Gradle ビルド スクリプトの例を示します。使用している Navigation SDK のバージョンがこのドキュメントと若干異なる可能性があるため、更新された依存関係のセットについてはサンプルアプリをご確認ください。

apply plugin: 'com.android.application'
apply plugin: 'com.google.cloud.artifactregistry.gradle-plugin'

ext {
    androidxVersion = "1.0.0"
    lifecycle_version = "1.1.1"
}

android {
    compileSdkVersion 30
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "<your id>"
        // Navigation SDK supports SDK 23 and later.
        minSdkVersion 23
        targetSdkVersion 30
        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 {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

// This tells Gradle where to look to find additional libraries - in this case, the
// google_navigation_navmap.aar file.
repositories {
    flatDir {
        dirs 'libs'
    }
    google()

    // Required for accessing the Navigation SDK on Google's Maven repository.
    maven {
        url "artifactregistry://us-west2-maven.pkg.dev/gmp-artifacts/transportation"
    }
}

dependencies {
    // Include the Google Navigation SDK
    implementation 'com.google.android.maps:navsdk:4.4.0'

    // The included AAR file under libs can be used instead of the Maven repository.
    // Uncomment the line below and comment out the previous dependency to use
    // the AAR file instead. Make sure that you add the AAR file to the libs directory.
    // implementation(name: 'google_navigation_navmap', ext: 'aar')

    // These dependencies are required for the Navigation SDK to function
    // properly at runtime.
    implementation 'org.chromium.net:cronet-fallback:69.3497.100'
    // Optional for Cronet users:
    // implementation 'org.chromium.net:cronet-api:69.3497.100'
    implementation 'androidx.appcompat:appcompat:${androidxVersion}'
    implementation 'androidx.cardview:cardview:${androidxVersion}'
    implementation 'com.google.android.material:material:${androidxVersion}'
    implementation 'androidx.mediarouter:mediarouter:${androidxVersion}'
    implementation 'androidx.preference:preference:${androidxVersion}'
    implementation 'androidx.recyclerview:recyclerview:${androidxVersion}'
    implementation 'androidx.legacy:legacy-support-v4:${androidxVersion}'
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.github.bumptech.glide:okhttp-integration:4.9.0'
    implementation 'android.arch.lifecycle:common-java8:$lifecycle_version'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.android.datatransport:transport-api:2.2.0'
    implementation 'com.google.android.datatransport:transport-backend-cct:2.2.0'
    implementation 'com.google.android.datatransport:transport-runtime:2.2.0'
    implementation 'joda-time:joda-time:2.9.9'
    annotationProcessor 'androidx.annotation:annotation:1.1.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

アプリに API キーを追加する

このセクションでは、アプリで安全に参照されるように API キーを保存する方法を説明します。API キーは、バージョン管理システムにはチェックインせず、プロジェクトのルート ディレクトリにある secrets.properties ファイルに保存することをおすすめします。secrets.properties ファイルについて詳しくは、Gradle プロパティ ファイルをご覧ください。

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

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

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

    MAPS_API_KEY=DEFAULT_API_KEY

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

  7. 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 v2 の認証にのみ使用できます。アプリでは、API キーのメタデータ名を 1 つのみ指定できます。両方を指定すると、API から例外がスローされます。

  8. Android Studio でモジュール レベルの build.gradle.kts ファイルか build.gradle ファイルを開き、secrets プロパティを編集します。secrets プロパティがない場合は追加します。

    プラグインのプロパティを編集して propertiesFileNamesecrets.properties に、defaultPropertiesFileNamelocal.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"
    }
            

    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"
    }
            

アプリに必要な帰属表示を含める

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

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

  • NOTICE.txt
  • LICENSES.txt