1page.title=Packaging Wearable Apps 2page.tags=wear 3helpoutsWidget=true 4 5@jd:body 6 7<div id="tb-wrapper"> 8<div id="tb"> 9 10<h2>This lesson teaches you to</h2> 11<ol> 12 <li><a href="#Studio">Package with Android Studio</a></li> 13 <li><a href="#PackageManually">Package Manually</a></li> 14 <li><a href="#AssetCompression">Turn off Asset Compression</a></li> 15</ol> 16</div> 17</div> 18 19<p>When publishing to users, you must package a wearable app inside of a handheld app, 20because users cannot browse and install apps directly on the wearable. If packaged properly, 21when users download the handheld app, the system automatically pushes the wearable app to the 22paired wearable. 23</p> 24 25<p class="note"><b>Note:</b> This feature doesn't work when you are signing your apps with 26a debug key when developing. While developing, installing apps with <code>adb install</code> or 27Android Studio directly to the wearable is required.</p> 28 29 30<h2 id="Studio">Package with Android Studio</h2> 31<p>To properly package a wearable app in Android Studio:</p> 32 33<ol> 34 <li>Include all the permissions declared in the manifest file of the wearable app module 35 in the manifest file of the handheld app module. For example, if you specify the {@link 36 android.Manifest.permission#VIBRATE} permission for the wearable app, you must also add that 37 permission to the handheld app.</li> 38 39 <li>Ensure that both the wearable and handheld app modules have the same package name and 40 version number.</li> 41 42 <li>Declare a Gradle dependency in the handheld app's <code>build.gradle</code> file 43 that points to the wearable app module: 44<pre> 45dependencies { 46 compile 'com.google.android.gms:play-services:5.0.+@aar' 47 compile 'com.android.support:support-v4:20.0.+'' 48 <b>wearApp project(':wearable')</b> 49} 50</pre> 51 </li> 52 <li>Click <b>Build > Generate Signed APK...</b> and follow the on-screen instructions 53 to specify your release keystore and sign your app. Android Studio exports the signed 54 handheld app with the wearable app embedded in it automatically into your project's root folder. 55 56 <p>Alternatively, you can sign both apps from the command line using the 57 <a href="{@docRoot}sdk/installing/studio-build.html#gradleWrapper">Gradle wrapper</a>. Both apps 58 must be signed to have the automatic pushing of the wearable app work.</p> 59 60 <p>Store your key file location and credentials in environment variables and run the Gradle 61 wrapper as follows:</p> 62 63<pre class="no-pretty-print"> 64./gradlew assembleRelease \ 65 -Pandroid.injected.signing.store.file=$KEYFILE \ 66 -Pandroid.injected.signing.store.password=$STORE_PASSWORD \ 67 -Pandroid.injected.signing.key.alias=$KEY_ALIAS \ 68 -Pandroid.injected.signing.key.password=$KEY_PASSWORD 69</pre> 70 </li> 71</ol> 72 73<h3>Signing the wearable and handheld app separately</h3> 74<p>If your build process requires signing the wearable app separately from the handheld app, 75you can declare the following Gradle rule in the handheld module's <code>build.gradle</code> to 76embed the previously-signed wearable app:</p> 77 78<pre> 79dependencies { 80 ... 81 wearApp files('/path/to/wearable_app.apk') 82} 83</pre> 84 85<p>You then sign your handheld app in any manner you wish (either with the Android Studio 86<b>Build > Generate Signed APK...</b> menu item or with Gradle <code>signingConfig</code> rules as 87described in the previous section.</p> 88 89<h2 id="PackageManually">Package Manually</h2> 90<p> 91It's still possible to package the wearable app into the handheld app manually 92if you are using another IDE or another method of building. 93</p> 94 95<ol> 96 <li>Include all the permissions declared in the manifest file of the wearable app 97 in the manifest file of the mobile app. For example, if you specify the {@link 98 android.Manifest.permission#VIBRATE} permission for the wearable app, you must also add that 99 permission to the mobile app.</li> 100 <li>Ensure that both the wearable and mobile APKs have the same package name and version 101 number.</li> 102 <li>Copy the signed wearable app to your handheld project's <code>res/raw</code> directory. We'll 103 refer to the APK as <code>wearable_app.apk</code>.</li> 104 <li>Create a <code>res/xml/wearable_app_desc.xml</code> file that contains the version and 105 path information of the wearable app. For example: 106<pre> 107<wearableApp package="wearable.app.package.name"> 108 <versionCode>1</versionCode> 109 <versionName>1.0</versionName> 110 <rawPathResId>wearable_app</rawPathResId> <!-- Do not include the .apk extension --> 111</wearableApp> 112</pre> 113 114<p> 115The <code>package</code>, <code>versionCode</code>, and <code>versionName</code> are the 116same values specified in the wearable app's <code>AndroidManifest.xml</code> file. 117The <code>rawPathResId</code> is the static variable name of the APK resource. For example, 118for <code>wearable_app.apk</code>, the static variable name is <code>wearable_app</code>. 119</p> 120</li> 121<li> 122Add a <code>meta-data</code> tag to your handheld app's <code><application></code> tag to 123reference the <code>wearable_app_desc.xml</code> file. 124<pre> 125 <meta-data android:name="com.google.android.wearable.beta.app" 126 android:resource="@xml/wearable_app_desc"/> 127</pre> 128</li> 129<li>Build and sign the handheld app.</li> 130</ol> 131 132<h2 id="AssetCompression">Turn off Asset Compression</h2> 133<p>Many build tools automatically compress any files added to the <code>res/raw</code> 134directory of an Android app. Because the wearable APK is already zipped, these tools re-compress the 135wearable APK and the wearable app installer can no longer read the wearable app. 136</p> 137 138<p>When this happens, the installation fails. On the handheld app, the <code>PackageUpdateService</code> 139logs the following error: "this file cannot be opened as a file descriptor; it is probably compressed." 140</p> 141 142<p>Android Studio doesn't compress your APK by default, but if you are using another build process, 143ensure that you don't doubly compress the wearable app.</p> 144