page.title=Set Up Google Play Services SDK @jd:body

In this document

  1. Install the Google Play Services SDK
  2. Set Up a Project that Uses Google Play Services
  3. Create a Proguard Exception
  4. Ensure Devices Have the Google Play services APK

To develop an app using the Google Play services APIs, you must download the Google Play services SDK from the SDK Manager. The download includes the client library and code samples.

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

Ideally, you should develop and test your app on a variety of devices, including both phones and tablets.

Install the Google Play Services SDK

To install the Google Play services SDK for development:

  1. Launch the SDK Manager in one of the following ways:
  2. Install the Google Play services SDK.

    Scroll to the bottom of the package list, expand Extras, select Google Play services, and install it. If you're using Android Studio, also install Google Repository (it provides the Maven repository used for Gradle builds).

    The Google Play services SDK is saved in your Android SDK environment at <android-sdk>/extras/google/google_play_services/.

    Note: Google Play services 4.0.30 (released November 2013) and newer versions require Android 2.3 or higher. If your app supports Android 2.2, you can continue development with the Google Play services SDK, but must instead install Google Play services for Froyo from the SDK Manager.

  3. Install a compatible version of the Google APIs platform.

    If you want to test your app on the emulator, expand the directory for Android 4.2.2 (API 17) or a higher version, select Google APIs, and install it. Then create a new AVD with Google APIs as the platform target.

  4. Make a copy of the Google Play services library project.

    Note: If you are using Android Studio, skip this step.

    Copy the library project at <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ to the location where you maintain your Android app projects.

    If you are using Eclipse, import the library project into your workspace. Click File > Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.

Set Up a Project that Uses Google Play Services

Using Android Studio:

  1. Open the build.gradle file inside your application directory.
  2. Add a new build rule under dependencies for the latest version of play-services. For example:
    apply plugin: 'android'
    ...
    
    dependencies {
        compile 'com.android.support:appcompat-v7:+'
        compile 'com.google.android.gms:play-services:4.0.30'
    }
    

    Be sure you update this version number each time Google Play services is updated.

  3. Save the changes and click Sync Project with Gradle Files in the toolbar.
  4. Open your app's manifest file and add the following tag as a child of the {@code <application>} element:
    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />
    

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

Using Eclipse or another IDE:

To make the Google Play services APIs available to your app, you must reference the library project you created in step 4 of the installation instructions.

See the Referencing a Library Project for Eclipse or Referencing a Library Project on the Command Line for more information on how to do this.

Note: You should be referencing a copy of the library that you copied to your development workspace—you should not reference the library directly from the Android SDK directory.

After you've added the Google Play services library as a dependency for your app project, open your app's manifest file and add the following tag as a child of the {@code <application>} element:

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

Once you've set up your project to reference the library project, you can begin developing features with the Google Play services APIs.

Create a Proguard Exception

To prevent ProGuard from stripping away required classes, add the following lines in the <project_directory>/proguard-project.txt file:

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

Note: When using Android Studio, you must add Proguard to your gradle.build file's build types. For more information, see the Gradle Plugin User Guide.

Ensure Devices Have the Google Play services APK

As described in the Google Play services introduction, Google Play delivers service updates for users on Android 2.3 through the Google Play Store app. However, updates might not reach all users immediately.

Important: Because it is hard to anticipate the state of each device, you must always check for a compatible Google Play services APK before you access Google Play services features. For many apps, the best time to check is during the {@link android.app.Activity#onResume onResume()} method of the main activity.

Here are four scenarios that describe the possible state of the Google Play services APK on a user's device:

  1. A recent version of the Google Play Store app is installed, and the most recent Google Play services APK has been downloaded.
  2. A recent version of the Google Play Store app is installed, but the most recent Google Play services APK has not been downloaded.
  3. An old version of the Google Play Store app, which does not proactively download Google Play services updates, is present.
  4. The Google Play services APK is missing or disabled on the device, which might happen if the user explicitly uninstalls or disables it.

Case 1 is the success scenario and is the most common. However, because the other scenarios can still happen, you must handle them every time your app connects to a Google Play service to ensure that the Google Play services APK is present, up-to-date, and enabled.

To help you, the Google Play services client library has utility methods to determine whether or not the Google Play services APK is recent enough to support the version of the client library you are using. If not, the client library sends users to the Google Play Store to download the recent version of the Google Play services APK.

Note: The Google Play services APK is not visible by searching the Google Play Store. The client library provides a deep link into the Google Play Store when it detects that the device has a missing or incompatible Google Play services APK.

It is up to you choose the appropriate place in your app to do the following steps to check for a valid Google Play services APK. For example, if Google Play services is required for your app, 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 do these checks if the user navigates to that portion of your app:

  1. Query for the status of Google Play services on the device with the {@code isGooglePlayServicesAvailable()} method, which returns a result code.
  2. If the result code is {@code SUCCESS}, then the Google Play services APK is up-to-date, and you can proceed as normal.
  3. If the result code is {@code SERVICE_MISSING}, {@code SERVICE_VERSION_UPDATE_REQUIRED}, or {@code SERVICE_DISABLED}, then call {@code getErrorDialog()} to display an error message to the user, which allows the user to download the APK from the Google Play Store or enable it in the device's system settings.