Set Up Google Play Services

To develop an app using the Google Play services APIs, you need to set up your project with the Google Play services SDK, which is available from the Google maven repository.

For more detailed instructions, and to learn more about Android Studio and related SDK tools, see Update the IDE and SDK Tools.

To test your app when using the Google Play services SDK, you must use either:

  • A compatible Android device that runs Android 4.0 or higher and includes Google Play Store.
  • The Android emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or higher.

Add Google Play Services to Your Project

To make the Google Play services APIs available to your app:

  1. Open the build.gradle file inside your application module directory.

    Note: Android Studio projects contain a top-level build.gradle file and a build.gradle file for each module. Be sure to edit the file for your application module. See Building Your Project with Gradle for more information about Gradle.

  2. Add a new build rule under dependencies for the latest version of play-services, using one of the APIs listed below.
  3. Save the changes and click Sync Project with Gradle Files in the toolbar.
  4. If you receive an error, check that your top-level build.gradle contains a reference to the google() repo or to maven { url "https://maven.google.com" }.

You can now begin developing features with the Google Play services APIs.

Selectively compiling APIs into your executable

Table 1 shows a list of the separate APIs that you can include when compiling your app, and how to describe them in your build.gradle file.

Note: Don't use the combined play-services target. It brings in dozens of libraries, bloating your application. Instead, specify only the specific Google Play services APIs your app uses.

Table 1. Individual APIs and corresponding build.gradle descriptions.

API Description in build.gradle
Google+ com.google.android.gms:play-services-plus:11.6.0
Google Account Login com.google.android.gms:play-services-auth:11.6.0
Google Actions, Base Client Library com.google.android.gms:play-services-base:11.6.0
Google Sign In com.google.android.gms:play-services-identity:11.6.0
Google Analytics com.google.android.gms:play-services-analytics:11.6.0
Google Awareness com.google.android.gms:play-services-awareness:11.6.0
Google Cast com.google.android.gms:play-services-cast:11.6.0
Google Cloud Messaging com.google.android.gms:play-services-gcm:11.6.0
Google Drive com.google.android.gms:play-services-drive:11.6.0
Google Fit com.google.android.gms:play-services-fitness:11.6.0
Google Location and Activity Recognition com.google.android.gms:play-services-location:11.6.0
Google Maps com.google.android.gms:play-services-maps:11.6.0
Google Mobile Ads com.google.android.gms:play-services-ads:11.6.0
Google Places com.google.android.gms:play-services-places:11.6.0
Mobile Vision com.google.android.gms:play-services-vision:11.6.0
Google Nearby com.google.android.gms:play-services-nearby:11.6.0
Google Panorama Viewer com.google.android.gms:play-services-panorama:11.6.0
Google Play Game services com.google.android.gms:play-services-games:11.6.0
SafetyNet com.google.android.gms:play-services-safetynet:11.6.0
Android Pay com.google.android.gms:play-services-wallet:11.6.0
Android Wear com.google.android.gms:play-services-wearable:11.6.0
Include any of the above dependencies into your app build.gradle file, for example:
apply plugin: 'com.android.application'
...

dependencies {
    implementation 'com.google...'
}
Be sure you update this version number each time Google Play services is updated.

Note: ProGuard directives are included in the Play services client libraries to preserve the required classes. The Android Plugin for Gradle automatically appends ProGuard configuration files in an AAR (Android ARchive) package and appends that package to your ProGuard configuration. During project creation, Android Studio automatically creates the ProGuard configuration files and build.gradle properties for ProGuard use. To use ProGuard with Android Studio, you must enable the ProGuard setting in your build.gradle buildTypes. For more information, see the ProGuard guide.

Ensure Devices Have the Google Play services APK

As described in the Google Play services overview, Google Play delivers service updates for users on Android 4.0 and higher through the Google Play Store app. However, updates might not reach all users immediately, so your app should verify the version available before attempting to perform API transactions.

Because each app uses Google Play services differently, it's up to you decide the appropriate place in your app to verify the Google Play services version. For example, if Google Play services is required for your app at all times, you might want to do it when your app first launches. On the other hand, if Google Play services is an optional part of your app, you can check the version only once the user navigates to that portion of your app.

To learn more about how to build your client and handle such connection errors, see Accessing Google APIs.

Another approach is to use the isGooglePlayServicesAvailable() method. You get a reference to the singleton object that provides this method using GoogleApiAvailability.getInstance(). You might call this method in the onResume() method of the main activity. If the result code is SUCCESS, then the Google Play services APK is up-to-date and you can continue to make a connection. If, however, the result code is SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, or SERVICE_DISABLED, then the user needs to install an update. In this case, call the getErrorDialog() method and pass it the result error code. The method returns a Dialog you should show, which provides an appropriate message about the error and provides an action that takes the user to Google Play Store to install the update.

To then begin a connection to Google Play services (required by most Google APIs such as Google Drive, Google+, and Games), read Accessing Google APIs.