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