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