Set up an Android Studio project

Stay organized with collections Save and categorize content based on your preferences.

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

  1. Android Studio Arctic Fox or later is required. If you haven't already done so, download and install it.
  2. Ensure that you are using the Android Gradle plugin version 7.0 or later in Android Studio.

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:

  1. In your top-level settings.gradle file, include the Gradle plugin portal, Google Maven repository, and Maven central repository under the pluginManagement block. The pluginManagement block must appear before any other statements in the script.
    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    } 
  2. In your top-level settings.gradle file, include the Google's Maven repository and Maven central repository under the dependencyResolutionManagement block:
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    } 
  3. In the dependencies section of your module-level build.gradle file, add a dependency to the Places SDK for Android:

    dependencies {
        implementation 'com.google.android.libraries.places:places:2.7.0'
    }
    
  4. In your module-level build.gradle file, set compileSdk and minSdk to the following values:
    android {
        compileSdk 31
    
        defaultConfig {
            minSdk 16
            // ...
        }

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 should not check your API key into your version control system, so we recommend storing it in the local.properties file, which is located in the root directory of your project. For more information about the local.properties file, see Gradle properties files.

To streamline this task, we recommend that you use the Secrets Gradle Plugin for Android. To install the plugin and store your API key:

  1. In Android Studio, open your project-level build.gradle file and add the following code to the dependencies element under buildscript.
    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
    }
  2. Next, open your module-level build.gradle file and add the following code to the plugins element.
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
        
  3. Save the file and sync your project with Gradle.
  4. Open the local.properties in your project level directory, and then add the following code. Replace YOUR_API_KEY with your API key.
    MAPS_API_KEY=YOUR_API_KEY
        
  5. Save the file.
  6. In your AndroidManifest.xml file, go to com.google.android.geo.API_KEY and update the android:value attribute as follows:
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="${MAPS_API_KEY}" />
        

Note: As shown above, 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 Places 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.

Step 4. Initialize the Places API client

Initialize the Places SDK for Android within an activity or fragment as shown in the following example (note that you pass the API key when calling Places.initialize()):

Java


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

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

      

Kotlin


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

    // Create a new PlacesClient instance
    val 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 4.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 Android Virtual Device (AVD) Manager that comes with Android Studio.

Next steps

After your project is configured, you can explore the sample apps.