198 lines
7.5 KiB
Plaintext
198 lines
7.5 KiB
Plaintext
page.title=Running Your App
|
|
parent.title=Building Your First App
|
|
parent.link=index.html
|
|
|
|
trainingnavtop=true
|
|
|
|
page.tags=emulator
|
|
helpoutsWidget=true
|
|
|
|
@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/managing-avds.html">Managing AVDs with AVD Manager</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 device running Android and
|
|
whether you're using Android Studio. 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 Android Studio or the command
|
|
line tools.</p>
|
|
|
|
<h2 id="RealDevice">Run on a Real Device</h2>
|
|
|
|
<p>If you have a device running Android, here's how to install and run your app.</p>
|
|
|
|
<h3>Set up your device</h3>
|
|
|
|
<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>Enable <strong>USB debugging</strong> on your device.
|
|
<ul>
|
|
<li>On most devices running Android 3.2 or older, you can find the option under
|
|
<strong>Settings > Applications > Development</strong>.</li>
|
|
<li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>.
|
|
<p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer
|
|
options</strong> is hidden by default. To make it available, go
|
|
to <strong>Settings > About phone</strong> and tap <strong>Build number</strong>
|
|
seven times. Return to the previous screen to find <strong>Developer options</strong>.</p>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ol>
|
|
|
|
<h3>Run the app from Android Studio</h3>
|
|
<ol>
|
|
<li>Select one of your project's files and click
|
|
<strong>Run</strong> <img
|
|
src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" />
|
|
from the toolbar.</li>
|
|
<li>In the <strong>Choose Device</strong> window that appears, select the
|
|
<strong>Choose a running device</strong> radio button, select your device, and click <strong>OK
|
|
</strong>.</li>
|
|
</ol>
|
|
<p>Android Studio installs the app on your connected device and starts it.</p>
|
|
|
|
|
|
<h3>Run the app from a command line</h3>
|
|
|
|
<p>Open a command-line and navigate to the root of your project directory.
|
|
Use Gradle to build your project in debug mode, invoke the <code>assembleDebug</code> build task
|
|
using the Gradle wrapper script (<code>gradlew assembleRelease</code>).
|
|
|
|
<p>This creates your debug <code>.apk</code> file inside the module <code>build/</code>
|
|
directory, named <code>MyFirstApp-debug.apk</code>. </p>
|
|
|
|
<p>On Windows platforms, type this command:</p>
|
|
|
|
<pre>
|
|
> gradlew.bat assembleDebug
|
|
</pre>
|
|
|
|
<p>On Mac OS and Linux platforms, type these commands:</p>
|
|
|
|
<pre>
|
|
$ chmod +x gradlew
|
|
$ ./gradlew assembleDebug
|
|
</pre>
|
|
|
|
<p>After you build the project, the output APK for the app module is located in
|
|
<code>app/build/outputs/apk/</code>
|
|
|
|
<p class="note"><strong>Note:</strong> The first command (<code>chmod</code>) adds the execution
|
|
permission to the Gradle wrapper script and is only necessary the first time you build this
|
|
project from the command line.</p>
|
|
|
|
<p>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 app/build/outputs/MyFirstApp-debug.apk</pre><p>
|
|
<p>On your device, locate <em>MyFirstApp</em> and open it.</p>
|
|
|
|
<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 Android Studio 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 a specific
|
|
device.</p>
|
|
|
|
|
|
<h3>Create an AVD</h3>
|
|
<ol>
|
|
<li>Launch the Android Virtual Device Manager:
|
|
<ul>
|
|
<li>In Android Studio, select <strong>Tools > Android > AVD Manager</strong>, or click
|
|
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>
|
|
<li>Or, from the command line, change directories to
|
|
<code>sdk/</code> and execute:
|
|
<pre class="no-pretty-print">tools/android avd</pre>
|
|
<p class="note"><strong>Note:</strong> The AVD Manager that appears
|
|
when launched from the command line is different from the version in
|
|
Android Studio, so the following instructions may not all apply.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<img src="{@docRoot}images/studio-avdmgr-firstscreen.png" alt=""
|
|
style="margin-top:1em">
|
|
<p class="img-caption"><strong>Figure 1.</strong> The AVD Manager main screen shows your current virtual devices.</p>
|
|
|
|
</li>
|
|
<li>On the AVD Manager main screen (figure 1), click <strong>Create Virtual Device</strong>.</li>
|
|
<li>In the Select Hardware window, select a device configuration, such as Nexus 6,
|
|
then click <strong>Next</strong>.
|
|
</li>
|
|
<li>Select the desired system version for the AVD and click <strong>Next</strong>.
|
|
</li>
|
|
<li>Verify the configuration settings, then click <strong>Finish</strong>.
|
|
</li>
|
|
</ol>
|
|
|
|
<p>For more information about using AVDs, see
|
|
<a href="{@docRoot}tools/devices/managing-avds.html">Managing AVDs with AVD Manager</a>.</p>
|
|
|
|
<h3>Run the app from Android Studio</h3>
|
|
<ol>
|
|
<li>In <strong>Android Studio</strong>, select your project and click <strong>Run</strong>
|
|
<img src="{@docRoot}images/tools/as-run.png" style="vertical-align:baseline;margin:0; max-height:1em" /> from the toolbar.</li>
|
|
<li>In the <strong>Choose Device</strong> window, click the <strong>Launch emulator</strong> radio
|
|
button.</li>
|
|
<li>From the <strong>Android virtual device</strong> pull-down menu, select the emulator
|
|
you created, and click <strong>OK</strong>.</li>
|
|
</ol>
|
|
<p>It can take a few minutes for the emulator to load itself. You may have to unlock the screen.
|
|
When you do, <em>My First App</em> appears on the emulator screen.</p>
|
|
|
|
|
|
<h3>Run your app from the command line</h3>
|
|
<ol>
|
|
<li>Build the project from the command line. The output APK for the app module is located in
|
|
<code>app/build/outputs/apk/</code>.</li>
|
|
<li>Make sure the Android SDK <code>platform-tools/</code> directory is included in your
|
|
<code>PATH</code> environment variable.</li>
|
|
<li>Execute this command:
|
|
<p>
|
|
<pre class="no-pretty-print">adb install app/build/outputs/MyFirstApp-debug.apk</pre>
|
|
</p>
|
|
</li>
|
|
<li>On the emulator, locate <em>MyFirstApp</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>
|
|
|
|
|