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