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