• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<p>Open a command-line and navigate to the root of your project directory.
87  Use Gradle to build your project in debug mode, invoke the <code>assembleDebug</code> build task
88  using the Gradle wrapper script (<code>gradlew assembleRelease</code>).
89
90  <p>This creates your debug <code>.apk</code> file inside the module <code>build/</code>
91  directory, named <code>MyFirstApp-debug.apk</code>.  </p>
92
93  <p>On Windows platforms, type this command:</p>
94
95<pre>
96> gradlew.bat assembleDebug
97</pre>
98
99<p>On Mac OS and Linux platforms, type these commands:</p>
100
101<pre>
102$ chmod +x gradlew
103$ ./gradlew assembleDebug
104</pre>
105
106  <p>After you build the project, the output APK for the app module is located in
107  <code>app/build/outputs/apk/</code>
108
109  <p class="note"><strong>Note:</strong> The first command (<code>chmod</code>) adds the execution
110  permission to the Gradle wrapper script and is only necessary the first time you build this
111  project from the command line.</p>
112
113  <p>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
114  <code>PATH</code> environment variable, then execute:
115  <pre class="no-pretty-print">adb install app/build/outputs/MyFirstApp-debug.apk</pre><p>
116  <p>On your device, locate <em>MyFirstApp</em> and open it.</p>
117
118<p>That's how you build and run your Android app on a device!
119  To start developing, continue to the <a href="building-ui.html">next
120lesson</a>.</p>
121
122
123
124<h2 id="Emulator">Run on the Emulator</h2>
125
126<p>Whether you're using Android Studio or the command line, to run your app on the emulator you need
127to first create an <a href="{@docRoot}tools/devices/index.html">Android Virtual Device</a> (AVD). An
128AVD is a device configuration for the Android emulator that allows you to model a specific
129device.</p>
130
131
132<h3>Create an AVD</h3>
133<ol>
134  <li>Launch the Android Virtual Device Manager:
135    <ul>
136      <li>In Android Studio, select <strong>Tools &gt; Android &gt; AVD Manager</strong>, or click
137  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>
138      <li>Or, from the command line, change directories to
139      <code>sdk/</code> and execute:
140        <pre class="no-pretty-print">tools/android avd</pre>
141        <p class="note"><strong>Note:</strong> The AVD Manager that appears
142        when launched from the command line is different from the version in
143        Android Studio, so the following instructions may not all apply.</p>
144        </li>
145    </ul>
146
147  <img src="{@docRoot}images/studio-avdmgr-firstscreen.png" alt=""
148  style="margin-top:1em">
149  <p class="img-caption"><strong>Figure 1.</strong> The AVD Manager main screen shows your current virtual devices.</p>
150
151  </li>
152  <li>On the AVD Manager main screen (figure 1), click <strong>Create Virtual Device</strong>.</li>
153  <li>In the Select Hardware window, select a device configuration, such as Nexus 6,
154  then click <strong>Next</strong>.
155  </li>
156  <li>Select the desired system version for the AVD and click <strong>Next</strong>.
157  </li>
158  <li>Verify the configuration settings, then click <strong>Finish</strong>.
159  </li>
160</ol>
161
162<p>For more information about using AVDs, see
163<a href="{@docRoot}tools/devices/managing-avds.html">Managing AVDs with AVD Manager</a>.</p>
164
165<h3>Run the app from Android Studio</h3>
166<ol>
167  <li>In <strong>Android Studio</strong>, select your project and click <strong>Run</strong>
168    <img src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> from the toolbar.</li>
169  <li>In the <strong>Choose Device</strong> window, click the <strong>Launch emulator</strong> radio
170    button.</li>
171  <li>From the <strong>Android virtual device</strong> pull-down menu, select the emulator
172    you created, and click <strong>OK</strong>.</li>
173</ol>
174<p>It can take a few minutes for the emulator to load itself. You may have to unlock the screen.
175When you do, <em>My First App</em> appears on the emulator screen.</p>
176
177
178<h3>Run your app from the command line</h3>
179 <ol>
180  <li>Build the project from the command line. The output APK for the app module is located in
181  <code>app/build/outputs/apk/</code>.</li>
182  <li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
183  <code>PATH</code> environment variable.</li>
184  <li>Execute this command:
185   <p>
186   <pre class="no-pretty-print">adb install app/build/outputs/MyFirstApp-debug.apk</pre>
187   </p>
188  </li>
189  <li>On the emulator, locate <em>MyFirstApp</em> and open it.</li>
190 </ol>
191
192
193  <p>That's how you build and run your Android app on the emulator!
194  To start developing, continue to the <a href="building-ui.html">next
195  lesson</a>.</p>
196
197
198