blob: 552d5fae27fda16397f9934f7d0d3c61c64acce6 [file] [log] [blame]
page.title=Running Your App
parent.title=Building Your First App
parent.link=index.html
trainingnavtop=true
previous.title=Creating a Project
previous.link=creating-project.html
next.title=Building a Simple User Interface
next.link=building-ui.html
@jd:body
<!-- This is the training bar -->
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<ol>
<li><a href="#RealDevice">Run on a Real Device</a></li>
<li><a href="#Emulator">Run on the Emulator</a></li>
</ol>
<h2>You should also read</h2>
<ul>
<li><a href="{@docRoot}tools/device.html">Using Hardware Devices</a></li>
<li><a href="{@docRoot}tools/devices/index.html">Managing Virtual Devices</a></li>
<li><a href="{@docRoot}tools/projects/index.html">Managing Projects</a></li>
</ul>
</div>
</div>
<p>If you followed the <a href="creating-project.html">previous lesson</a> to create an
Android project, it includes a default set of "Hello World" source files that allow you to
immediately run the app.</p>
<p>How you run your app depends on two things: whether you have a real Android-powered device and
whether youre using Eclipse. This lesson shows you how to install and run your app on a
real device and on the Android emulator, and in both cases with either Eclipse or the command line
tools.</p>
<p>Before you run your app, you should be aware of a few directories and files in the Android
project:</p>
<dl>
<dt><code>AndroidManifest.xml</code></dt>
<dd>The <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest file</a> describes
the fundamental characteristics of the app and defines each of
its components. You'll learn about various declarations in this file as you read more training
classes.</dd>
<dt><code>src/</code></dt>
<dd>Directory for your app's main source files. By default, it includes an {@link
android.app.Activity} class that runs when your app is launched using the app icon.</dd>
<dt><code>res/</code></dt>
<dd>Contains several sub-directories for <a
href="{@docRoot}guide/topics/resources/overview.html">app resources</a>. Here are just a few:
<dl style="margin-top:1em">
<dt><code>drawable-hdpi/</code></dt>
<dd>Directory for drawable objects (such as bitmaps) that are designed for high-density
(hdpi) screens. Other drawable directories contain assets designed for other screen densities.</dd>
<dt><code>layout/</code></dt>
<dd>Directory for files that define your app's user interface.</dd>
<dt><code>values/</code></dt>
<dd>Directory for other various XML files that contain a collection of resources, such as
string and color definitions.</dd>
</dl>
</dd>
</dl>
<p>When you build and run the default Android app, the default {@link android.app.Activity}
class starts and loads a layout file
that says "Hello World." The result is nothing exciting, but it's
important that you understand how to run your app before you start developing.</p>
<h2 id="RealDevice">Run on a Real Device</h2>
<p>If you have a real Android-powered device, here's how you can install and run your app:</p>
<ol>
<li>Plug in your device to your development machine with a USB cable.
If you’re developing on Windows, you might need to install the appropriate USB driver for your
device. For help installing drivers, see the <a href="{@docRoot}tools/extras/oem-usb.html">OEM USB
Drivers</a> document.</li>
<li>Ensure that <strong>USB debugging</strong> is enabled in the device Settings (open Settings
and navitage to <strong>Applications > Development</strong> on most devices, or click
<strong>Developer options</strong> on Android 4.0 and higher).</li>
</ol>
<p>To run the app from Eclipse, open one of your project's files and click
Run <img src="{@docRoot}images/tools/eclipse-run.png" style="vertical-align:baseline;margin:0" />
from the toolbar. Eclipse installs the app on your connected device and starts
it.</p>
<p>Or to run your app from a command line:</p>
<ol>
<li>Change directories to the root of your Android project and execute:
<pre class="no-pretty-print">ant debug</pre></li>
<li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
<code>PATH</code> environment variable, then execute:
<pre class="no-pretty-print">adb install bin/MyFirstApp-debug.apk</pre></li>
<li>On your device, locate <em>MyFirstActivity</em> and open it.</li>
</ol>
<p>That's how you build and run your Android app on a device!
To start developing, continue to the <a href="building-ui.html">next
lesson</a>.</p>
<h2 id="Emulator">Run on the Emulator</h2>
<p>Whether you’re using Eclipse or the command line, to run your app on the emulator you need to
first create an <a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD). An
AVD is a device configuration for the Android emulator that allows you to model different
devices.</p>
<div class="figure" style="width:457px">
<img src="{@docRoot}images/screens_support/avds-config.png" alt="" />
<p class="img-caption"><strong>Figure 1.</strong> The AVD Manager showing a few virtual
devices.</p>
</div>
<p>To create an AVD:</p>
<ol>
<li>Launch the Android Virtual Device Manager:
<ol type="a">
<li>In Eclipse, AVD Manager <img src="{@docRoot}images/tools/avd_manager.png"
style="vertical-align:baseline;margin:0" /> in the toolbar.</li>
<li>From the command line, change
directories to <code>&lt;sdk>/tools/</code> and execute:
<pre class="no-pretty-print">android avd</pre></li>
</ol>
</li>
<li>In the <em>Android Virtual Device Manager</em> panel, click <strong>New</strong>.</li>
<li>Fill in the details for the AVD.
Give it a name, a platform target, an SD card size, and a skin (HVGA is default).</li>
<li>Click <strong>Create AVD</strong>.</li>
<li>Select the new AVD from the <em>Android Virtual Device Manager</em> and click
<strong>Start</strong>.</li>
<li>After the emulator boots up, unlock the emulator screen.</li>
</ol>
<p>To run the app from Eclipse, open one of your project's files and click
Run <img src="{@docRoot}images/tools/eclipse-run.png" style="vertical-align:baseline;margin:0" />
from the toolbar. Eclipse installs the app on your AVD and starts it.</p>
<p>Or to run your app from the command line:</p>
<ol>
<li>Change directories to the root of your Android project and execute:
<pre class="no-pretty-print">ant debug</pre></li>
<li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
<code>PATH</code> environment
variable, then execute:
<pre class="no-pretty-print">adb install bin/MyFirstApp-debug.apk</pre></li>
<li>On the emulator, locate <em>MyFirstActivity</em> and open it.</li>
</ol>
<p>That's how you build and run your Android app on the emulator!
To start developing, continue to the <a href="building-ui.html">next
lesson</a>.</p>