Places SDK for Android を使用するようにアプリを構成する手順は次のとおりです。これらの手順は、Places SDK for Android を使用するすべてのアプリで必要です。
ステップ 1: Android Studio をセットアップする
このドキュメントでは、Android Studio Hedgehog と Android Gradle プラグイン バージョン 8.2 を使用した開発環境について説明します。
ステップ 2. SDK をセットアップする
Places SDK for Android ライブラリは、 Google の Maven リポジトリを通して利用できます。アプリに SDK を追加するには、以下の手順を行います。
- 最上位レベルの
settings.gradle.ktsファイルで、 Gradle プラグイン ポータル、 Google Maven リポジトリ、 および Maven 中央リポジトリ をpluginManagementブロック以下に含めます。pluginManagementブロックは、スクリプト内の他のステートメントよりも前に配置する必要があります。pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } }
- 最上位レベルの
settings.gradle.ktsファイルで、 Google の Maven リポジトリ と Maven 中央リポジトリ をdependencyResolutionManagementブロック以下に含めます:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() } }
-
dependencies**モジュール** レベルのbuild.gradle.ktsファイルの セクションに、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:6.0.2") }
- モジュール レベルの
build.gradle.ktsファイルで、compileSdkとminSdkをそれぞれ次の値に設定します:Groovy
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- モジュール レベルの
build.gradleファイルのbuildFeaturesセクションにBuildConfigクラスを追加します。このクラスを使用すると、この手順で後ほど定義するメタデータの値にアクセスできます。Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
ステップ 3: API キーをプロジェクトに追加する
このセクションでは、アプリで安全に参照されるように API キーを保存する方法を説明します。API
キーは、バージョン管理システムにはチェックインせず、プロジェクトのルート ディレクトリにある secrets.properties
ファイルに保存し、そのファイルをバージョン管理から除外することをおすすめします。Gradle
プロパティ ファイルについて詳しくは、
Gradle プロパティ ファイルをご覧ください。
このタスクを簡単にするには、Android 用 Secrets Gradle プラグインの使用をおすすめします。
Android 用 Secrets Gradle プラグインをインストールして API キーを保存する手順は次のとおりです。
-
Android Studio で最上位レベルの
build.gradle.ktsまたはbuild.gradleファイルを開き、pluginsブロックに次のコードを追加します。Kotlin
plugins { // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false }
Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false }
-
モジュール レベルの
build.gradle.ktsまたはbuild.gradleファイルを開き、次のコードをplugins要素に追加します。Kotlin
plugins { id("com.android.application") // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
Groovy
plugins { id 'com.android.application' // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
- Android Studio を使用している場合は、 プロジェクトを Gradle と同期します。
-
最上位レベルのディレクトリで
secrets.propertiesファイルを開き、 次のコードを追加します。YOUR_API_KEYを API キーに置き換えます。API キーをバージョン管理にチェックインしないように、secrets.propertiesを.gitignoreファイルに追加します(または、バージョン管理システムから除外します)。PLACES_API_KEY=YOUR_API_KEY
-
最上位レベルのディレクトリ(
secrets.propertiesファイルと同じフォルダ)にlocal.defaults.propertiesファイルを作成し、次のコードを追加します。DEFAULT_API_KEYPLACES_API_KEY=DEFAULT_API_KEY
このファイルの目的は、API キーのバックアップ場所を提供し 、
secrets.propertiesファイルがない場合にビルドが 失敗しないようにすることです。この状況は、secrets.propertiesを省略したバージョン管理 システムからアプリのクローンを作成し、API キーを提供するためにsecrets.propertiesファイルをまだローカルに作成していない場合に発生する可能性があります。 -
Android Studio でモジュール レベルの
build.gradle.ktsまたはbuild.gradleファイルを開き、secretsプロパティを編集します。secretsプロパティが存在しない場合は、追加します。プラグインのプロパティを編集して、
propertiesFileNameを"secrets.properties"に、defaultPropertiesFileNameを"local.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 root directory (the same folder as the root 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 root directory (the same folder as the root 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" }
ステップ 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 デバイスまたは Android Emulator にアプリをデプロイする必要があります。このデバイスまたはエミュレータは、Android 6.0 以降をベースとし Google API を含む必要があります。
- Android デバイスを使用する場合は、 ハードウェア デバイス上でのアプリの実行の手順を踏んでください。
- Android Emulator を使用する場合は、Android Studio に付属している デバイス マネージャー を使用して仮想デバイスを作成し、エミュレータをインストールしてください。