• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Building and Running from Eclipse with ADT
2parent.title=Building and Running
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7    <div id="qv">
8      <h2>In this document</h2>
9
10      <ol>
11        <li><a href="#RunningOnEmulatorEclipse">Running on an Emulator</a></li>
12
13        <li><a href="#RunningOnDeviceEclipse">Running on a Device</a></li>
14
15        <li><a href="#RunConfig">Creating a Run Configuration</a></li>
16      </ol>
17    </div>
18  </div>
19
20  <p>Eclipse and ADT provide an environment where most of the details of the build process are
21  hidden from you. By default, the build process constantly runs in the background as you make
22  changes to your project.</p>
23
24  <p>When Eclipse automatically builds your application, it enables debugging and signs the
25  <code>.apk</code> with a debug key, by default. When you run the application,
26  Eclipse invokes ADB and installs your application to a device or emulator, so you do not have to
27  manually perform these tasks. Since most of the build process is taken care of by Eclipse, the
28  following topics show you how to run an application, which will automatically build your
29  application as well.</p>
30
31  <p>To distribute your application, however, you must build your application in release mode and sign the
32  <code>.apk</code> file with your own private key.</p>
33
34   <p>This document shows you how to run your application on an emulator or a real device
35   from Eclipse&mdash;all of which is done using the debug version of your application.
36   For more information about how to sign your application with a private key for release, see <a href=
37  "{@docRoot}tools/publishing/app-signing.html#ExportWizard">Signing Your Applications</a></p>
38
39  <h2 id="RunningOnEmulatorEclipse">Running on the emulator</h2>
40
41  <p>Before you can run your application on the Android Emulator, you must <a href=
42  "{@docRoot}tools/devices/managing-avds.html">create an AVD</a>.</p>
43
44  <p>To run (or debug) your application, select <strong>Run</strong> &gt; <strong>Run</strong> (or
45  <strong>Run</strong> &gt; <strong>Debug</strong>) from the Eclipse menu bar. The ADT plugin will
46  automatically create a default run configuration for the project. Eclipse will then perform the
47  following:</p>
48
49  <ol>
50    <li>Compile the project (if there have been changes since the last build).</li>
51
52    <li>Create a default run configuration (if one does not already exist for the project).</li>
53
54    <li>Install and start the application on an emulator (or device), based on the Deployment
55    Target defined by the run configuration.
56
57      <p>By default, Android run configurations use an "automatic target" mode for selecting a
58      device target. For information on how automatic target mode selects a deployment target, see
59      <a href="#AutoAndManualTargetModes">Automatic and manual target modes</a> below.</p>
60    </li>
61  </ol>
62
63  <p>If you run the application with the Debug option, the application will start in the "Waiting For Debugger" mode. Once the debugger
64  is attached, Eclipse opens the Debug perspective and starts the application's main activity. Otherwise, if you run the
65  application with the normal Run option, Eclipse installs the application on the device and launches the main activity.</p>
66
67  <p>To set or change the run configuration used for your project, use the run configuration
68  manager. See the section below about <a href="#RunConfig">Creating a Run Configuration</a> for more information.</p>
69
70  <p>Be certain to create multiple AVDs upon which to test your application. You should have one
71  AVD for each platform and screen type with which your application is compatible. For instance, if
72  your application compiles against the Android 4.0 (API Level 14) platform, you should create an
73  AVD for each platform equal to and greater than 4.0 and an AVD for each <a href=
74  "{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test your
75  application on each one.</p>
76
77  <h2 id="RunningOnDeviceEclipse">Running on a device</h2>
78
79  <p>Before you can run your application on a device, you must perform some basic setup for your
80  device:</p>
81
82  <ul>
83    <li>Ensure that your application is debuggable by setting the
84    <code>android:debuggable</code> attribute of the <code>&lt;application&gt;</code>
85    element to <code>true</code>. As of ADT 8.0, this is done by default when you build in debug mode.</li>
86
87    <li>Enable <strong>USB debugging</strong> on your device.
88      <ul>
89        <li>On most devices running Android 3.2 or older, you can find the option under
90          <strong>Settings > Applications > Development</strong>.</li>
91        <li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>.
92          <p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer
93          options</strong> is hidden by default. To make it available, go
94          to <strong>Settings > About phone</strong> and tap <strong>Build number</strong>
95          seven times. Return to the previous screen to find <strong>Developer options</strong>.</p>
96        </li>
97      </ul>
98    </li>
99
100    <li>Ensure that your development computer can detect your device when connected via USB</li>
101  </ul>
102
103  <p>Read <a href="{@docRoot}tools/device.html">Using Hardware Devices</a>
104  for more information.</p>
105
106  <p>Once set up and your device is connected via USB, install your application on the device by
107  selecting <strong>Run</strong> &gt; <strong>Run</strong> (or <strong>Run</strong> &gt;
108  <strong>Debug</strong>) from the Eclipse menu bar.</p>
109
110  <h2 id="RunConfig">Creating a Run Configuration</h2>
111
112  <p>The run configuration specifies the project to run, the Activity to start, the emulator or
113  connected device to use, and so on. When you first run a project as an <em>Android
114  Application</em>, ADT will automatically create a run configuration. The default run
115  configuration will launch the default project Activity and use automatic target mode for device
116  selection (with no preferred AVD). If the default settings don't suit your project, you can
117  customize the run configuration or even create a new one.</p>
118
119  <p>To create or modify a run configuration, refer to the Eclipse documentation on how to create Run configurations.
120  The following steps highlight the important things you need to do for an Android project:</p>
121
122  <ol>
123    <li>Open the run configuration manager from the Run Menu.</li>
124
125    <li>Expand the <strong>Android Application</strong> item and create a new configuration or open
126    an existing one.
127    </li>
128
129    <li>With the Run Configuration selected, adjust your desired run configuration settings:
130      <ul>
131      <li>In the Android tab, specify the Project and Activity to launch.
132      </li>
133      <li><p>In the Target tab, consider whether you'd like to use Manual or Automatic mode when
134      selecting an AVD to run your application. See the following section on <a href=
135      "#AutoAndManualTargetModes">Automatic and manual target modes</a>).</p>
136
137      <p>You can specify any emulator options to the Additional Emulator Command Line Options
138      field. For example, you could add <code>-scale 96dpi</code> to scale the AVD's screen to an
139      accurate size, based on the dpi of your computer monitor. For a full list of emulator
140      options, see the <a href="{@docRoot}tools/help/emulator.html">Android
141      Emulator</a> document.</p>
142      </li>
143      </ul>
144    </li>
145  </ol>
146
147  <h4 id="AutoAndManualTargetModes">Automatic and manual target modes</h4>
148
149  <p>By default, a run configuration uses the <strong>automatic</strong> target mode in order to
150  select an AVD. In this mode, ADT will select an AVD for the application in the following
151  manner:</p>
152
153  <ol>
154    <li>If there's a device or emulator already running and its AVD configuration meets the
155    requirements of the application's build target, the application is installed and run upon
156    it.</li>
157
158    <li>If there's more than one device or emulator running, each of which meets the requirements
159    of the build target, a "device chooser" is shown to let you select which device to use.</li>
160
161    <li>If there are no devices or emulators running that meet the requirements of the build
162    target, ADT looks at the available AVDs. If there is an AVD that matches the build target of the project,
163    ADT chooses that AVD. If the AVD versions are newer than the build target of the project, ADT chooses
164    the oldest possible version of an AVD that meets the project's build target requirement.</li>
165
166    <li>If there are no suitable AVDs, the application is not installed a console error warning tells
167    you that there is no existing AVD that meets the build target requirements.</li>
168  </ol>
169
170  <p>However, if a "preferred AVD" is selected in the run configuration, then the application will
171  <em>always</em> be deployed to that AVD. If it's not already running, then a new emulator will be
172  launched.</p>
173
174  <p>If your run configuration uses <strong>manual</strong> mode, then the "device chooser" is
175  presented every time that your application is run, so that you can select which AVD to use.</p>