1page.title=Setting Up Google Play Services 2@jd:body 3 4 5 6<div id="qv-wrapper"> 7<div id="qv"> 8 9<h2>In this document</h2> 10<ol> 11 <li><a href="#Setup">Add Google Play Services to Your Project</a></li> 12 <li><a href="#Proguard">Create a Proguard Exception</a></li> 13 <li><a href="#ensure">Ensure Devices Have the Google Play services APK</a></li> 14</ol> 15 16 17</div> 18</div> 19 20<script> 21$(document).ready(function() { 22 setupIdeDocToggle(); 23}); 24</script> 25 26 27<p>To develop an app using the <a href="{@docRoot}reference/gms-packages.html">Google 28Play services APIs</a>, you need to set up your project with the Google Play services SDK. 29<p>If you haven't installed the Google Play services SDK yet, go get it now by following the guide 30to <a href="{@docRoot}sdk/installing/adding-packages.html">Adding SDK Packages</a>.</p> 31 32<p>To test your app when using the Google Play services SDK, you must use either:</p> 33<ul> 34 <li>A compatible Android 35 device that runs Android 2.3 or higher and includes Google Play Store.</li> 36 <li>The Android emulator with an <a href="{@docRoot}tools/devices/index.html">AVD</a> 37 that runs the Google APIs platform based on Android 4.2.2 or higher.</li> 38</ul> 39 40 41 42<h2 id="Setup">Add Google Play Services to Your Project</h2> 43 44<p> 45<select class="ide"> 46 <option value="studio">Using Android Studio</option> 47 <option value="eclipse">Using Eclipse with ADT</option> 48 <option value="other">Using something else</option> 49</select> 50</p> 51 52 53<div class="select-ide studio"> 54<p>To make the Google Play services APIs available to your app:</p> 55<ol> 56 <li>Open the <code>build.gradle</code> file inside your application module directory. 57 <p class="note"><strong>Note:</strong> Android Studio projects contain a top-level 58 <code>build.gradle</code> file and a <code>build.gradle</code> file for each module. 59 Be sure to edit the file for your application module. See 60 <a href="{@docRoot}sdk/installing/studio-build.html">Building Your Project with 61 Gradle</a> for more information about Gradle.</p></li> 62 <li>Add a new build rule under <code>dependencies</code> for the latest version of 63<code>play-services</code>. For example: 64<pre class="no-pretty-print"> 65apply plugin: 'com.android.application' 66... 67 68dependencies { 69 compile 'com.android.support:appcompat-v7:21.0.3' 70 <strong>compile 'com.google.android.gms:play-services:6.5.87'</strong> 71} 72</pre> 73<p>Be sure you update this version number each time Google Play services is updated.</p> 74<p class="note"><strong>Note:</strong> If the number of method references in your app exceeds the 75<a href="{@docRoot}tools/building/multidex.html">65K limit</a>, your app may fail to compile. You 76may be able to mitigate this problem when compiling your app by specifying only the specific Google 77Play services APIs your app uses, instead of all of them. For information on how to do this, 78see <a href="#split">Selectively compiling APIs into your executable</a>. 79 80 </li> 81 <li>Save the changes and click <strong>Sync Project with Gradle Files</strong> 82<img src="{@docRoot}images/tools/sync-project.png" style="vertical-align:bottom;margin:0;height:19px" /> 83in the toolbar. 84 </li> 85 <li>Open your app's manifest file and add the following tag as a child of the <a 86href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> 87element: 88<pre> 89<meta-data android:name="com.google.android.gms.version" 90 android:value="@integer/google_play_services_version" /> 91</pre> 92 </li> 93</ol> 94 95<p>You can now begin developing features with the 96<a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p> 97 98<h3 id="split">Selectively compiling APIs into your executable</h3> 99 100<p>In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs 101into your app. In some cases, doing so made it more difficult to keep the number of methods 102in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.</p> 103 104<p>From version 6.5, you can instead selectively compile Google Play service APIs into your app. For 105example, to include only the Google Fit and Android Wear APIs, replace the following line in your 106<code>build.gradle</code> file:</p> 107 108<pre class="no-pretty-print"> 109compile 'com.google.android.gms:play-services:6.5.87' 110</pre> 111 112<p>with these lines:</p> 113 114<pre class="no-pretty-print"> 115compile 'com.google.android.gms:play-services-fitness:6.5.87' 116compile 'com.google.android.gms:play-services-wearable:6.5.87' 117</pre> 118 119<p>Table 1 shows a list of the separate APIs that you can include when compiling your app, and 120how to describe them in your <code>build.gradle</code> file. Some APIs do not have a separate 121library; include them by including the base library. (This lib is automatically included when 122you include an API that does have a separate library.)</p> 123 124<p class="table-caption" id="table1"> 125<strong>Table 1.</strong> Individual APIs and corresponding <code>build.gradle</code> descriptions.</p> 126 127<table> 128 <tr> 129 <th scope="col">Google Play services API</th> 130 <th scope="col">Description in <code>build.gradle</code></th> 131 </tr> 132 <tr> 133 <td>Google+</td> 134 <td>com.google.android.gms:play-services-plus:6.5.87</td> 135 </tr> 136 <tr> 137 <td>Google Account Login</td> 138 <td>com.google.android.gms:play-services-identity:6.5.87</td> 139 </tr> 140 <tr> 141 <td>Google Activity Recognition</td> 142 <td>com.google.android.gms:play-services-location:6.5.87</td> 143 </tr> 144 <tr> 145 <td>Google App Indexing</td> 146 <td>com.google.android.gms:play-services-appindexing:6.5.87</td> 147 </tr> 148 <tr> 149 <td>Google Cast</td> 150 <td>com.google.android.gms:play-services-cast:6.5.87</td> 151 </tr> 152 <tr> 153 <td>Google Drive</td> 154 <td>com.google.android.gms:play-services-drive:6.5.87</td> 155 </tr> 156 <tr> 157 <td>Google Fit</td> 158 <td>com.google.android.gms:play-services-fitness:6.5.87</td> 159 </tr> 160 <tr> 161 <td>Google Maps</td> 162 <td>com.google.android.gms:play-services-maps:6.5.87</td> 163 </tr> 164 <tr> 165 <td>Google Mobile Ads</td> 166 <td>com.google.android.gms:play-services-ads:6.5.87</td> 167 </tr> 168 <tr> 169 <td>Google Panorama Viewer</td> 170 <td>com.google.android.gms:play-services-panorama:6.5.87</td> 171 </tr> 172 <tr> 173 <td>Google Play Game services</td> 174 <td>com.google.android.gms:play-services-games:6.5.87</td> 175 </tr> 176 <tr> 177 <td>Google Wallet</td> 178 <td>com.google.android.gms:play-services-wallet:6.5.87</td> 179 </tr> 180 <tr> 181 <td>Android Wear</td> 182 <td>com.google.android.gms:play-services-wearable:6.5.87</td> 183 </tr> 184 <tr> 185 <td> 186 Google Actions<br> 187 Google Analytics<br> 188 Google Cloud Messaging<br> 189 </td> 190 <td>com.google.android.gms:play-services-base:6.5.87</td> 191 </tr> 192 193</table> 194 195</div><!-- end studio --> 196 197<div class="select-ide eclipse"> 198 199<p>To make the Google Play services APIs available to your app:</p> 200<ol> 201 <li>Copy the library project at 202 <code><android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/</code> 203 to the location where you maintain your Android app projects.</li> 204 <li>Import the library project into your Eclipse workspace. Click 205 <b>File > Import</b>, select <b>Android > Existing Android Code into 206Workspace</b>, and browse to the copy of the library project to import it.</li> 207 <li>In your app project, reference Google Play services library project. See 208 <a href="{@docRoot}tools/projects/projects-eclipse.html#ReferencingLibraryProject">Referencing a Library Project for Eclipse</a> for more information on how to 209 do this. 210 <p class="note"><strong>Note:</strong> You should be referencing a copy of the 211 library that you copied to your development workspace—you should not 212 reference the library directly from the Android SDK directory.</p> 213 </li> 214 <li>After you've added the Google Play services library as a dependency for your app project, 215 open your app's manifest file and add the following tag as a child of the 216 <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> 217element: 218 <pre> 219<meta-data android:name="com.google.android.gms.version" 220 android:value="@integer/google_play_services_version" /> 221 </pre> 222 </li> 223</ol> 224 225<p>Once you've set up your project to reference the library project, 226you can begin developing features with the 227<a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p> 228 229</div><!-- end eclipse --> 230 231<div class="select-ide other"> 232 233<p>To make the Google Play services APIs available to your app:</p> 234<ol> 235 <li>Copy the library project at <code><android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/</code> to the location where you maintain your Android app projects.</li> 236 237 <li>In your app project, reference the Google Play services library project. See 238 <a href="{@docRoot}tools/projects/projects-cmdline.html#ReferencingLibraryProject">Referencing 239 a Library Project on the Command Line</a> for more information on how to do this. 240 <p class="note"><strong>Note:</strong> 241You should be referencing a copy of the library that you copied to your development 242workspace—you should not reference the library directly from the Android SDK directory.</p> 243 </li> 244 <li>After you've added the Google Play services library as a dependency for 245 your app project, open your app's manifest file and add the following tag as 246 a child of the 247 <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> element: 248 <pre> 249<meta-data android:name="com.google.android.gms.version" 250 android:value="@integer/google_play_services_version" /> 251 </pre> 252 </li> 253</ol> 254 255<p>Once you've set up your project to reference the library project, 256you can begin developing features with the 257<a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p> 258 259</div><!-- end other --> 260 261 262<h2 id="Proguard">Create a Proguard Exception</h2> 263 264<p>To prevent <a href="{@docRoot}tools/help/proguard.html">ProGuard</a> from stripping away 265required classes, add the following lines in the 266<code><project_directory>/proguard-project.txt</code> file: 267<pre> 268-keep class * extends java.util.ListResourceBundle { 269 protected Object[][] getContents(); 270} 271 272-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable { 273 public static final *** NULL; 274} 275 276-keepnames @com.google.android.gms.common.annotation.KeepName class * 277-keepclassmembernames class * { 278 @com.google.android.gms.common.annotation.KeepName *; 279} 280 281-keepnames class * implements android.os.Parcelable { 282 public static final ** CREATOR; 283} 284</pre> 285 286<p class="note"><strong>Note:</strong> When using Android Studio, you must add Proguard 287to your <code>build.gradle</code> file's build types. For more information, see the 288<a href="http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard" 289>Gradle Plugin User Guide</a>. 290</ol> 291 292 293<h2 id="ensure">Ensure Devices Have the Google Play services APK</h2> 294 295<p>As described in the <a href="{@docRoot}google/play-services/index.html">Google Play services 296introduction</a>, Google Play delivers service updates for users on 297Android 2.3 and higher through the Google Play Store app. However, updates might not reach 298all users immediately, so your app should verify the version available before attempting to 299perform API transactions.</p> 300 301<p class="caution"> 302<strong>Important:</strong> 303 Because it is hard to anticipate the state of each device, you must <em>always</em> check for a 304 compatible Google Play services APK before you access Google Play services 305 features. For many apps, the best time to check is during the 306 {@link android.app.Activity#onResume onResume()} method of the main activity. 307</p> 308 309<p>The Google Play services library includes utility methods that help you determine whether or not 310the Google Play services version on the device supports the version of the client library you are 311using. If the version on the device is too old, the system will take the user to Google Play Store 312in order to install the recent version of the Google Play services.</p> 313 314<p>Because each app uses Google Play services differently, it's up to you decide the appropriate 315place in your app to check verify the Google Play services version. For example, if Google Play 316services is required for your app at all times, you might want to do it when your app first 317launches. On the other hand, if Google Play services is an optional part of your app, you can check 318the version only once the user navigates to that portion of your app.</p> 319 320<p>To verify the Google Play services version, call <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#isGooglePlayServicesAvailable(android.content.Context)" 321>{@code isGooglePlayServicesAvailable()}</a>. If the result code is 322<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SUCCESS" 323>{@code SUCCESS}</a>, 324 then the Google Play services APK is up-to-date and you can continue to make a connection. 325If, however, the result code is 326<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_MISSING" 327>{@code SERVICE_MISSING}</a>, 328<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_VERSION_UPDATE_REQUIRED" 329>{@code SERVICE_VERSION_UPDATE_REQUIRED}</a>, 330 or 331<a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_DISABLED" 332>{@code SERVICE_DISABLED}</a>, then the user needs to install an update. So, 333 call <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)" 334 >{@code GooglePlayServicesUtil.getErrorDialog()}</a> and pass it the result error code. 335This returns a {@link android.app.Dialog} you should show, which provides an appropriate message 336about the error and provides an action 337that takes the user to Google Play Store to install the update.</p> 338 339 340<p>To then begin a connection to Google Play services (required by most Google APIs such 341as Google Drive, Google+, and Games), read <a 342href="{@docRoot}google/auth/api-client.html">Accessing Google APIs</a>.</p> 343