Page Summary
-
The Places SDK for Android now requires AndroidX and uses the Android Gradle plugin version 8.2, with compatibility information available for other versions.
-
You need to configure your project with the Places SDK dependency, an API key stored securely via the Secrets Gradle Plugin, and initialize the Places API client.
-
Ensure your development environment is set up with Android Studio Hedgehog and deploy your application to a compatible Android device or emulator running Android 5.0 or higher with Google APIs.
-
Securely manage API keys in production using recommended mechanisms, and refer to provided sample apps for practical implementation guidance.
To configure your app to use the Places SDK for Android, follow these steps. They are required for all apps using the Places SDK for Android.
Step 1: Set up Android Studio
This document describes a development environment using Android Studio Hedgehog and the Android Gradle plugin version 8.2.
Step 2. Set up the SDK
The Places SDK for Android library is available through Google's Maven repository. To add the SDK to your app, do the following:
- In your top-level
settings.gradle.ktsfile, include the Gradle plugin portal, Google Maven repository, and Maven central repository under thepluginManagementblock. ThepluginManagementblock must appear before any other statements in the script.pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } }
- In your top-level
settings.gradle.ktsfile, include the Google's Maven repository and Maven central repository under thedependencyResolutionManagementblock:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() } }
-
In the
dependenciessection of your module-levelbuild.gradle.ktsfile, add a dependency to the 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") }
- In your module-level
build.gradle.ktsfile, setcompileSdkandminSdkto the following values:Groovy
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- In the
buildFeaturessection of your module-levelbuild.gradlefile, add theBuildConfigclass, which you use to access metadata values defined later in this procedure:Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
Step 3: Add your API key to the project
This section describes how to store your API key so that it can be securely
referenced by your app. You shouldn't check your API key into your version
control system, so we recommend storing it in a
secrets.properties file in the root directory of your project and
excluding that file from version control. For more information about Gradle
properties files, see
Gradle properties files.
To simplify this task, we recommend that you use the Secrets Gradle Plugin for Android.
To install the Secrets Gradle Plugin for Android and store your API key:
-
In Android Studio, open your top-level
build.gradle.ktsorbuild.gradlefile and add the following code to thepluginsblock.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 }
-
Open your module-level
build.gradle.ktsorbuild.gradlefile and add the following code to thepluginselement.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' }
- If you use Android Studio, sync your project with Gradle.
-
Open the
secrets.propertiesfile in your top-level directory, and then add the following code. ReplaceYOUR_API_KEYwith your API key. Make sure to addsecrets.propertiesto your.gitignorefile (or otherwise exclude it from your version control system) so that you don't check your API key into version control.PLACES_API_KEY=YOUR_API_KEY
-
Create the
local.defaults.propertiesfile in your top-level directory, the same folder as thesecrets.propertiesfile, and then add the following code.PLACES_API_KEY=DEFAULT_API_KEY
The purpose of this file is to provide a backup location for the API key if the
secrets.propertiesfile isn't found so that builds don't fail. This can happen if you clone the app from a version control system that omitssecrets.propertiesand you haven't yet created asecrets.propertiesfile locally to provide your API key. -
In Android Studio, open your module-level
build.gradle.ktsorbuild.gradlefile and edit thesecretsproperty. If thesecretsproperty doesn't exist, add it.Edit the properties of the plugin to set
propertiesFileNameto"secrets.properties", setdefaultPropertiesFileNameto"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 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" }
Step 4. Initialize the Places API client
To initialize the Places SDK for Android within an activity or fragment, pass the API key when calling
Places.initializeWithNewPlacesApiEnabled():
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);
You are now ready to begin using the Places SDK for Android!
Step 5: Set up an Android device
To run an app that uses the Places SDK for Android, you must deploy it to an Android device or Android emulator that is based on Android 6.0 or higher and includes the Google APIs.
- To use an Android device, follow the instructions at Run apps on a hardware device.
- To use an Android emulator, you can create a virtual device and install the emulator by using the Device Manager that comes with Android Studio.