• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Set Up Google Play Services SDK
2@jd:body
3
4
5<p>To develop an app using the <a href="{@docRoot}reference/gms-packages.html">Google
6Play services APIs</a>, you must download the Google Play services SDK
7from the <a href="{@docRoot}tools/help/sdk-manager.html">SDK Manager</a>.
8The download includes the client library and code samples.</p>
9
10<p>To test your app when using the Google Play services SDK, you must use either:</p>
11<ul>
12  <li>A compatible Android
13    device that runs Android 2.2 or higher and includes Google Play Store.</li>
14  <li>The Android emulator with an <a href="{@docRoot}tools/devices/index.html">AVD</a>
15  that runs the Google APIs platform based on Android 4.2.2 or higher.</li>
16</ul>
17
18<p>Ideally, you should develop and test your app on a variety of devices, including
19both phones and tablets.</p>
20
21
22<h2 id="Install">Install the Google Play Services SDK</h2>
23
24<p>To install the Google Play services SDK for development:</p>
25
26<ol>
27  <li>Launch the SDK Manager.
28   <ul>
29    <li>From Eclipse (with <a href="{@docRoot}tools/help/adt.html">ADT</a>),
30    select <strong>Window</strong> &gt; <strong>Android SDK Manager</strong>.</li>
31    <li>On Windows, double-click the <code>SDK Manager.exe</code> file at the root of the Android
32  SDK directory.</li>
33    <li>On Mac or Linux, open a terminal and navigate to the <code>tools/</code> directory in the
34  Android SDK, then execute <code>android sdk</code>.</li>
35    </ul>
36  </li>
37  <li>Install the Google Play services SDK.
38    <p>Scroll to the bottom of the package list, expand <b>Extras</b>, select
39    <b>Google Play services</b>, and install it.</p>
40      <p>The Google Play services SDK is saved in your Android SDK environment at
41      <code>&lt;android-sdk&gt;/extras/google/google_play_services/</code>.</p>
42  </li>
43  <li>Install a compatible version of the Google APIs platform.
44    <p>If you want to test your app on the emulator, expand the directory for <b>Android 4.2.2
45    (API 17)</b> or a higher version, select <b>Google APIs</b>, and install it. Then create a
46    new <a href="{@docRoot}tools/devices/index.html">AVD</a> with Google APIs as
47    the platform target.</p>
48    <p class="note"><strong>Note:</strong> Only Android 4.2.2 and higher versions of the
49    Google APIs platform include Google Play services.</p>
50  </li>
51  <li>Make a copy of the Google Play services library project.
52    <p>Copy the library project at
53  <code>&lt;android-sdk&gt;/extras/google/google_play_services/libproject/google-play-services_lib/</code>
54  to the location where you maintain your Android app projects.
55  <p>If you are using Eclipse, import the library project into your workspace.
56  Click <b>File > Import</b>, select <b>Android > Existing
57  Android Code into Workspace</b>, and browse to the copy of the library project to import it.</p>
58  </li>
59</ol>
60
61
62
63<h2 id="Setup">Set Up a Project with the Library</h2>
64
65<p>To set up a project to use the Google Play services SDK:</p>
66
67<ol>
68  <li>Reference the library project in your Android project.
69      <p>See the
70      <a href="{@docRoot}tools/projects/projects-eclipse.html#ReferencingLibraryProject">Referencing a Library Project for Eclipse</a>
71      or <a href="{@docRoot}tools/projects/projects-cmdline.html#ReferencingLibraryProject">Referencing a Library Project on the Command Line</a>
72      for more information on how to do this.</p>
73      <p class="note"><strong>Note:</strong>
74      You should be referencing a copy of the library that you copied to your development
75      workspace&mdash;you should not reference the library directly from the Android SDK directory.</p>
76  </li>
77  <li>If you are using <a href="{@docRoot}tools/help/proguard.html">ProGuard</a>, add the following
78      lines in the <code>&lt;project_directory&gt;/proguard-project.txt</code> file
79      to prevent ProGuard from stripping away required classes:
80<pre>
81-keep class * extends java.util.ListResourceBundle {
82    protected Object[][] getContents();
83}
84
85-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
86    public static final *** NULL;
87}
88
89-keepnames &#64;com.google.android.gms.common.annotation.KeepName class *
90-keepclassmembernames class * {
91    &#64;ccom.google.android.gms.common.annotation.KeepName *;
92}
93
94-keepnames class * implements android.os.Parcelable {
95    public static final ** CREATOR;
96}
97</pre>
98</ol>
99
100<p>Once you have the Google Play services library project added to your app project,
101you can begin developing features with the
102<a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p>
103
104
105
106<h2 id="ensure">Ensure Devices Have the Google Play services APK</h2>
107
108<p>As described in the <a href="{@docRoot}google/play-services/index.html">Google Play services
109introduction</a>, Google Play delivers service updates for users on
110Android 2.2 through the Google Play Store app. However, updates might not reach
111all users immediately.</p>
112
113<p class="caution">
114<strong>Important:</strong>
115    Because it is hard to anticipate the state of each device, you must <em>always</em> check for a
116    compatible Google Play services APK before you access Google Play services
117    features.  For many apps, the best time to check is during the
118    {@link android.app.Activity#onResume onResume()} method of the main activity.
119</p>
120
121<p>Here are four scenarios that describe the possible state of the Google Play services APK on
122a user's device:</p>
123<ol>
124    <li>
125        A recent version of the Google Play Store app is installed, and the most recent Google Play
126        services APK has been downloaded.
127    </li>
128    <li>
129        A recent version of the Google Play Store app is installed, but the most recent Google Play
130        services APK has <em>not</em> been downloaded.
131    </li>
132    <li>
133        An old version of the Google Play Store app, which does not proactively download Google Play
134        services updates, is present.
135    </li>
136    <li>
137        The Google Play services APK is missing or disabled on the device, which might happen if the
138        user explicitly uninstalls or disables it.
139    </li>
140</ol>
141<p>
142    Case 1 is the success scenario and is the most common. However, because the other scenarios can
143    still happen, you must handle them every time your app connects to a Google Play service to
144    ensure that the Google Play services APK is present, up-to-date, and enabled.
145</p>
146<p>
147    To help you, the Google Play services client library has utility methods to
148    determine whether or not the Google Play services APK is recent enough to support the
149    version of the client library you are using.  If not, the client library sends users to the
150    Google Play Store to download the recent version of the Google Play services APK.
151</p>
152
153<p class="note">
154<b>Note:</b>
155    The Google Play services APK is not visible by searching the Google Play Store. The client
156    library provides a deep link into the Google Play Store when it detects that the device has a
157    missing or incompatible Google Play services APK.
158</p>
159
160<p>
161    It is up to you choose the appropriate place in your app to do the following steps to check for
162    a valid Google Play services APK. For example, if Google Play services is required for your app,
163    you might want to do it when your app first launches. On the other hand, if Google Play services
164    is an optional part of your app, you can do these checks if the user navigates to that portion
165    of your app:
166</p>
167
168<ol>
169    <li>
170        Query for the status of Google Play services on the device with the
171<a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#isGooglePlayServicesAvailable(android.content.Context)"
172>{@code isGooglePlayServicesAvailable()}</a> method, which returns a result code.
173    </li>
174    <li>
175        If the result code is
176<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SUCCESS"
177>{@code SUCCESS}</a>,
178        then the Google Play services APK is up-to-date, and you can proceed as normal.
179    </li>
180    <li>
181        If the result code is
182<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_MISSING"
183>{@code SERVICE_MISSING}</a>,
184<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_VERSION_UPDATE_REQUIRED"
185>{@code SERVICE_VERSION_UPDATE_REQUIRED}</a>,
186        or
187<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_DISABLED"
188>{@code SERVICE_DISABLED}</a>, then
189  call <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)"
190  >{@code getErrorDialog()}</a>
191  to display an error message to the user, which allows the user to download the APK
192  from the Google Play Store or enable it in the device's system settings.
193    </li>
194</ol>