blob: a5ab07dbaaed019f9dce347a21b127fd1b936559 [file] [log] [blame]
Scott Main300cd262011-02-08 15:04:42 -08001page.title=Building and Running from the Command Line
Robert Ly70628c72010-12-22 10:59:38 -08002@jd:body
3
4 <div id="qv-wrapper">
5 <div id="qv">
6 <h2>In this document</h2>
7
8 <ol>
9 <li><a href="#DebugMode">Building in debug mode</a></li>
10
11 <li><a href="#ReleaseMode">Building in release mode</a></li>
12
13 <li><a href="#RunningOnEmulator">Running on an emulator</a></li>
14
15 <li><a href="#RunningOnDevice">Running on a device</a></li>
16 </ol>
17 </div>
18 </div>
19
20 <p>There are two ways to build your application using the Ant build script: one for
21 testing/debugging your application &mdash; <em>debug mode</em> &mdash; and one for building your
22 final package for release &mdash; <em>release mode</em>. Regardless of which way you build your application,
23 it must be signed before it can install on an emulator or device&mdash;with a debug key when building
24 in debug mode and with your own private key when building in release mode.</p>
25
26 <p>Whether you're building in debug mode or release mode, you need to use the Ant tool to compile
27 and build your project. This will create the .apk file that you can install on an emulator or device.
28 When you build in debug mode, the .apk file is automatically signed by the SDK tools with
29 a debug key, so it's instantly ready for installation onto an emulator or attached
30 development device. You cannot distribute an application that is signed with a debug key.
31 When you build in release mode, the .apk file is <em>unsigned</em>, so you
32 must manually sign it with your own private key, using Keytool and Jarsigner.</p>
33
34 <p>It's important that you read and understand <a href=
35 "{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>, particularly once
36 you're ready to release your application and share it with end-users. That document describes the
37 procedure for generating a private key and then using it to sign your .apk file. If you're just
38 getting started, however, you can quickly run your applications on an emulator or your own
39 development device by building in debug mode.</p>
40
41 <p>If you don't have Ant, you can obtain it from the <a href="http://ant.apache.org/">Apache Ant
42 home page</a>. Install it and make sure it is in your executable PATH. Before calling Ant, you
43 need to declare the JAVA_HOME environment variable to specify the path to where the JDK is
44 installed.</p>
45
46 <p class="note"><strong>Note:</strong> When installing JDK on Windows, the default is to install
47 in the "Program Files" directory. This location will cause <code>ant</code> to fail, because of
48 the space. To fix the problem, you can specify the JAVA_HOME variable like this:
Scott Main300cd262011-02-08 15:04:42 -080049 <pre>set JAVA_HOME=c:\Progra~1\Java\&lt;jdkdir&gt;</pre>
Robert Ly70628c72010-12-22 10:59:38 -080050
Scott Main300cd262011-02-08 15:04:42 -080051 <p>The easiest solution, however, is to install JDK in a non-space directory, for example:</p>
Robert Ly70628c72010-12-22 10:59:38 -080052
53 <pre>c:\java\jdk1.6.0_02</pre>
54
55 <h2 id="DebugMode">Building in debug mode</h2>
56
57 <p>For immediate application testing and debugging, you can build your application in debug mode
58 and immediately install it on an emulator. In debug mode, the build tools automatically sign your
59 application with a debug key and optimize the package with {@code zipalign}.</p>
60
61 <p>To build in debug mode:</p>
62
63 <ol>
64 <li>Open a command-line and navigate to the root of your project directory.</li>
65 <li>Use Ant to compile your project in debug mode:
66 <pre>
67ant debug
68</pre>
69
70 <p>This creates your debug <code>.apk</code> file inside the project <code>bin/</code> directory, named
71 <code>&lt;your_project_name&gt;-debug.apk</code>. The file is already signed with
72 the debug key and has been aligned with
73 <a href="{@docRoot}/guide/developing/tools/zipalign.html"><code>zipalign</code></a>.
74 </p>
75 </li>
76 </ol>
77
78 <p>Each time you change a source file or resource, you must run Ant again in order to package up
79 the latest version of the application.</p>
80
81 <p>To install and run your application on an emulator, see the following section about <a href=
82 "#RunningOnEmulator">Running on the Emulator</a>.</p>
83
84 <h2 id="ReleaseMode">Building in release mode</h2>
85
86 <p>When you're ready to release and distribute your application to end-users, you must build your
87 application in release mode. Once you have built in release mode, it's a good idea to perform
88 additional testing and debugging with the final .apk.</p>
89
90 <p>Before you start building your application in release mode, be aware that you must sign the
91 resulting application package with your private key, and should then align it using the {@code
92 zipalign} tool. There are two approaches to building in release mode: build an unsigned package
93 in release mode and then manually sign and align the package, or allow the build script to sign
94 and align the package for you.</p>
95
96 <h3 id="ManualReleaseMode">Build unsigned</h3>
97
98 <p>If you build your application <em>unsigned</em>, then you will need to manually sign and align
99 the package.</p>
100
101 <p>To build an <em>unsigned</em> .apk in release mode:</p>
102
103 <ol>
104 <li>Open a command-line and navigate to the root of your project directory.</li>
105
106 <li>Use Ant to compile your project in release mode:
107 <pre>
108ant release
109</pre>
110 </li>
111 </ol>
112
113 <p>This creates your Android application .apk file inside the project <code>bin/</code>
114 directory, named <code><em>&lt;your_project_name&gt;</em>-unsigned.apk</code>.</p>
115
116 <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point and can't
117 be installed until signed with your private key.</p>
118
119 <p>Once you have created the unsigned .apk, your next step is to sign the .apk with your private
120 key and then align it with {@code zipalign}. To complete this procedure, read <a href=
121 "{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>.</p>
122
123 <p>When your <code>.apk</code> has been signed and aligned, it's ready to be distributed to end-users.
124 You should test the final build on different devices or AVDs to ensure that it
125 runs properly on different platforms.</p>
126
127 <h3 id="AutoReleaseMode">Build signed and aligned</h3>
128
129 <p>If you would like, you can configure the Android build script to automatically sign and align
130 your application package. To do so, you must provide the path to your keystore and the name of
131 your key alias in your project's {@code build.properties} file. With this information provided,
132 the build script will prompt you for your keystore and alias password when you build in release
133 mode and produce your final application package, which will be ready for distribution.</p>
134
135 <p class="caution"><strong>Caution:</strong> Due to the way Ant handles input, the password that
136 you enter during the build process <strong>will be visible</strong>. If you are concerned about
137 your keystore and alias password being visible on screen, then you may prefer to perform the
138 application signing manually, via Jarsigner (or a similar tool). To instead perform the signing
139 procedure manually, <a href="#ManualReleaseMode">build unsigned</a> and then continue with
140 <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>.</p>
141
142 <p>To specify your keystore and alias, open the project {@code build.properties} file (found in
143 the root of the project directory) and add entries for {@code key.store} and {@code key.alias}.
144 For example:</p>
145 <pre>
146key.store=path/to/my.keystore
147key.alias=mykeystore
148</pre>
149
150 <p>Save your changes. Now you can build a <em>signed</em> .apk in release mode:</p>
151
152 <ol>
153 <li>Open a command-line and navigate to the root of your project directory.</li>
154
155 <li>Use Ant to compile your project in release mode:
156 <pre>
157ant release
158</pre>
159 </li>
160
161 <li>When prompted, enter you keystore and alias passwords.
162
163 <p class="caution"><strong>Caution:</strong> As described above, your password will be
164 visible on the screen.</p>
165 </li>
166 </ol>
167
168 <p>This creates your Android application .apk file inside the project <code>bin/</code>
169 directory, named <code><em>&lt;your_project_name&gt;</em>-release.apk</code>. This .apk file has
170 been signed with the private key specified in {@code build.properties} and aligned with {@code
171 zipalign}. It's ready for installation and distribution.</p>
172
173 <h3>Once built and signed in release mode</h3>
174
175 <p>Once you have signed your application with a private key, you can install and run it on an
176 <a href="#RunningOnEmulator">emulator</a> or <a href="#RunningOnDevice">device</a>. You can
177 also try installing it onto a device from a web server. Simply upload the signed .apk to a web
178 site, then load the .apk URL in your Android web browser to download the application and begin
179 installation. (On your device, be sure you have enabled
180 <em>Settings &gt; Applications &gt; Unknown sources</em>.)</p>
181
182 <h2 id="RunningOnEmulator">Running on the emulator</h2>
183
184 <p>Before you can run your application on the Android Emulator, you must <a href=
185 "{@docRoot}guide/developing/devices/managing-avds.html">create an AVD</a>.</p>
186
187 <p>To run your application:</p>
188
189 <ol>
190 <li>
191 <strong>Open the SDK and AVD Manager and launch a virtual device</strong>
192
193 <p>From your SDK's <code>platform-tools/</code> directory, execute the {@code android} tool with no
194 arguments:</p>
195 <pre>
196android
197</pre>
198
199 <p>In the <em>Virtual Devices</em> view, select an AVD and click <strong>Start</strong>.</p>
200 </li>
201
202 <li>
203 <strong>Install your application</strong>
204
205 <p>From your SDK's <code>tools/</code> directory, install the {@code .apk} on the
206 emulator:</p>
207 <pre>
208adb install <em>&lt;path_to_your_bin&gt;</em>.apk
209</pre>
210
211 <p>Your .apk file (signed with either a release or debug key) is in your project {@code bin/}
212 directory after you build your application.</p>
213
214 <p>If there is more than one emulator running, you must specify the emulator upon which to
215 install the application, by its serial number, with the <code>-s</code> option. For
216 example:</p>
217 <pre>
218adb -s emulator-5554 install <em>path/to/your/app</em>.apk
219</pre>
220
221 <p>To see a list of available device serial numbers, execute {@code adb devices}.</p>
222 </li>
223 </ol>
224
225 <p>If you don't see your application on the emulator, try closing the emulator and launching the
226 virtual device again from the SDK and AVD Manager. Sometimes when you install an application for the
227 first time, it won't show up in the application launcher or be accessible by other applications.
228 This is because the package manager usually examines manifests completely only on emulator
229 startup.</p>
230
231 <p>Be certain to create multiple AVDs upon which to test your application. You should have one
232 AVD for each platform and screen type with which your application is compatible. For instance, if
233 your application compiles against the Android 1.5 (API Level 3) platform, you should create an
234 AVD for each platform equal to and greater than 1.5 and an AVD for each <a href=
235 "{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test your
236 application on each one.</p>
237
238 <p class="note"><strong>Tip:</strong> If you have <em>only one</em> emulator running, you can
239 build your application and install it on the emulator in one simple step. Navigate to the root of
240 your project directory and use Ant to compile the project with <em>install mode</em>: <code>ant
241 install</code>. This will build your application, sign it with the debug key, and install it on
242 the currently running emulator.</p>
243
244 <h2 id="RunningOnDevice">Running on a device</h2>
245
246 <p>Before you can run your application on a device, you must perform some basic setup for your
247 device:</p>
248
249 <ul>
250 <li>Ensure that your application is debuggable by setting the
251 <code>android:debuggable</code> attribute of the <code>&lt;application&gt;</code>
252 element to <code>true</code>. As of ADT 8.0, this is done by default when you build in debug mode.</li>
253
254 <li>Enable USB Debugging on your device. You can find the setting on most Android devices by
255 going to <strong>Settings > Applications > Development > USB debugging</strong>.</li>
256
257 <li>Ensure that your development computer can detect your device when connected via USB</li>
258 </ul>
259
260 <p>Read <a href="{@docRoot}guide/developing/device.html#setting-up">Setting up a Device for
261 Development</a> for more information.</p>
262
263 <p>Once your device is set up and connected via USB, navigate to your SDK's <code>platform-tools/</code>
264 directory and install the <code>.apk</code> on the device:</p>
265 <pre>
266adb -d install <em>path/to/your/app</em>.apk
267</pre>
268
269 <p>The {@code -d} flag specifies that you want to use the attached device (in case you also have
270 an emulator running).</p>
271
272 <p>For more information on the tools used above, please see the following documents:</p>
273
274 <ul>
275 <li><a href="{@docRoot}guide/developing/tools/android.html">android Tool</a></li>
276
277 <li><a href="{@docRoot}guide/developing/devices/emulator.html">Android Emulator</a></li>
278
279 <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> (ADB)</li>
280 </ul>
281
282 <h2 id="Signing">Application Signing</h2>
283
284 <p>As you begin developing Android applications, understand that all Android applications must be
285 digitally signed before the system will install them on an emulator or device. There are two ways
286 to do this: with a <em>debug key</em> (for immediate testing on an emulator or development
287 device) or with a <em>private key</em> (for application distribution).</p>
288
289 <p>The Android build tools help you get started by automatically signing your .apk files with a
290 debug key at build time. This means that you can compile your application and install it on the
291 emulator without having to generate your own private key. However, please note that if you intend
292 to publish your application, you <strong>must</strong> sign the application with your own private
293 key, rather than the debug key generated by the SDK tools.</p>
294
295 <p>The ADT plugin helps you get started quickly by signing your .apk files with a debug key,
296 prior to installing them on an emulator or development device. This means that you can quickly
297 run your application from Eclipse without having to generate your own private key. No specific
298 action on your part is needed, provided ADT has access to Keytool.However, please note that if
299 you intend to publish your application, you <strong>must</strong> sign the application with your
300 own private key, rather than the debug key generated by the SDK tools.</p>
301
302 <p>Please read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your
303 Applications</a>, which provides a thorough guide to application signing on Android and what it
304 means to you as an Android application developer. The document also includes a guide to exporting
305 and signing your application with the ADT's Export Wizard.</p>