page.title=Setting Up Google Play Services @jd:body
To develop an app using the Google Play services APIs, you need to set up your project with the Google Play services SDK.
If you haven't installed the Google Play services SDK yet, go get it now by following the guide to Adding SDK Packages.
To test your app when using the Google Play services SDK, you must use either:
To make the Google Play services APIs available to your app:
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.
dependencies
for the latest version of
play-services
. For example:
apply plugin: 'com.android.application' ... dependencies { compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.google.android.gms:play-services:7.3.0' }
Be sure you update this version number each time Google Play services is updated.
Note: If the number of method references in your app exceeds the 65K limit, your app may fail to compile. You may be able to mitigate this problem when compiling your app by specifying only the specific Google Play services APIs your app uses, instead of all of them. For information on how to do this, see Selectively compiling APIs into your executable.
You can now begin developing features with the Google Play services APIs.
In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.
From version 6.5, you can instead selectively compile Google Play service APIs into your app. For
example, to include only the Google Fit and Android Wear APIs, replace the following line in your
build.gradle
file:
compile 'com.google.android.gms:play-services:7.3.0'
with these lines:
compile 'com.google.android.gms:play-services-fitness:7.3.0' compile 'com.google.android.gms:play-services-wearable:7.3.0'
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. Some APIs do not have a separate
library; include them by including the base library. (This lib is automatically included when
you include an API that does have a separate library.)
Google Play services API | Description in build.gradle |
---|---|
Google+ | com.google.android.gms:play-services-plus:7.3.0 |
Google Account Login | com.google.android.gms:play-services-identity:7.3.0 |
Google Actions, Base Client Library | com.google.android.gms:play-services-base:7.3.0 |
Google App Indexing | com.google.android.gms:play-services-appindexing:7.3.0 |
Google Analytics | com.google.android.gms:play-services-analytics:7.3.0 |
Google Cast | com.google.android.gms:play-services-cast:7.3.0 |
Google Cloud Messaging | com.google.android.gms:play-services-gcm:7.3.0 |
Google Drive | com.google.android.gms:play-services-drive:7.3.0 |
Google Fit | com.google.android.gms:play-services-fitness:7.3.0 |
Google Location, Activity Recognition, and Places | com.google.android.gms:play-services-location:7.3.0 |
Google Maps | com.google.android.gms:play-services-maps:7.3.0 |
Google Mobile Ads | com.google.android.gms:play-services-ads:7.3.0 |
Google Nearby | com.google.android.gms:play-services-nearby:7.3.0 |
Google Panorama Viewer | com.google.android.gms:play-services-panorama:7.3.0 |
Google Play Game services | com.google.android.gms:play-services-games:7.3.0 |
SafetyNet | com.google.android.gms:play-services-safetynet:7.3.0 |
Google Wallet | com.google.android.gms:play-services-wallet:7.3.0 |
Android Wear | com.google.android.gms:play-services-wearable:7.3.0 |
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 topic.
To make the Google Play services APIs available to your app:
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.
<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.
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; }
To make the Google Play services APIs available to your app:
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.
<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.
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; }
As described in the Google Play services introduction, Google Play delivers service updates for users on Android 2.3 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.
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.
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.
You are strongly encouraged to use the {@code GoogleApiClient} class to access Google Play services features. This approach allows you to attach an {@code OnConnectionFailedListener} object to your client. To detect if the device has the appropriate version of the Google Play services APK, implement the {@code onConnectionFailed()} callback method. If the connection fails due to a missing or out-of-date version of the Google Play APK, the callback receives an error code such as {@code SERVICE_MISSING}, {@code SERVICE_VERSION_UPDATE_REQUIRED}, or {@code SERVICE_DISABLED}. To learn more about how to build your client and handle such connection errors, see Accessing Google APIs.
Another approach is to use the {@code isGooglePlayServicesAvailable()} method. You might call this method in the {@link android.app.Activity#onResume onResume()} method of the main activity. If the result code is {@code 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 {@code SERVICE_MISSING}, {@code SERVICE_VERSION_UPDATE_REQUIRED}, or {@code SERVICE_DISABLED}, then the user needs to install an update. In this case, call the {@code getErrorDialog()} method and pass it the result error code. The method returns a {@link android.app.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.