Home | History | Annotate | Download | only in apps
      1 page.title=Creating Android Wear Apps for China
      2 parent.title=Training
      3 parent.link=creating.html
      4 page.tags= "wearable", "apps", "china"
      5 page.article=true
      6 
      7 @jd:body
      8 
      9 <div id="tb-wrapper">
     10 <div id="tb">
     11 <h2>This lesson teaches you to</h2>
     12 <ol>
     13   <li><a href="#ChinaSDK">Support Your App on Android Wear for China</a></li>
     14   <li><a href="#other-services">Use Other Google Play Services APIs</a></li>
     15 </ol>
     16 <h2>Dependencies and prerequisites</h2>
     17 <ol>
     18 <li> Android 4.3 (API Level 18) or higher on the handset and wearable device</li>
     19 </ol>
     20 <h2>Downloads</h2>
     21 <ol>
     22 <div class="download-box">
     23 <a href="https://dl.google.com/androidwear/developers/china/google-play-services-7-8-87.zip"
     24   class="button">Standalone Client Library</a>
     25 <p class="filename">google-play-services-7-8-87.zip</p>
     26 </div>
     27 </ol>
     28 </div>
     29 </div>
     30 
     31 
     32 <p>
     33 Handsets sold in China do not have Google Play services preinstalled. For this reason, wearable
     34 apps running on devices in China must communicate with paired handsets through the Android Wear
     35 companion app.  To enable you to develop a single APK that works with both Android Wear for China and
     36  Android Wear in the rest of the world, we provide a special variant of the Google Play services
     37 client library.</p>
     38 
     39 <p>
     40 This client library is compatible with Android 4.3 (API level 18) and higher, and you can simply
     41 drop it into your app. You do not need to write any new code. Instead, you change several project
     42 configuration settings, and re-compile your app.
     43 </p>
     44 
     45 
     46 <p>The rest of this page explains how to perform this process.</p>
     47 
     48 
     49 
     50 <h2 id = "ChinaSDK">Support Your App on Android Wear for China</h2>
     51 
     52 <p>
     53 <p>In order to support your wearable app on all handsets, you must download and add the Google Play
     54 Services 7.8.87 client library as a Maven repository in your project, configure your
     55 development project to use it, and re-compile your app.
     56 </p>
     57 
     58 <h3>Add the Google Play Services 7.8.87 library</h3>
     59 
     60 <p>The Google Play services 7.8.87 client library is distributed as a Maven repository. To add this
     61 repository to your project:</p>
     62 
     63 <ol>
     64 <li><a href="https://dl.google.com/androidwear/developers/china/google-play-services-7-8-87.zip">
     65 Download</a> the client library.
     66 The filename is {@code google-play-services-7-8-87.zip}.</li>
     67 <li>Create a local Maven repository by extracting the {@code google-play-services-7-8-87/} directory
     68 from the downloaded zip file, and placing it into the root directory of your project.
     69 </li>
     70 <li>In your top-level project {@code build.gradle} file, specify the location of the newly created
     71 local Maven {@code google-play-services-7-8-87} repository.
     72 </li>
     73 <p>
     74 The following example shows how to do so:
     75 </p>
     76 <pre>
     77 allprojects {
     78   repositories {
     79 
     80         maven {
     81                 url "${rootProject.projectDir}/google-play-services-7-8-87"
     82               }
     83        // ... other repositories may go here ...
     84 
     85     }</pre>
     86 </ol>
     87 <h3>Configure your app to use the library</h3>
     88 <p>In the {@code build.gradle} file of your <em>mobile</em> module replace the Google Play
     89  services dependency with a reference to the client library from the newly added repository. The
     90  following example shows how to do so:
     91 </p>
     92 
     93 
     94 <pre>
     95 dependencies{
     96     ...
     97     wearApp project(':wear')
     98     compile 'com.google.android.gms:play-services-wearable:7.8.87'
     99     ...
    100     }
    101 </pre>
    102 <p>The {@code build.gradle} file of your <em>wear</em> module must also use this version of the
    103 client library, for example:
    104 </p>
    105 <pre>
    106 dependencies {
    107     compile 'com.google.android.support:wearable:1.3.0'
    108     compile 'com.google.android.gms:play-services-wearable:7.8.87'
    109 }
    110 </pre>
    111 
    112 <p class="note"><strong>Note:</strong> If you are using any other Google Play services APIs in your
    113 wearable app, you must selectively add those Google Play service APIs into your app and explicitly
    114  specify the 7.8.87 version. For example to include the Google location API in your wearable app,
    115  add the following line in your {@code build.gradle} file:
    116 </p>
    117 <pre>
    118 compile 'com.google.android.gms:play-services-location:7.8.87'
    119 </pre>
    120 </p>
    121 <h3>Build the project</h3>
    122 
    123 
    124 <p>You can now <a href="{@docRoot}training/wearables/apps/packaging.html">build
    125 </a>a new version of your app and deploy it to Android handsets globally.</p>
    126 </ol>
    127 
    128 
    129 <h2 id= "other-services">Use Other Google Play services APIs</h2>
    130 
    131 <p>
    132 If your app uses Google Play services APIs other than the Wearable API, then your app needs to check
    133 whether these APIs are available to use during runtime and respond appropriately. There are two ways
    134 to check the availability of Google Play service APIs:
    135 </p>
    136 
    137 <ol>
    138 <li>Use a separate <a href="https://developers.google.com/android/reference/com/google/android/gms/
    139 common/api/GoogleApiClient.html">{@code GoogleApiClient}</a> instance for connecting to other APIs.
    140 This interface contains callbacks to alert your app to the success or failure of the connection.
    141 To learn how to handle connection failures,
    142 see <a href="https://developers.google.com/android/guides/api-client">Accessing Google APIs</a>.</li>
    143 
    144 <li>Use the <a href="https://developers.google.com/android/reference/com/google/android/gms/common/
    145 api/GoogleApiClient.Builder.html#addApiIfAvailable(com.google.android.gms.common.api.Api<O>, O,
    146 com.google.android.gms.common.api.Scope...)"> {@code addApiIfAvailable()}</a> method of
    147 <a href="https://developers.google.com/android/
    148 reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html">{@code GoogleApiClient.Builder}
    149 </a>
    150 to connect to the required APIs. After the <a href = "https://developers.google.com/android/reference/
    151 com/google/android/gms/common/api/GoogleApiClient.ConnectionCallbacks#onConnected(android.os.Bundle)">
    152 {@code onConnected()}</a> callback fires, check if each of the requested API is connected correctly
    153 using the <a href="https://developers.google.com/android/reference/com/google/android/gms/common/api/
    154 GoogleApiClient.html#hasConnectedApi(com.google.android.gms.common.api.Api<?>)">
    155  {@code hasConnectedApi()}</a> method.
    156 
    157 </ol>
    158