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

このガイドでは、Terraform 構成ファイルを使用する際のビルド構成要件を Android 用 Navigation SDKこの手順は、Android IDE があることを前提としています。 Android 開発に精通していること。

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

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

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

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

  • Navigation SDK で作成したアプリを実行するには、 Google Play 開発者サービスがインストールされている必要があります。 インストールして有効になっていることを確認します。

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

プロジェクトをセットアップする: Cloud コンソール プロジェクトと Android プロジェクト

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

設定方法

  1. お好みのウェブブラウザで、 Cloud コンソールに移動して、 Cloud コンソール できます。
  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 を介して、または AAR バンドル: 開発プロジェクトを作成したら、次の方法でそのプロジェクトに SDK を統合できます。 次のいずれかの方法を使用します

次の例では、最もシンプルな google() Maven リポジトリを使用しています。 ナビゲーション 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>
    

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

Navigation SDK は引き続き、 元の Maven リポジトリを v4 の残りのバージョンで移行する必要があります。これが 更新が加えられたバージョンと同じライブラリであり、 移行中も Driver SDK や他のライブラリとの互換性が確保されます。使用 この依存関係では、次の場合に gcloud を介してクラウド プロジェクトにログインする必要があります。 表示されます。

  1. 以下の説明に従って、Google の Maven リポジトリにアクセスするための環境を設定します。 前提条件 セクションをご覧ください Navigation SDK はワークスペース グループを介して制御されます。
  2. Gradle または Maven の構成に次の依存関係を追加します。 目的の Navigation SDK バージョンの VERSION_NUMBER プレースホルダ。

    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. 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.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>
    

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

  1. Navigation SDK の最新バージョンをダウンロードする Google ドライブからコピーし、解凍します。もし アクセス権がない場合は、担当者にご連絡ください。

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

  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. 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'
            }
        }
    }
    

ビルドを構成する

プロジェクトを作成したら、プロジェクト 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 ステップの実行に時間がかかりすぎる場合は、 Multidex 必要があります。
    • ビルドに含まれる言語翻訳の数を削減する: Set 開発中の 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. Ensure 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 または 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