Places SDK for Android を使用するようにアプリを設定する手順は次のとおりです。Places SDK for Android を使用するすべてのアプリに必要です。
ステップ 1: Android Studio をセットアップする
- Arctic Fox 以降の Android Studio が必要です。お持ちでない場合は、ダウンロードしてインストールしてください。
- Android Studio で Android Gradle プラグイン バージョン 7.0 以降を使用していることを確認してください。
手順 2. SDK をセットアップする
Places SDK for Android ライブラリは、 Google の Maven リポジトリから利用できます。アプリに SDK を追加するには、以下の手順を行います。
- トップレベルの
settings.gradle
ファイルで、pluginManagement
ブロック以下に Gradle プラグイン ポータル、Google Maven リポジトリ、Maven セントラル リポジトリをインクルードします。pluginManagement
ブロックは、スクリプト内の他のステートメントよりも前に配置する必要があります。pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- トップレベルの
settings.gradle
ファイルで、dependencyResolutionManagement
ブロック以下に Google の Maven リポジトリと Maven セントラル リポジトリをインクルードします。dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
モジュールレベルの
build.gradle
ファイルのdependencies
セクションで、Places SDK for Android に依存関係を追加します。dependencies { implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version")) implementation 'com.google.android.libraries.places:places:3.3.0' }
- モジュール レベルの
build.gradle
ファイルで、compileSdk
とminSdk
を次の値に設定します。android { compileSdk 31 defaultConfig { minSdk 21 // ... }
ステップ 3: API キーをプロジェクトに追加する
このセクションでは、アプリで安全に参照されるように API キーを保存する方法を説明します。API キーは、バージョン管理システムにはチェックインせず、プロジェクトのルート ディレクトリにある secrets.properties
ファイルに保存することをおすすめします。secrets.properties
ファイルについて詳しくは、Gradle プロパティ ファイルをご覧ください。
このタスクを効率化するには、Android 用 Secrets Gradle プラグインの使用をおすすめします。
Android 用 Secrets Gradle プラグインを Google マップ プロジェクトにインストールする手順は以下のとおりです。
-
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") } }
-
Open your module-level
build.gradle
file and add the following code to theplugins
element.Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- Save the file and sync your project with Gradle.
-
Open the
secrets.properties
in your project level directory, and then add the following code. ReplaceYOUR_API_KEY
with your API key.MAPS_API_KEY=YOUR_API_KEY