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

Places SDK for Android を使用するようにアプリを設定する手順は次のとおりです。これらは、Places SDK for Android を使用するすべてのアプリで必要です。

ステップ 1: Android Studio をセットアップする

このドキュメントでは、Android Studio HedgehogAndroid Gradle プラグイン バージョン 8.2 を使用した開発環境について説明します。

ステップ 2: SDK をセットアップする

Places SDK for Android ライブラリは、Google の Maven リポジトリを通して利用できます。アプリに SDK を追加するには、以下の手順を行います。

  1. 最上位レベルの settings.gradle.kts ファイルで、pluginManagement ブロック以下に Gradle プラグイン ポータルGoogle Maven リポジトリMaven セントラル リポジトリを含めます。pluginManagement ブロックは、スクリプト内の他のステートメントよりも前に配置する必要があります。
    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    } 
  2. トップレベルの settings.gradle.kts ファイルで、dependencyResolutionManagement ブロック以下に Google の Maven リポジトリMaven セントラル リポジトリをインクルードします。
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    } 
  3. モジュール レベルの build.gradle.kts ファイルの dependencies セクションに、Places SDK for Android への依存関係を追加します。

    Groovy

    dependencies {
        implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version"))
        implementation("com.google.android.libraries.places:places:3.5.0")
    }

    Kotlin

              dependencies {
    
        // Places and Maps SDKs
        implementation("com.google.android.libraries.places:places:5.0.0")
    }
  4. モジュール レベルの build.gradle.kts ファイルで、compileSdkminSdk をそれぞれ次の値に設定します。

    Groovy

    android {
        compileSdk 34
    
        defaultConfig {
            minSdk 23
            // ...
        }
    }

    Kotlin

    android {
        compileSdk = 34
    
        defaultConfig {
            minSdk = 23
            // ...
        }
    }
  5. モジュール レベルの build.gradle ファイルの buildFeatures セクションに BuildConfig クラスを追加します。このクラスを使用すると、この手順で後ほど定義するメタデータの値にアクセスできます。

    Groovy

    android {
      // ...
      buildFeatures {
        buildConfig true
        // ...
      }
    }

    Kotlin

    android {
      // ...
      buildFeatures {
        buildConfig = true
        // ...
      }
    }

ステップ 3: API キーをプロジェクトに追加する

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

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

Android 用 Secrets Gradle プラグインをインストールして API キーを保存する手順は以下のとおりです。

  1. Android Studio でルートレベルの build.gradle ファイルを開き、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.android.application'
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.android.application")
        // ...
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. Android Studio を使用している場合は、Gradle とプロジェクトを同期します。
  4. プロジェクト レベルのディレクトリで local.properties を開き、次のコードを追加します。YOUR_API_KEY は実際の API キーに置き換えてください。
    PLACES_API_KEY=YOUR_API_KEY

ステップ 4. Places API クライアントを初期化する

アクティビティまたはフラグメント内で Places SDK for Android を初期化するには、Places.initializeWithNewPlacesApiEnabled() を呼び出すときに API キーを渡します。

Kotlin

    // Define a variable to hold the Places API key.
    val apiKey = BuildConfig.PLACES_API_KEY

    // Log an error if apiKey is not set.
    if (apiKey.isEmpty() || apiKey == "DEFAULT_API_KEY") {
        Log.e("Places test", "No api key")
        finish()
        return
    }

    // Initialize the SDK
    Places.initializeWithNewPlacesApiEnabled(applicationContext, apiKey)

    // Create a new PlacesClient instance
    val placesClient = Places.createClient(this)
    

Java

    // Define a variable to hold the Places API key.
    String apiKey = BuildConfig.PLACES_API_KEY;

    // Log an error if apiKey is not set.
    if (TextUtils.isEmpty(apiKey) || apiKey.equals("DEFAULT_API_KEY")) {
      Log.e("Places test", "No api key");
      finish();
      return;
    }

    // Initialize the SDK
    Places.initializeWithNewPlacesApiEnabled(getApplicationContext(), apiKey);

    // Create a new PlacesClient instance
    PlacesClient placesClient = Places.createClient(this);
    

これで、Places SDK for Android を使用する準備が整いました。

ステップ 5: Android デバイスをセットアップする

Places SDK for Android を使用するアプリを実行するには、Android 5.0 以降をベースとし Google API を含む Android デバイスまたは Android Emulator にアプリをデプロイする必要があります。

  • Android デバイスを使用する場合は、ハードウェア デバイス上でのアプリの実行の手順を踏んでください。
  • Android Emulator を使用する場合は、Android Studio に付属している Android Virtual Device(AVD)Manager を使用して仮想デバイスを作成し、エミュレータをインストールしてください。

次のステップ

プロジェクトの設定が完了したら、サンプルアプリを試すことができます。