• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Running Your App
2parent.title=Building Your First App
3parent.link=index.html
4
5trainingnavtop=true
6previous.title=Creating a Project
7previous.link=creating-project.html
8next.title=Building a Simple User Interface
9next.link=building-ui.html
10
11@jd:body
12
13
14<!-- This is the training bar -->
15<div id="tb-wrapper">
16<div id="tb">
17
18<h2>This lesson teaches you to</h2>
19
20<ol>
21  <li><a href="#RealDevice">Run on a Real Device</a></li>
22  <li><a href="#Emulator">Run on the Emulator</a></li>
23</ol>
24
25<h2>You should also read</h2>
26
27<ul>
28  <li><a href="{@docRoot}tools/device.html">Using Hardware Devices</a></li>
29  <li><a href="{@docRoot}tools/devices/index.html">Managing Virtual Devices</a></li>
30  <li><a href="{@docRoot}tools/projects/index.html">Managing Projects</a></li>
31</ul>
32
33
34</div>
35</div>
36
37
38<p>If you followed the <a href="creating-project.html">previous lesson</a> to create an
39Android project, it includes a default set of "Hello World" source files that allow you to
40run the app right away.</p>
41
42<p>How you run your app depends on two things: whether you have a real Android-powered device and
43whether you’re using Eclipse. This lesson shows you how to install and run your app on a
44real device and on the Android emulator, and in both cases with either Eclipse or the command line
45tools.</p>
46
47<p>Before you run your app, you should be aware of a few directories and files in the Android
48project:</p>
49
50<dl>
51  <dt><code>AndroidManifest.xml</code></dt>
52  <dd>This manifest file describes the fundamental characteristics of the app and defines each of
53its components. You'll learn about various declarations in this file as you read more training
54classes.</dd>
55  <dt><code>src/</code></dt>
56  <dd>Directory for your app's main source files. By default, it includes an {@link
57android.app.Activity} class that runs when your app is launched using the app icon.</dd>
58  <dt><code>res/</code></dt>
59  <dd>Contains several sub-directories for app resources. Here are just a few:
60    <dl style="margin-top:1em">
61      <dt><code>drawable-hdpi/</code></dt>
62        <dd>Directory for drawable objects (such as bitmaps) that are designed for high-density
63(hdpi) screens. Other drawable directories contain assets designed for other screen densities.</dd>
64      <dt><code>layout/</code></dt>
65        <dd>Directory for files that define your app's user interface.</dd>
66      <dt><code>values/</code></dt>
67        <dd>Directory for other various XML files that contain a collection of resources, such as
68string and color definitions.</dd>
69    </dl>
70  </dd>
71</dl>
72
73<p>When you build and run the default Android project, the default {@link android.app.Activity}
74class in the <code>src/</code> directory starts and loads a layout file from the
75<code>layout/</code> directory, which includes a "Hello World" message. Not real exciting, but it's
76important that you understand how to build and run your app before adding real functionality to
77the app.</p>
78
79
80
81<h2 id="RealDevice">Run on a Real Device</h2>
82
83<p>Whether you’re using Eclipse or the command line, you need to:</p>
84
85<ol>
86  <li>Plug in your Android-powered device to your machine with a USB cable.
87If you’re developing on Windows, you might need to install the appropriate USB driver for your
88device. For help installing drivers, see the <a href="{@docRoot}tools/extras/oem-usb.html">OEM USB
89Drivers</a> document.</li>
90  <li>Ensure that <strong>USB debugging</strong> is enabled in the device Settings (open Settings
91and navitage to <strong>Applications > Development</strong> on most devices, or select
92<strong>Developer options</strong> on Android 4.0 and higher).</li>
93</ol>
94
95<p>To run the app from Eclipse, open one of your project's files and click
96<strong>Run</strong> from the toolbar. Eclipse installs the app on your connected device and starts
97it.</p>
98
99
100<p>Or to run your app from a command line:</p>
101
102<ol>
103  <li>Change directories to the root of your Android project and execute:
104<pre class="no-pretty-print">ant debug</pre></li>
105  <li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
106<code>PATH</code> environment variable, then execute:
107<pre class="no-pretty-print">adb install bin/MyFirstApp-debug.apk</pre></li>
108  <li>On your device, locate <em>MyFirstActivity</em> and open it.</li>
109</ol>
110
111<p>To start adding stuff to the app, continue to the <a href="building-ui.html">next
112lesson</a>.</p>
113
114
115
116<h2 id="Emulator">Run on the Emulator</h2>
117
118<p>Whether you’re using Eclipse or the command line, you need to first create an <a
119href="{@docRoot}tools/devices/index.html">Android Virtual
120Device</a> (AVD). An AVD is a
121device configuration for the Android emulator that allows you to model
122different device configurations.</p>
123
124<div class="figure" style="width:457px">
125  <img src="{@docRoot}images/screens_support/avds-config.png" alt="" />
126  <p class="img-caption"><strong>Figure 1.</strong> The AVD Manager showing a few virtual
127devices.</p>
128</div>
129
130<p>To create an AVD:</p>
131<ol>
132  <li>Launch the Android Virtual Device Manager:
133    <ol type="a">
134      <li>In Eclipse, select <strong>Window > AVD Manager</strong>, or click the <em>AVD
135Manager</em> icon in the Eclipse toolbar.</li>
136      <li>From the command line, change directories to <code>&lt;sdk>/tools/</code> and execute:
137<pre class="no-pretty-print">./android avd</pre></li>
138    </ol>
139  </li>
140  <li>In the <em>Android Virtual Device Device Manager</em> panel, click <strong>New</strong>.</li>
141  <li>Fill in the details for the AVD.
142Give it a name, a platform target, an SD card size, and a skin (HVGA is default).</li>
143  <li>Click <strong>Create AVD</strong>.</li>
144  <li>Select the new AVD from the <em>Android Virtual Device Manager</em> and click
145<strong>Start</strong>.</li>
146  <li>After the emulator boots up, unlock the emulator screen.</li>
147</ol>
148
149<p>To run the app from Eclipse, open one of your project's files and click
150<strong>Run</strong> from the toolbar. Eclipse installs the app on your AVD and starts it.</p>
151
152
153<p>Or to run your app from the command line:</p>
154
155<ol>
156  <li>Change directories to the root of your Android project and execute:
157<pre class="no-pretty-print">ant debug</pre></li>
158  <li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
159<code>PATH</code> environment
160variable, then execute:
161<pre class="no-pretty-print">adb install bin/MyFirstApp-debug.apk</pre></li>
162  <li>On the emulator, locate <em>MyFirstActivity</em> and open it.</li>
163</ol>
164
165
166<p>To start adding stuff to the app, continue to the <a href="building-ui.html">next
167lesson</a>.</p>
168
169
170
171
172
173
174
175
176
177
178
179