The Google Play services SDK is an extension to the Android SDK and is available to you as a downloadable package from the SDK Manager. The download includes the client library and code samples.
To develop using the Google Play services APIs, you must download the Google Play services SDK. Additionally, you must provide a physical development device on which you can run and debug your app. You can develop on any compatible Android device that runs Android 2.2 or higher and includes the Google Play Store. Ideally, you would develop and test the app on a variety of devices, including both phones and tablets.
Note: Google Play services is not supported on the Android emulator — to develop using the APIs, you need to provide a development device such as an Android phone or tablet.
To install the Google Play services SDK for development:
- Launch the SDK Manager.
- From Eclipse (with ADT), select Window > Android SDK Manager.
- On Windows, double-click the
SDK Manager.exe
file at the root of the Android SDK directory. - On Mac or Linux, open a terminal and navigate to the
tools/
directory in the Android SDK, then executeandroid sdk
.
-
Scroll to the bottom of the package list, select Extras > Google Play services,
and install it.
The Google Play services SDK is saved in your Android SDK environment at
<android-sdk-folder>/extras/google/google_play_services/
. - Copy the
<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib
library project into the source tree 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.
To set up a project to use the Google Play services SDK:
- Reference the library project in your Android project.
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 source tree—you should not reference the library from the Android SDK directory.
- If you are using ProGuard, add the following
lines in the
<project_directory>/proguard-project.txt
file to prevent ProGuard from stripping away required classes:-keep class * extends java.util.ListResourceBundle { protected Object[][] getContents(); }
Ensuring 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.2 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 in your app before you access Google Play services
features. For many apps, the best time to check is during the
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:
- A recent version of the Google Play Store app is installed, and the most recent Google Play services APK has been downloaded.
- A recent version of the Google Play Store app is installed, but the most recent Google Play services APK has not been downloaded.
- An old version of the Google Play Store app, which does not proactively download Google Play services updates, is present.
- 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:
-
Query for the status of Google Play services on the device with the
isGooglePlayServicesAvailable()
method, which returns a result code. -
If the result code is
SUCCESS
, then the Google Play services APK is up-to-date, and you can proceed as normal. -
If the result code is
SERVICE_MISSING
,SERVICE_VERSION_UPDATE_REQUIRED
, orSERVICE_DISABLED
, then callgetErrorDialog()
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.