Home | History | Annotate | Download | only in apps
      1 page.title=Creating and Running a Wearable App
      2 page.tags=wear
      3 helpoutsWidget=true
      4 
      5 @jd:body
      6 
      7 <div id="tb-wrapper">
      8 <div id="tb">
      9 <h2>This lesson teaches you to</h2>
     10 <ol>
     11   <li><a href="#UpdateSDK">Update Your SDK</a></li>
     12   <li><a href="#SetupEmulator">Set Up an Android Wear Emulator or Device</a></li>
     13   <li><a href="#CreateProject">Create a Project</a></li>
     14   <li><a href="#Install">Install the Wearable App</a></li>
     15   <li><a href="#Libraries">Include the Correct Libraries</a></li>
     16 </ol>
     17 </div>
     18 </div>
     19 
     20 <p>Wearable apps run directly on the wearable device, giving you access to low-level
     21 hardware such as sensors, activities, services, and more, right
     22 on the wearable.</p>
     23 
     24 <p>A companion handheld app that contains the
     25 wearable app is also required when you want to publish to the Google Play store.
     26 Wearables don't support the Google Play store, so users download the companion handheld app,
     27 which automatically pushes the wearable app to the wearable. The handheld app is also
     28 useful for doing heavy processing, network actions, or other work and
     29 sending the results to the wearable.
     30 </p>
     31 
     32 <p>This lesson goes over how to set up a device or emulator and create one project to contain
     33 both your wearable and handheld apps.
     34 </p>
     35 
     36 <h2 id="UpdateSDK">Update Your SDK</h2>
     37 
     38 <p>Before you begin building wearable apps, you must:</p>
     39 
     40 <ul>
     41   <li><strong>Update your SDK tools to version 23.0.0 or higher</strong>
     42     <br>
     43     The updated SDK tools enable you to build and test wearable apps.
     44   </li>
     45   <li><strong>Update your SDK with Android 4.4W.2 (API 20) or higher</strong>
     46     <br>
     47     The updated platform version provides new APIs for wearable apps.
     48   </li>
     49 </ul>
     50 
     51 <p>To update your SDK with these components, see
     52 <a href="{@docRoot}studio/intro/update.html#GetTools"> Get the latest SDK tools</a>.</p>
     53 
     54 <p class="note"><strong>Note:</strong> If you plan to make your Wear apps available for China,
     55   you must use the special release version 7.8.87 of the Google Play services client library to handle
     56   communication between handset and wearable.
     57 
     58  For information on how to configure Wear apps for China, see
     59  <a href="{@docRoot}training/wearables/apps/creating-app-china.html"> Creating Android Wear Apps for
     60  China</a>.</p>
     61 
     62 <h2 id="SetupEmulator">Set Up an Android Wear Emulator or Device</h2>
     63 <p>We recommend that you develop on real hardware so you can better
     64 gauge the user experience. However, the emulator lets you test out different
     65 types of screen shapes, which is useful for testing.</p>
     66 
     67 <h3>Set up an Android Wear Virtual Device</h3>
     68 
     69 <p>To set up an Android Wear virtual device:</p>
     70 <ol>
     71   <li>Click <b>Tools > Android > AVD Manager</b>.</li>
     72   <li>Click <b>Create Virtual Device...</b>.</li>
     73   <ol>
     74     <li>Click <b>Wear</b> in the Category list:</li>
     75     <li>Select Android Wear Square or Android Wear Round.</li>
     76     <li>Click <b>Next</b>.</li>
     77     <li>Select a release name (for example, KitKat Wear).</li>
     78     <li>Click <b>Next</b>.</li>
     79     <li>(Optional) Change any preferences for your virtual device.</li>
     80     <li>Click <b>Finish</b>.</li>
     81   </ol>
     82 <li>Start the emulator:
     83 <ol>
     84   <li>Select the virtual device you just created.</li>
     85   <li>Click the <b>Play</b> button.</li>
     86   <li>Wait until the emulator initializes and shows the Android Wear home screen.</li>
     87 </ol>
     88 </li>
     89 <li>Pair your handheld with the emulator:
     90 <ol>
     91   <li>On your handheld, install the Android Wear app from Google Play.</li>
     92   <li>Connect the handheld to your machine through USB.</li>
     93   <li>Forward the AVD's communication port to the connected handheld device (you must
     94   do this every time the handheld is connected):
     95   <pre>adb -d forward tcp:5601 tcp:5601</pre>
     96   </li>
     97   <li>Start the Android Wear app on your handheld device and connect to the emulator.</li>
     98   <li>Tap the menu on the top right corner of the Android Wear app and select
     99   <b>Demo Cards</b>.</li>
    100   <li>The cards you select appear as notifications on the home screen of the emulator.</li>
    101 </ol>
    102 </li>
    103 </ol>
    104 
    105 <h3 id="SetupDevice">Set Up an Android Wear Device</h3>
    106 <p>To set up an Android Wear device:</p>
    107 <ol>
    108   <li>Install the Android Wear app, available on Google Play, on your handheld.</li>
    109   <li>Follow the app's instructions to pair your handheld with your wearable.
    110   This allows you to test out synced handheld notifications, if you're building them.</li>
    111   <li>Leave the Android Wear app open on your phone.</li>
    112   <li>Enable adb debugging on the Android Wear device.</li>
    113   <ol>
    114     <li>Go to <strong>Settings > About</strong>.</li>
    115     <li>Tap <strong>Build number</strong> seven times.</li>
    116     <li>Swipe right to return to the Settings menu.</li>
    117     <li>Go to <strong>Developer options</strong> at the bottom of the screen.
    118     </li>
    119     <li>Tap <strong>ADB Debugging</strong> to enable adb.</li>
    120   </ol>
    121   <li>Connect the wearable to your machine through USB, so you can install apps directly to it
    122   as you develop. A message appears on both the wearable and the Android Wear app prompting you to
    123   allow debugging.</li>
    124 
    125   <p class="note"><strong>Note:</strong> If you can not connect your wearable
    126   to your machine via USB, you can try
    127   <a href="{@docRoot}training/wearables/apps/bt-debugging.html">connecting over Bluetooth</a>.
    128   </p>
    129 
    130   <li>On the Android Wear app, check <strong>Always allow from this computer</strong> and tap
    131   <strong>OK</strong>.</li>
    132 </ol>
    133 
    134 <p>The <i>Android</i> tool window on Android Studio shows the system log from the
    135 wearable. The wearable should also be listed when you run the <code>adb devices</code> command.</p>
    136 
    137 <h2 id="CreateProject">Create a Project</h2>
    138 
    139 <p>To begin development, create an app project that contains
    140  wearable and handheld app modules. In Android Studio, click <b>File</b> >
    141  <b>New Project</b> and follow the Project Wizard instructions, as described in
    142  <a href="{@docRoot}studio/projects/create-project.html">Creating a
    143 Project</a>. As you follow the wizard, enter the following information:</p>
    144 
    145 <ol>
    146   <li>In the <b>Configure your Project</b> window, enter a name for your app and a package
    147   name.</li>
    148   <li>In the <b>Form Factors</b> window:
    149     <ul>
    150       <li>Select <b>Phone and Tablet</b> and select <b>API 9: Android 2.3 (Gingerbread)</b>
    151       under <b>Minimum SDK</b>.</li>
    152       <li>Select <b>Wear</b> and select <b>API 20: Android 4.4 (KitKat Wear)</b>
    153       under <b>Minimum SDK</b>.</li>
    154     </ul>
    155   </li>
    156   <li>In the first <b>Add an Activity</b> window, add a blank activity for mobile.</li>
    157   <li>In the second <b>Add an Activity</b> window, add a blank activity for Wear.</li>
    158 </ol>
    159   <p>When the wizard completes, Android Studio creates a new project with two modules, <b>mobile</b>
    160   and <b>wear</b>. You now have a project for both your handheld and wearable apps for which you can
    161   create activities, services, and custom layouts. The handheld app does most of
    162   the heavy lifting, such as network communications, intensive processing, or tasks that require
    163   long amounts of user interaction. When the app completes these operations, your app should
    164   notify the wearable of the results through notifications or by syncing and sending data to
    165   the wearable.</p>
    166 
    167   <p class="note"><b>Note:</b> The <b>wear</b> module also contains a "Hello World" activity that
    168   uses a
    169   <a href="{@docRoot}reference/android/support/wearable/view/WatchViewStub.html"><code>WatchViewStub</code></a>.
    170   This class inflates a layout based on whether the device's screen is round or square. The
    171   <a href="{@docRoot}reference/android/support/wearable/view/WatchViewStub.html"><code>WatchViewStub</code></a>
    172   class is one of the UI widgets that the
    173   <a href="{@docRoot}training/wearables/apps/layouts.html#UiLibrary">wearable support library</a>
    174   provides.
    175   </p>
    176 </li>
    177 
    178 <h2 id="Install">Install the Wearable App</h2>
    179 
    180 <p>When developing, you install apps directly to the wearable like with handheld apps. Use
    181 either <code>adb install</code> or the <b>Play</b> button on Android Studio.</p>
    182 
    183 <p>When you're ready to publish your app to users, you embed the wearable app inside of the
    184 handheld app. When a user installs the handheld app from Google Play, a connected wearable
    185 automatically receives the wearable app.</p>
    186 
    187 <p class="note"><b>Note:</b> The automatic installation of wearable apps
    188 does not work when you are signing apps with a debug key and only works with release keys. See
    189 <a href="{@docRoot}training/wearables/apps/packaging.html">Packaging Wearable Apps</a> for
    190 complete information on how to properly package wearable apps.</p>
    191 
    192 <li>
    193 To install the "Hello World" app to the wearable, select <b>wear</b> from the <b>Run/Debug
    194 configuration</b> drop-down menu and click the <b>Play</b> button. The activity shows up on the
    195 wearable and prints out "Hello world!"
    196 </li></ol>
    197 <h2 id="Libraries">Include the Correct Libraries</h2>
    198 
    199 <p>As part of the Project Wizard, the correct
    200 dependencies are imported for you in the appropriate module's <code>build.gradle</code> file.
    201 However, these dependencies are not required, so read the following descriptions to find out if you
    202 need them or not:</p>
    203 
    204 <b>Notifications</b>
    205 <p>The <a href="{@docRoot}tools/support-library/features.html#v4">Android
    206 v4 support library</a> (or v13, which includes v4)
    207 contains the APIs to extend your existing notifications on handhelds to support wearables.</p>
    208 
    209 <p>For notifications that appear only on
    210 the wearable (meaning, they are issued by an app that runs on the wearable), you can just use the
    211 standard framework APIs (API Level 20) on the wearable and remove the support library
    212 dependency in the <b>mobile</b> module of your project.
    213 </p>
    214 
    215 <b>Wearable Data Layer</b>
    216 <p>To sync and send data between wearables and handhelds with the Wearable Data Layer APIs,
    217 you need the latest version of
    218 <a href="{@docRoot}google/play-services/setup.html">Google Play services</a>.
    219 If you're not using these APIs, remove the dependency from both modules.</p>
    220 
    221 <b>Wearable UI support library</b>
    222 <p>This is an unofficial library that includes
    223 <a href="{@docRoot}training/wearables/apps/layouts.html#UiLibrary">UI widgets designed for
    224 wearables</a>. We encourage you to use them in your apps, because they exemplify best practices,
    225 but they can still change at any time. However, if the libraries are updated, your apps won't
    226 break since they are compiled into your app. To get new features from an updated library, you just
    227 need to statically link the new version and update your app accordingly. This library is only
    228 applicable if you create wearable apps.
    229 </p>
    230 
    231 <p>In the next lessons, you'll learn how to create layouts designed for wearables as well as how
    232 to use the various voice actions that are supported by the platform.</p>
    233