Home | History | Annotate | Download | only in play-services
      1 page.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
     28 Play 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
     30 to <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="eclipse">Using Eclipse with ADT</option>
     47   <option value="studio">Using Android Studio</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">
     65 apply plugin: 'android'
     66 ...
     67 
     68 dependencies {
     69     compile 'com.android.support:appcompat-v7:20.+'
     70     <strong>compile 'com.google.android.gms:play-services:6.1.+'</strong>
     71 }
     72 </pre>
     73 <p>Be sure you update this version number each time Google Play services is updated.</p>
     74   </li>
     75   <li>Save the changes and click <strong>Sync Project with Gradle Files</strong>
     76 <img src="{@docRoot}images/tools/sync-project.png" style="vertical-align:bottom;margin:0;height:19px" />
     77 in the toolbar.
     78   </li>
     79   <li>Open your app's manifest file and add the following tag as a child of the <a
     80 href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application>}</a>
     81 element:
     82 <pre>
     83 &lt;meta-data android:name="com.google.android.gms.version"
     84            android:value="&#64;integer/google_play_services_version" />
     85 </pre>
     86   </li>
     87 </ol>
     88 
     89 <p>You can now begin developing features with the
     90 <a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p>
     91 
     92 </div><!-- end studio -->
     93 
     94 <div class="select-ide eclipse">
     95 
     96 <p>To make the Google Play services APIs available to your app:</p>
     97 <ol>
     98   <li>Copy the library project at <code>&lt;android-sdk&gt;/extras/google/google_play_services/libproject/google-play-services_lib/</code> to the location where you maintain your Android app projects.</li>
     99   <li>Import the library project into your Eclipse workspace. Click
    100     <b>File > Import</b>, select <b>Android > Existing Android Code into
    101 Workspace</b>, and browse to the copy of the library project to import it.</li>
    102   <li>In your app project, reference Google Play services library project. See
    103     <a href="{@docRoot}tools/projects/projects-eclipse.html#ReferencingLibraryProject">Referencing a Library Project for Eclipse</a> for more information on how to
    104     do this.
    105   <p class="note"><strong>Note:</strong> You should be referencing a copy of the
    106     library that you copied to your development workspace&mdash;you should not
    107     reference the library directly from the Android SDK directory.</p>
    108   </li>
    109   <li>After you've added the Google Play services library as a dependency for your app project,
    110     open your app's manifest file and add the following tag as a child of the
    111     <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application>}</a>
    112 element:
    113     <pre>
    114 &lt;meta-data android:name="com.google.android.gms.version"
    115         android:value="&#64;integer/google_play_services_version" />
    116     </pre>
    117   </li>
    118 </ol>
    119 
    120 <p>Once you've set up your project to reference the library project,
    121 you can begin developing features with the
    122 <a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p>
    123 
    124 </div><!-- end eclipse -->
    125 
    126 <div class="select-ide other">
    127 
    128 <p>To make the Google Play services APIs available to your app:</p>
    129 <ol>
    130   <li>Copy the library project at <code>&lt;android-sdk&gt;/extras/google/google_play_services/libproject/google-play-services_lib/</code> to the location where you maintain your Android app projects.</li>
    131 
    132   <li>In your app project, reference the Google Play services library project. See
    133     <a href="{@docRoot}tools/projects/projects-cmdline.html#ReferencingLibraryProject">Referencing
    134     a Library Project on the Command Line</a> for more information on how to do this.
    135     <p class="note"><strong>Note:</strong>
    136 You should be referencing a copy of the library that you copied to your development
    137 workspace&mdash;you should not reference the library directly from the Android SDK directory.</p>
    138   </li>
    139   <li>After you've added the Google Play services library as a dependency for
    140     your app project, open your app's manifest file and add the following tag as
    141     a child of the
    142     <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application>}</a> element:
    143     <pre>
    144 &lt;meta-data android:name="com.google.android.gms.version"
    145         android:value="&#64;integer/google_play_services_version" />
    146     </pre>
    147   </li>
    148 </ol>
    149 
    150 <p>Once you've set up your project to reference the library project,
    151 you can begin developing features with the
    152 <a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p>
    153 
    154 </div><!-- end other -->
    155 
    156 
    157 <h2 id="Proguard">Create a Proguard Exception</h2>
    158 
    159 <p>To prevent <a href="{@docRoot}tools/help/proguard.html">ProGuard</a> from stripping away
    160 required classes, add the following lines in the
    161 <code>&lt;project_directory&gt;/proguard-project.txt</code> file:
    162 <pre>
    163 -keep class * extends java.util.ListResourceBundle {
    164     protected Object[][] getContents();
    165 }
    166 
    167 -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    168     public static final *** NULL;
    169 }
    170 
    171 -keepnames &#64;com.google.android.gms.common.annotation.KeepName class *
    172 -keepclassmembernames class * {
    173     &#64;com.google.android.gms.common.annotation.KeepName *;
    174 }
    175 
    176 -keepnames class * implements android.os.Parcelable {
    177     public static final ** CREATOR;
    178 }
    179 </pre>
    180 
    181 <p class="note"><strong>Note:</strong> When using Android Studio, you must add Proguard
    182 to your <code>gradle.build</code> file's build types. For more information, see the
    183 <a href="http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard"
    184 >Gradle Plugin User Guide</a>.
    185 </ol>
    186 
    187 
    188 <h2 id="ensure">Ensure Devices Have the Google Play services APK</h2>
    189 
    190 <p>As described in the <a href="{@docRoot}google/play-services/index.html">Google Play services
    191 introduction</a>, Google Play delivers service updates for users on
    192 Android 2.3 and higher through the Google Play Store app. However, updates might not reach
    193 all users immediately, so your app should verify the version available before attempting to
    194 perform API transactions.</p>
    195 
    196 <p class="caution">
    197 <strong>Important:</strong>
    198     Because it is hard to anticipate the state of each device, you must <em>always</em> check for a
    199     compatible Google Play services APK before you access Google Play services
    200     features.  For many apps, the best time to check is during the
    201     {@link android.app.Activity#onResume onResume()} method of the main activity.
    202 </p>
    203 
    204 <p>The Google Play services library includes utility methods that help you determine whether or not
    205 the Google Play services version on the device supports the version of the client library you are
    206 using. If the version on the device is too old, the system will take the user to Google Play Store
    207 in order to install the recent version of the Google Play services.</p>
    208 
    209 <p>Because each app uses Google Play services differently, it's up to you decide the appropriate
    210 place in your app to check verify the Google Play services version. For example, if Google Play
    211 services is required for your app at all times, you might want to do it when your app first
    212 launches. On the other hand, if Google Play services is an optional part of your app, you can check
    213 the version only once the user navigates to that portion of your app.</p>
    214 
    215 <p>To verify the Google Play services version, call <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#isGooglePlayServicesAvailable(android.content.Context)"
    216 >{@code isGooglePlayServicesAvailable()}</a>. If the result code is
    217 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SUCCESS"
    218 >{@code SUCCESS}</a>,
    219         then the Google Play services APK is up-to-date and you can continue to make a connection.
    220 If, however, the result code is
    221 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_MISSING"
    222 >{@code SERVICE_MISSING}</a>,
    223 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_VERSION_UPDATE_REQUIRED"
    224 >{@code SERVICE_VERSION_UPDATE_REQUIRED}</a>,
    225         or
    226 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_DISABLED"
    227 >{@code SERVICE_DISABLED}</a>, then the user needs to install an update. So,
    228   call <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)"
    229   >{@code GooglePlayServicesUtil.getErrorDialog()}</a> and pass it the result error code.
    230 This returns a {@link android.app.Dialog} you should show, which provides an appropriate message
    231 about the error and provides an action
    232 that takes the user to Google Play Store to install the update.</p>
    233 
    234 
    235 <p>To then begin a connection to Google Play services (required by most Google APIs such
    236 as Google Drive, Google+, and Games), read <a
    237 href="{@docRoot}google/auth/api-client.html">Accessing Google APIs</a>.</p>
    238