Home | History | Annotate | Download | only in play-services
      1 page.title=Setup 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
      6 Play services APIs</a>, you must download the Google Play services SDK
      7 from the <a href="{@docRoot}tools/help/sdk-manager.html">SDK Manager</a>.
      8 The 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
     19 both 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 </pre>
     85 </ol>
     86 
     87 <p>Once you have the Google Play services library project added to your app project,
     88 you can begin developing features with the
     89 <a href="{@docRoot}reference/gms-packages.html">Google Play services APIs</a>.</p>
     90 
     91 
     92 
     93 <h2 id="ensure">Ensure Devices Have the Google Play services APK</h2>
     94 
     95 <p>As described in the <a href="{@docRoot}google/play-services/index.html">Google Play services
     96 introduction</a>, Google Play delivers service updates for users on
     97 Android 2.2 through the Google Play Store app. However, updates might not reach
     98 all users immediately.</p>
     99 
    100 <p class="caution">
    101 <strong>Important:</strong>
    102     Because it is hard to anticipate the state of each device, you must <em>always</em> check for a
    103     compatible Google Play services APK before you access Google Play services
    104     features.  For many apps, the best time to check is during the
    105     {@link android.app.Activity#onResume onResume()} method of the main activity.
    106 </p>
    107 
    108 <p>Here are four scenarios that describe the possible state of the Google Play services APK on
    109 a user's device:</p>
    110 <ol>
    111     <li>
    112         A recent version of the Google Play Store app is installed, and the most recent Google Play
    113         services APK has been downloaded.
    114     </li>
    115     <li>
    116         A recent version of the Google Play Store app is installed, but the most recent Google Play
    117         services APK has <em>not</em> been downloaded.
    118     </li>
    119     <li>
    120         An old version of the Google Play Store app, which does not proactively download Google Play
    121         services updates, is present.
    122     </li>
    123     <li>
    124         The Google Play services APK is missing or disabled on the device, which might happen if the
    125         user explicitly uninstalls or disables it.
    126     </li>
    127 </ol>
    128 <p>
    129     Case 1 is the success scenario and is the most common. However, because the other scenarios can
    130     still happen, you must handle them every time your app connects to a Google Play service to
    131     ensure that the Google Play services APK is present, up-to-date, and enabled.
    132 </p>
    133 <p>
    134     To help you, the Google Play services client library has utility methods to
    135     determine whether or not the Google Play services APK is recent enough to support the
    136     version of the client library you are using.  If not, the client library sends users to the
    137     Google Play Store to download the recent version of the Google Play services APK.
    138 </p>
    139 
    140 <p class="note">
    141 <b>Note:</b>
    142     The Google Play services APK is not visible by searching the Google Play Store. The client
    143     library provides a deep link into the Google Play Store when it detects that the device has a
    144     missing or incompatible Google Play services APK.
    145 </p>
    146 
    147 <p>
    148     It is up to you choose the appropriate place in your app to do the following steps to check for
    149     a valid Google Play services APK. For example, if Google Play services is required for your app,
    150     you might want to do it when your app first launches. On the other hand, if Google Play services
    151     is an optional part of your app, you can do these checks if the user navigates to that portion
    152     of your app:
    153 </p>
    154 
    155 <ol>
    156     <li>
    157         Query for the status of Google Play services on the device with the
    158 <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#isGooglePlayServicesAvailable(android.content.Context)"
    159 >{@code isGooglePlayServicesAvailable()}</a> method, which returns a result code.
    160     </li>
    161     <li>
    162         If the result code is
    163 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SUCCESS"
    164 >{@code SUCCESS}</a>,
    165         then the Google Play services APK is up-to-date, and you can proceed as normal.
    166     </li>
    167     <li>
    168         If the result code is
    169 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_MISSING"
    170 >{@code SERVICE_MISSING}</a>,
    171 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_VERSION_UPDATE_REQUIRED"
    172 >{@code SERVICE_VERSION_UPDATE_REQUIRED}</a>,
    173         or
    174 <a href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SERVICE_DISABLED"
    175 >{@code SERVICE_DISABLED}</a>, then
    176   call <a href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)"
    177   >{@code getErrorDialog()}</a>
    178   to display an error message to the user, which allows the user to download the APK
    179   from the Google Play Store or enable it in the device's system settings.
    180     </li>
    181 </ol>