1 page.title=Building and Running from Eclipse with ADT 2 parent.title=Building and Running 3 parent.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—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}guide/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}guide/developing/devices/managing-avds.html">create an AVD</a>.</p> 43 44 <p>To run (or debug) your application, select <strong>Run</strong> > <strong>Run</strong> (or 45 <strong>Run</strong> > <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 1.5 (API Level 3) platform, you should create an 73 AVD for each platform equal to and greater than 1.5 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><application></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 USB Debugging on your device. You can find the setting on most Android devices by 88 going to <strong>Settings > Applications > Development > USB debugging</strong>.</li> 89 90 <li>Ensure that your development computer can detect your device when connected via USB</li> 91 </ul> 92 93 <p>Read <a href="{@docRoot}guide/developing/device.html">Using Hardware Devices</a> 94 for more information.</p> 95 96 <p>Once set up and your device is connected via USB, install your application on the device by 97 selecting <strong>Run</strong> > <strong>Run</strong> (or <strong>Run</strong> > 98 <strong>Debug</strong>) from the Eclipse menu bar.</p> 99 100 <h2 id="RunConfig">Creating a Run Configuration</h2> 101 102 <p>The run configuration specifies the project to run, the Activity to start, the emulator or 103 connected device to use, and so on. When you first run a project as an <em>Android 104 Application</em>, ADT will automatically create a run configuration. The default run 105 configuration will launch the default project Activity and use automatic target mode for device 106 selection (with no preferred AVD). If the default settings don't suit your project, you can 107 customize the run configuration or even create a new one.</p> 108 109 <p>To create or modify a run configuration, refer to the Eclipse documentation on how to create Run configurations. 110 The following steps highlight the important things you need to do for an Android project:</p> 111 112 <ol> 113 <li>Open the run configuration manager from the Run Menu.</li> 114 115 <li>Expand the <strong>Android Application</strong> item and create a new configuration or open 116 an existing one. 117 </li> 118 119 <li>With the Run Configuration selected, adjust your desired run configuration settings: 120 <ul> 121 <li>In the Android tab, specify the Project and Activity to launch. 122 </li> 123 <li><p>In the Target tab, consider whether you'd like to use Manual or Automatic mode when 124 selecting an AVD to run your application. See the following section on <a href= 125 "#AutoAndManualTargetModes">Automatic and manual target modes</a>).</p> 126 127 <p>You can specify any emulator options to the Additional Emulator Command Line Options 128 field. For example, you could add <code>-scale 96dpi</code> to scale the AVD's screen to an 129 accurate size, based on the dpi of your computer monitor. For a full list of emulator 130 options, see the <a href="{@docRoot}guide/developing/tools/emulator.html">Android 131 Emulator</a> document.</p> 132 </li> 133 </ul> 134 </li> 135 </ol> 136 137 <h4 id="AutoAndManualTargetModes">Automatic and manual target modes</h4> 138 139 <p>By default, a run configuration uses the <strong>automatic</strong> target mode in order to 140 select an AVD. In this mode, ADT will select an AVD for the application in the following 141 manner:</p> 142 143 <ol> 144 <li>If there's a device or emulator already running and its AVD configuration meets the 145 requirements of the application's build target, the application is installed and run upon 146 it.</li> 147 148 <li>If there's more than one device or emulator running, each of which meets the requirements 149 of the build target, a "device chooser" is shown to let you select which device to use.</li> 150 151 <li>If there are no devices or emulators running that meet the requirements of the build 152 target, ADT looks at the available AVDs. If there is an AVD that matches the build target of the project, 153 ADT chooses that AVD. If the AVD versions are newer than the build target of the project, ADT chooses 154 the oldest possible version of an AVD that meets the project's build target requirement.</li> 155 156 <li>If there are no suitable AVDs, the application is not installed a console error warning tells 157 you that there is no existing AVD that meets the build target requirements.</li> 158 </ol> 159 160 <p>However, if a "preferred AVD" is selected in the run configuration, then the application will 161 <em>always</em> be deployed to that AVD. If it's not already running, then a new emulator will be 162 launched.</p> 163 164 <p>If your run configuration uses <strong>manual</strong> mode, then the "device chooser" is 165 presented every time that your application is run, so that you can select which AVD to use.</p>