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