1page.title=Running Your App 2parent.title=Building Your First App 3parent.link=index.html 4 5trainingnavtop=true 6 7page.tags=emulator 8helpoutsWidget=true 9 10@jd:body 11 12 13<!-- This is the training bar --> 14<div id="tb-wrapper"> 15<div id="tb"> 16 17<h2>This lesson teaches you to</h2> 18 19<ol> 20 <li><a href="#RealDevice">Run on a Real Device</a></li> 21 <li><a href="#Emulator">Run on the Emulator</a></li> 22</ol> 23 24<h2>You should also read</h2> 25 26<ul> 27 <li><a href="{@docRoot}tools/device.html">Using Hardware Devices</a></li> 28 <li><a href="{@docRoot}tools/devices/managing-avds.html">Managing AVDs with AVD Manager</a></li> 29 <li><a href="{@docRoot}tools/projects/index.html">Managing Projects</a></li> 30</ul> 31 32 33</div> 34</div> 35 36 37<p>If you followed the <a href="creating-project.html">previous lesson</a> to create an 38Android project, it includes a default set of "Hello World" source files that allow you to 39immediately run the app.</p> 40 41<p>How you run your app depends on two things: whether you have a real device running Android and 42whether you're using Android Studio. This lesson shows you how to install and run your app on a 43real device and on the Android emulator, and in both cases with either Android Studio or the command 44line tools.</p> 45 46<h2 id="RealDevice">Run on a Real Device</h2> 47 48<p>If you have a device running Android, here's how to install and run your app.</p> 49 50<h3>Set up your device</h3> 51 52<ol> 53 <li>Plug in your device to your development machine with a USB cable. 54 If you're developing on Windows, you might need to install the appropriate USB driver for your 55 device. For help installing drivers, see the <a href="{@docRoot}tools/extras/oem-usb.html">OEM 56 USB Drivers</a> document.</li> 57 <li>Enable <strong>USB debugging</strong> on your device. 58 <ul> 59 <li>On most devices running Android 3.2 or older, you can find the option under 60 <strong>Settings > Applications > Development</strong>.</li> 61 <li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>. 62 <p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer 63 options</strong> is hidden by default. To make it available, go 64 to <strong>Settings > About phone</strong> and tap <strong>Build number</strong> 65 seven times. Return to the previous screen to find <strong>Developer options</strong>.</p> 66 </li> 67 </ul> 68 </li> 69</ol> 70 71<h3>Run the app from Android Studio</h3> 72<ol> 73 <li>Select one of your project's files and click 74<strong>Run</strong> <img 75src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> 76from the toolbar.</li> 77 <li>In the <strong>Choose Device</strong> window that appears, select the 78 <strong>Choose a running device</strong> radio button, select your device, and click <strong>OK 79 </strong>.</li> 80</ol> 81<p>Android Studio installs the app on your connected device and starts it.</p> 82 83 84<h3>Run the app from a command line</h3> 85 86<ol> 87 <li>Change directories to the root of your Android project and execute: 88<pre class="no-pretty-print">ant debug</pre></li> 89 <li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your 90<code>PATH</code> environment variable, then execute: 91<pre class="no-pretty-print">adb install bin/MyFirstApp-debug.apk</pre></li> 92 <li>On your device, locate <em>MyFirstApp</em> and open it.</li> 93</ol> 94 95<p>That's how you build and run your Android app on a device! 96 To start developing, continue to the <a href="building-ui.html">next 97lesson</a>.</p> 98 99 100 101<h2 id="Emulator">Run on the Emulator</h2> 102 103<p>Whether you're using Android Studio or the command line, to run your app on the emulator you need 104to first create an <a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD). An 105AVD is a device configuration for the Android emulator that allows you to model a specific 106device.</p> 107 108 109<h3>Create an AVD</h3> 110<ol> 111 <li>Launch the Android Virtual Device Manager: 112 <ul> 113 <li>In Android Studio, select <strong>Tools > Android > AVD Manager</strong>, or click 114 the AVD Manager icon <img src="{@docRoot}images/tools/avd-manager-studio.png" style="vertical-align:bottom;margin:0;height:19px"> in the toolbar.</li> 115 <li>Or, from the command line, change directories to 116 <code><sdk>/tools/</code> and execute: 117 <pre class="no-pretty-print">android avd</pre> 118 <p class="note"><strong>Note:</strong> The AVD Manager that appears 119 when launched from the command line is different from the version in 120 Android Studio, so the following instructions may not apply.</p> 121 </li> 122 </ul> 123 124 <img src="{@docRoot}images/studio-avdmgr-firstscreen.png" alt="" 125 style="margin-top:1em"> 126 <p class="img-caption"><strong>Figure 1.</strong> The AVD Manager main screen shows your current virtual devices.</p> 127 128 </li> 129 <li>On the AVD Manager main screen (figure 1), click <strong>Create Virtual Device</strong>.</li> 130 131 <li>In the Select Hardware window, select a device configuration, such as Nexus 6, 132 then click <strong>Next</strong>. 133 </li> 134 <li>Select the desired system version for the AVD and click <strong>Next</strong>. 135 </li> 136 <li>Verify the configuration settings, then click <strong>Finish</strong>. 137 </li> 138</ol> 139 140<p>For more information about using AVDs, see 141<a href="{@docRoot}tools/devices/managing-avds.html">Managing AVDs with AVD Manager</a>.</p> 142 143<h3>Run the app from Android Studio</h3> 144<ol> 145 <li>In <strong>Android Studio</strong>, select your project and click <strong>Run</strong> 146 <img src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> 147 from the toolbar.</li> 148 <li>In the <strong>Choose Device</strong> window, click the <strong>Launch emulator</strong> radio 149 button.</li> 150 <li>From the <strong>Android virtual device</strong> pull-down menu, select the emulator 151 you created, and click <strong>OK</strong>.</li> 152</ol> 153<p>It can take a few minutes for the emulator to load itself. You may have to unlock the screen. 154 When you do, My First App appears on the emulator screen.</p> 155 156 157<h3>Run your app from the command line</h3> 158 159<ol> 160 <li>Change directories to the root of your Android project and execute: 161 <pre class="no-pretty-print">ant debug</pre></li> 162 <li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your 163 <code>PATH</code> environment variable, then execute: 164 <pre class="no-pretty-print">adb install bin/MyFirstApp-debug.apk</pre></li> 165 <li>On the emulator, locate <em>MyFirstApp</em> and open it.</li> 166</ol> 167 168 169<p>That's how you build and run your Android app on the emulator! 170 To start developing, continue to the <a href="building-ui.html">next 171lesson</a>.</p> 172 173 174 175 176 177 178 179 180 181 182 183