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

このページでは、Navigation SDK を開発プロジェクトに統合する方法について説明します。

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

Navigation SDK は、Google Maven リポジトリ。 プロジェクトに SDK を追加するには、Gradle build.gradle または Maven pom.xml の構成。

  1. Gradle または Maven の構成に次の依存関係を追加します。 VERSION_NUMBER プレースホルダを 必要なバージョンの Navigation SDK for Android。

    Gradle

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

    dependencies {
            ...
            implementation 'com.google.android.libraries.navigation:navigation:VERSION_NUMBER'
    }
    

    Maven

    pom.xml に次の行を追加します。

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.libraries.navigation</groupId>
        <artifactId>navigation</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    
  2. Maps SDK を使用する依存関係がある場合は、 宣言された各依存関係の、Maps SDK に依存する各依存関係。

    Gradle

    最上位の 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>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 に必要な設定

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

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

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

    Java8 サポートの脱糖を追加

    • Android Gradle プラグイン 4.0.0 または このプラグインは多数の Java 8 言語のサポートを拡張し、 APIJava 8 の脱糖 サポート をご覧ください。作成方法については、以下のビルド スクリプト スニペットの例をご覧ください。 コンパイルと依存関係のオプションを使用します。
    • Android Gradle プラグイン バージョンである Gradle 8.4 を使用することをおすすめします。 8.3.0、Desugar ライブラリ com.android.tools:desugar_jdk_libs_nio:2.0.3。この設定は、Android 向け Navigation SDK バージョン 6.0.0 以降に対応しています。
    • app モジュールと任意のサービスで Desugar ライブラリを有効にする必要があります。 直接 Navigation SDK に依存するモジュールです。

以下に、このアプリケーションの 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 34
        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_nio:2.0.3'
}

アプリに 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 はバージョン管理システムにチェックインされないため、このファイルにキーを保存します。
    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.kts or build.gradle 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.

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

    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"
    
        // 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 をご利用の場合は、Mobility のドキュメントで課金についてご確認ください。トランザクションの記録について詳しくは、課金システムを設定する課金対象のトランザクションを記録するレポート課金対象のトランザクションを記録する(Android)をご覧ください。