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:
- A compatible Android device that runs Android 2.3 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:
- Open the
build.gradle
file inside your application module directory.Note: Android Studio projects contain a top-level
build.gradle
file and abuild.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. - Add a new build rule under
dependencies
for the latest version ofplay-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:6.5.87' }
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.
- Save the changes and click Sync Project with Gradle Files in the toolbar.
- Open your app's manifest file and add the following tag as a child of the
<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.
Selectively compiling APIs into your executable
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:6.5.87'
with these lines:
compile 'com.google.android.gms:play-services-fitness:6.5.87' compile 'com.google.android.gms:play-services-wearable:6.5.87'
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:6.5.87 |
Google Account Login | com.google.android.gms:play-services-identity:6.5.87 |
Google Activity Recognition | com.google.android.gms:play-services-location:6.5.87 |
Google App Indexing | com.google.android.gms:play-services-appindexing:6.5.87 |
Google Cast | com.google.android.gms:play-services-cast:6.5.87 |
Google Drive | com.google.android.gms:play-services-drive:6.5.87 |
Google Fit | com.google.android.gms:play-services-fitness:6.5.87 |
Google Maps | com.google.android.gms:play-services-maps:6.5.87 |
Google Mobile Ads | com.google.android.gms:play-services-ads:6.5.87 |
Google Panorama Viewer | com.google.android.gms:play-services-panorama:6.5.87 |
Google Play Game services | com.google.android.gms:play-services-games:6.5.87 |
Google Wallet | com.google.android.gms:play-services-wallet:6.5.87 |
Android Wear | com.google.android.gms:play-services-wearable:6.5.87 |
Google Actions Google Analytics Google Cloud Messaging |
com.google.android.gms:play-services-base:6.5.87 |
To make the Google Play services APIs available to your app:
- 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. - Import the library project into your Eclipse workspace. Click File > Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.
- In your app project, reference Google Play services library project. See
Referencing a Library Project for Eclipse 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
<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.
To make the Google Play services APIs available to your app:
- 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. - In your app project, reference the Google Play services library project. See
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
<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 build.gradle
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 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. For many apps, the best time to check is during the
onResume()
method of the main activity.
The Google Play services library includes utility methods that help you determine whether or not the Google Play services version on the device supports the version of the client library you are using. If the version on the device is too old, the system will take the user to Google Play Store in order to install the recent version of the Google Play services.
Because each app uses Google Play services differently, it's up to you decide the appropriate place in your app to check 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 verify the Google Play services version, call isGooglePlayServicesAvailable()
. 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. So,
call GooglePlayServicesUtil.getErrorDialog()
and pass it the result error code.
This 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.