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