blob: c3ec2e552cc532d4afc2d98eca1d75b6f0602786 [file] [log] [blame]
Scott Main9d8dd802009-04-21 19:17:59 -07001page.title=Developing In Eclipse, with ADT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002@jd:body
3
Scott Main9d8dd802009-04-21 19:17:59 -07004<div id="qv-wrapper">
5 <div id="qv">
6 <h2>In this document</h2>
7 <ol>
8 <li><a href="#CreatingAProject">Creating an Android Project</a></li>
9 <li><a href="#Running">Running Your Application</a>
10 <ol>
11 <li><a href="#CreatingAnAvd">Creating an AVD</a></li>
12 <li><a href="#RunningAnApplication">Running an application</a></li>
13 </ol>
14 </li>
15 <li><a href="#RunConfig">Creating a Custom Run Configuration</a></li>
16 <li><a href="#Signing">Setting Up Application Signing</a></li>
17 <li><a href="#Tips">Eclipse Tips</a></li>
18 </div>
19</div>
20
21
22<p>The Android Development Tools (ADT) plugin for Eclipse adds powerful extensions to the Eclipse
23integrated development environment. It allows you to create and debug Android applications easier
24and faster. If you use Eclipse, the ADT plugin gives you an incredible boost in developing Android
25applications:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27<ul>
Scott Main9d8dd802009-04-21 19:17:59 -070028 <li>It gives you access to other Android development tools from inside the Eclipse IDE. For
29example, ADT lets you access the many capabilities of the DDMS tool: take screenshots, manage
30port-forwarding, set breakpoints, and view thread and process informationd irectly from Eclipse.</li>
31 <li>It provides a New Project Wizard, which helps you quickly create and set up all of the
32basic files you'll need for a new Android application.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 <li>It automates and simplifies the process of building your Android application.</li>
Scott Main9d8dd802009-04-21 19:17:59 -070034 <li>It provides an Android code editor that helps you write valid XML for your Android
35manifest and resource files.</li>
36 <li>It will even export your project into a signed APK, which can be distributed to users.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037</ul>
38
Scott Main9d8dd802009-04-21 19:17:59 -070039<p>To begin developing Android applications in the Eclipse IDE with ADT, you first need to
40download the Eclipse IDE and then download and install the ADT plugin. To do so, follow the
Dirk Dougherty3f9de8b2009-05-22 16:04:18 -070041steps given in <a href="{@docRoot}sdk/{@sdkCurrent}/installing.html#installingplugin">Installing
Scott Main9d8dd802009-04-21 19:17:59 -070042the ADT Plugin</a>.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Scott Main9d8dd802009-04-21 19:17:59 -070044<p>If you are already developing applications using a version of ADT earlier than 0.9, make
45sure to upgrade to the latest version before continuing. See the guide to
Dirk Dougherty3f9de8b2009-05-22 16:04:18 -070046<a href="{@docRoot}sdk/{@sdkCurrent}/upgrading.html#UpdateAdt">Updating Your Eclipse ADT Plugin</a>.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Scott Main9d8dd802009-04-21 19:17:59 -070048<p class="note"><strong>Note:</strong> This guide assumes you are using the latest version of
Dirk Dougherty3f9de8b2009-05-22 16:04:18 -070049the ADT plugin. While most of the information covered also applies to previous
Scott Main9d8dd802009-04-21 19:17:59 -070050versions, if you are using an older version, you may want to consult this document from
51the set of documentation included in your SDK package (instead of the online version).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Scott Main9d8dd802009-04-21 19:17:59 -070054<h2 id="CreatingAProject">Creating an Android Project</h2>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Scott Main9d8dd802009-04-21 19:17:59 -070056<p>The ADT plugin provides a New Project Wizard that you can use to quickly create a new
57Android project (or a project from existing code). To create a new project:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59<ol>
Scott Main9d8dd802009-04-21 19:17:59 -070060 <li>Select <strong>File</strong> &gt; <strong>New</strong> &gt; <strong>Project</strong>.</li>
61 <li>Select <strong>Android</strong> &gt; <strong>Android Project</strong>, and click
62 <strong>Next</strong>.</li>
63 <li>Select the contents for the project:
64 <ul>
65 <li>Enter a <em>Project Name</em>. This will be the name of the folder where your
66 project is created.</li>
67 <li>Under Contents, select <strong>Create new project in workspace</strong>.
68 Select your project workspace location.</li>
69 <li>Under Target, select an Android target to be used as the project's Build Target.
70 The Build Target
71 specifies which Android platform you'd like your application built against.
72 <p>Unless you know that you'll be using new APIs introduced in the latest SDK, you should
73 select a target with the lowest platform version possible, such as Android 1.1.</p>
74 <p class="note"><strong>Note:</strong> You can change your the Build Target for your
75 project at any time: Right-click the project in the Package Explorer, select
76 <strong>Properties</strong>, select <strong>Android</strong> and then check
77 the desired Project Target.</p>
78 </li>
79 <li>Under Properties, fill in all necessary fields.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 <ul>
Scott Main9d8dd802009-04-21 19:17:59 -070081 <li>Enter an <em>Application name</em>. This is the human-readable title for your
82 application &mdash; the name that will appear on the Android device.</li>
83 <li>Enter a <em>Package name</em>. This is the package namespace (following the same rules
84 as for packages in the Java programming language) where all your source code
85 will reside.</li>
86 <li>Select <em>Create Activity</em> (optional, of course, but common) and enter a name
87 for your main Activity class.</li>
88 <li>Enter a <em>Min SDK Version</em>. This is an integer that indicates
89 the minimum API Level required to properly run your application.
90 Entering this here automatically sets the <code>minSdkVersion</code> attribute in the
91 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a>
92 of your Android Manifest file. If you're unsure of the appropriate API Level to use,
93 copy the API Level listed for the Build Target you selected in the Target tab.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 </ul>
Scott Main9d8dd802009-04-21 19:17:59 -070095 </li>
96 </ul>
97 </li>
98 <li>Click <strong>Finish</strong>.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099</ol>
100
Scott Main9d8dd802009-04-21 19:17:59 -0700101<p class="note"><strong>Tip:</strong>
102You can also start the New Project Wizard from the <em>New</em> icon in the toolbar.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Scott Main9d8dd802009-04-21 19:17:59 -0700104<p>Once you complete the New Project Wizard, ADT creates the following
105folders and files in your new project:</p>
106 <dl>
107 <dt><code>src/</code></dt>
108 <dd>Includes your stub Activity Java file. All other Java files for your application
109 go here.</dd>
110 <dt><code><em>&lt;Android Version&gt;</em>/</code> (e.g., <code>Android 1.1/</code>)</dt>
111 <dd>Includes the <code>android.jar</code> file that your application will build against.
112 This is determined by the build target that you have chosen in the <em>New Project
113 Wizard</em>.</dd>
114 <dt><code>gen/</code></dt>
115 <dd>This contains the Java files generated by ADT, such as your <code>R.java</code> file
116 and interfaces created from AIDL files.</dd>
117 <dt><code>assets/</code></dt>
118 <dd>This is empty. You can use it to store raw asset files. See
119 <a href="{@docRoot}guide/topics/resources/index.html">Resources and Assets</a>.</dd>
120 <dt><code>res/</code></dt>
121 <dd>A folder for your application resources, such as drawable files, layout files, string
122 values, etc. See
123 <a href="{@docRoot}guide/topics/resources/index.html">Resources and Assets</a>.</dd>
124 <dt><code>AndroidManifest.xml</code></dt>
125 <dd>The Android Manifest for your project. See
126 <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml
127 File</a>.</dd>
128 <dt><code>default.properties</code></dt>
129 <dd>This file contains project settings, such as the build target. This files is integral
130 to the project, as such, it should be maintained in a Source Revision Control system.
131 It should never be edited manually &mdash; to edit project properties,
132 right-click the project folder and select "Properties".</dd>
133 </dl>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Scott Main9d8dd802009-04-21 19:17:59 -0700136<h2 id="Running">Running Your Application</h2>
137
Scott Main75f7a262009-08-05 19:31:00 -0700138<p>Before you can run your application on the Android Emulator,
Scott Main9d8dd802009-04-21 19:17:59 -0700139you <strong>must</strong> create an Android Virtual Device (AVD).
140An AVD is a configuration that specifies the Android platform to be used on the emulator.
Dirk Dougherty3f9de8b2009-05-22 16:04:18 -0700141You can read more in the <a href="{@docRoot}guide/developing/tools/avd.html">Android Virtual
142Devices</a> document, but if you just want to get started, follow the simple guide below to
143create an AVD.</p>
Scott Main9d8dd802009-04-21 19:17:59 -0700144
145<p>If you will be running your applications only on actual device hardware, you do not
146need an AVD &mdash; see
147<a href="{@docRoot}guide/developing/device.html">Developing On a Device</a> for information
148on running your applicaiton.</p>
149
150<h3 id="CreatingAnAvd">Creating an AVD</h3>
151
Dirk Doughertya6602f12009-08-27 16:26:43 -0700152<p>With ADT 0.9.3 and above, the Android SDK and AVD Manager provides a simple graphical interface
Scott Main75f7a262009-08-05 19:31:00 -0700153for creating and managing AVDs. (If you're using ADT version 0.9.1 or older, you must
154use the <code>android</code> tool to create your AVDs&mdash;read the AVD guide to
155<a href="{@docRoot}guide/developing/tools/avd.html#creating">Creating an AVD</a>.)
156
Dirk Doughertya6602f12009-08-27 16:26:43 -0700157<p>To create an AVD with the AVD Manager:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159<ol>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700160 <li>Select <strong>Window > Android SDK and AVD Manager</strong>, or click the Android SDK and AVD Manager icon (a black device)
Scott Main75f7a262009-08-05 19:31:00 -0700161 in the Eclipse toolbar.</p>
Scott Main9d8dd802009-04-21 19:17:59 -0700162 </li>
Scott Main75f7a262009-08-05 19:31:00 -0700163 <li>In the Virtual Devices panel, you'll see a list of existing AVDs. Click <strong>New</strong>
164 to create a new AVD.</li>
165 <li>Fill in the details for the AVD.
166 <p>Give it a name, a platform target, an SD card image (optional), and
167 a skin (HVGA is default).</p>
Scott Main9d8dd802009-04-21 19:17:59 -0700168 </li>
Scott Main75f7a262009-08-05 19:31:00 -0700169 <li>Click <strong>Create AVD</strong>.</li>
Scott Main9d8dd802009-04-21 19:17:59 -0700170</ol>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Dirk Doughertya6602f12009-08-27 16:26:43 -0700172<p>Your AVD is now ready and you can close the AVD Manager.
Scott Main75f7a262009-08-05 19:31:00 -0700173In the next section, you'll see how the AVD is used
Scott Main9d8dd802009-04-21 19:17:59 -0700174when launching your application on an emulator.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Scott Main75f7a262009-08-05 19:31:00 -0700176<p>For more information about AVDs, read the
Scott Main8a4c53a2009-04-24 13:41:44 -0700177<a href="{@docRoot}guide/developing/tools/avd.html">Android Virtual Devices</a>
178documentation.</p>
Scott Main9d8dd802009-04-21 19:17:59 -0700179
180
181<h3 id="RunningYourApplication">Running your application</h3>
182
183<p class="note"><strong>Note:</strong> Before you can run your application, be sure that
184you have created an AVD with a target that satisfies your application's Build Target.
185If an AVD cannot be found that meets the requirements of your Build Target, you will see
186a console error telling you so and the launch will be aborted.</p>
187
188<p>To run (or debug) your application, select <strong>Run</strong> &gt; <strong>Run</strong> (or
189<strong>Run</strong> &gt; <strong>Debug</strong>) from the Eclipse main menu. The ADT plugin
190will automatically create a default launch configuration for the project.</p>
191
192<p>When you choose to run or debug your application, Eclipse will perform the following:</p>
193
194<ol>
195 <li>Compile the project (if there have been changes since the last build).</li>
196 <li>Create a default launch configuration (if one does not already exist for the project).</li>
197 <li>Install and start the application on an emulator or device (based on the Deployment Target
198 defined by the run configuration).
199 <p>By default, Android application run configurations use an "automatic target" mode for
200 selecting a device target. For information on how automatic target mode selects a
201 deployment target, see <a href="#AutoAndManualTargetModes">Automatic and manual
202 target modes</a> below.</p>
203 </li>
204</ol>
205
206<p>If debugging, the application will start in the "Waiting For Debugger" mode. Once the
207debugger is attached, Eclipse will open the Debug perspective.</p>
208
209<p>To set or change the launch configuration used for your project, use the launch configuration manager.
210See <a href="#launchconfig">Creating a Launch Configuration</a> for information.</p>
211
212
213<h2 id="RunConfig">Creating a Run Configuration</h2>
214
215<p>The run configuration specifies the project to run, the Activity
216to start, the emulator options to use, and so on. When you first run a project
217as an <em>Android Application</em>, ADT will automatically create a run configuration.
218The default run configuration will
219launch the default project Activity and use automatic target mode for device selection
Dirk Doughertya6602f12009-08-27 16:26:43 -0700220(with no preferred AVD). If the default settings don't suit your project, you can
Scott Main9d8dd802009-04-21 19:17:59 -0700221customize the launch configuration or even create a new.</p>
222
223<p>To create or modify a launch configuration, follow these steps as appropriate
224for your Eclipse version:</p>
225
226<ol>
227 <li>Open the run configuration manager.
228 <ul>
229 <li>In Eclipse 3.3 (Europa), select <strong>Run</strong> &gt;
230 <strong>Open Run Dialog</strong> (or <strong>Open Debug Dialog</strong>)
231 </li>
232 <li>In Eclipse 3.4 (Ganymede), select <strong>Run </strong>&gt;
233 <strong>Run Configurations</strong> (or
234 <strong>Debug Configurations</strong>)
235 </li>
236 </ul>
237 </li>
238 <li>Expand the <strong>Android Application</strong> item and create a new
239 configuration or open an existing one.
240 <ul>
241 <li>To create a new configuration:
242 <ol>
243 <li>Select <strong>Android Application</strong> and click the <em>New launch configuration</em>
244 icon above the list (or, right-click <strong>Android Application</strong> and click
245 <strong>New</strong>).</li>
246 <li>Enter a Name for your configuration.</li>
247 <li>In the Android tab, browse and select the project you'd like to run with the
248 configuration.</li>
249 </ol>
250 <li>To open an existing configuration, select the configuration name from the list
251 nested below <strong>Android Application</strong>.</li>
252 </ul>
253 </li>
254 <li>Adjust your desired launch configuration settings.
255 <p>In the Target tab, consider whether you'd like to use Manual or Automatic mode
256 when selecting an AVD to run your application.
257 See the following section on <a href=#AutoAndManualModes">Automatic and manual target
258 modes</a>).</p>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700259 <p>You can specify any emulator options to the Additional Emulator Command
260 Line Options field. For example, you could add <code>-scale 96dpi</code> to
261 scale the AVD's screen to an accurate size, based on the dpi of your
262 computer monitor. For a full list of emulator options, see the <a
263 href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a>
264document.</p>
Scott Main9d8dd802009-04-21 19:17:59 -0700265 </li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266</ol>
267
268
Scott Main9d8dd802009-04-21 19:17:59 -0700269<h3 id="AutoAndManualTargetModes">Automatic and manual target modes</h3>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
Scott Main9d8dd802009-04-21 19:17:59 -0700271<p>By default, a run configuration uses the <strong>automatic</strong> target mode in order to select
272an AVD. In this mode, ADT will select an AVD for the application in the following manner:</p>
273
274<ol>
275 <li>If there's a device or emulator already running and its AVD configuration
276 meets the requirements of the application's build target, the application is installed
277 and run upon it.</li>
278 <li>If there's more than one device or emulator running, each of which meets the requirements
279 of the build target, a "device chooser" is shown to let you select which device to use.</li>
280 <li>If there are no devices or emulators running that meet the requirements of the build target,
281 ADT looks at the available AVDs. If one meets the requirements of the build target,
282 the AVD is used to launch a new emulator, upon which the application is installed and run.</li>
283 <li>If all else fails, the application will not be run and you will see a console error warning
284 you that there is no existing AVD that meets the build target requirements.</li>
285</ol>
286
287<p>However, if a "preferred AVD" is selected in the run configuration, then the application
288will <em>always</em> be deployed to that AVD. If it's not already running, then a new emulator
289will be launched.</p>
290
291<p>If your run configuration uses <strong>manual</strong> mode, then the "device chooser"
292is presented every time that your application is run, so that you can select which AVD to use.</p>
293
294
295<h2 id="Signing">Signing your Applications</h2>
296
297<p>As you begin developing Android applications, understand that all
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298Android applications must be digitally signed before the system will install
Scott Main9d8dd802009-04-21 19:17:59 -0700299them on an emulator or an actual device. There are two ways to do this:
300with a debug key (for immediate testing on an emulator or development device)
301or with a private key (for application distribution).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
303<p>The ADT plugin helps you get started quickly by signing your .apk files with
Scott Main9d8dd802009-04-21 19:17:59 -0700304a debug key, prior to installing them on an emulator or development device. This means that you can
305quickly run your application from Eclipse without having to
306generate your own private key. No specific action on your part is needed,
307provided ADT has access to Keytool.However, please note that if you intend
308to publish your application, you <strong>must</strong> sign the application with your
309own private key, rather than the debug key generated by the SDK tools.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310
Scott Main9d8dd802009-04-21 19:17:59 -0700311<p>Please read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your
312Applications</a>, which provides a thorough guide to application signing on Android
313and what it means to you as an Android application developer. The document also includes
314a guide to exporting and signing your application with the ADT's Export Wizard.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316
Scott Main9d8dd802009-04-21 19:17:59 -0700317<h2 id="Tips">Eclipse Tips </h2>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Scott Main9d8dd802009-04-21 19:17:59 -0700319<h3 id="arbitraryexpressions">Executing arbitrary Java expressions in Eclipse</h3>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
Scott Main9d8dd802009-04-21 19:17:59 -0700321<p>You can execute arbitrary code when paused at a breakpoint in Eclipse. For example,
322 when in a function with a String argument called &quot;zip&quot;, you can get
323 information about packages and call class methods. You can also invoke arbitrary
324 static methods: for example, entering <code>android.os.Debug.startMethodTracing()</code> will
325 start dmTrace. </p>
326<p>Open a code execution window, select <strong>Window</strong>&gt;<strong>Show
327 View</strong>&gt;<strong>Display</strong> from the main menu to open the
328 Display window, a simple text editor. Type your expression, highlight the
329 text, and click the 'J' icon (or CTRL + SHIFT + D) to run your
330 code. The code runs in the context of the selected thread, which must be
331 stopped at a breakpoint or single-step point. (If you suspend the thread
332 manually, you have to single-step once; this doesn't work if the thread is
333 in Object.wait().)</p>
334<p>If you are currently paused on a breakpoint, you can simply highlight and execute
335 a piece of source code by pressing CTRL + SHIFT + D. </p>
336<p>You can highlight a block of text within the same scope by pressing ALT +SHIFT
337 + UP ARROW to select larger and larger enclosing blocks, or DOWN ARROW to select
338 smaller blocks. </p>
339<p>Here are a few sample inputs and responses in Eclipse using the Display window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340
Scott Main9d8dd802009-04-21 19:17:59 -0700341<table width="100%" border="1">
342 <tr>
343 <th scope="col">Input</th>
344 <th scope="col">Response</th>
345 </tr>
346 <tr>
347 <td><code>zip</code></td>
348 <td><code>(java.lang.String)
349 /work/device/out/linux-x86-debug/android/app/android_sdk.zip</code></td>
350 </tr>
351 <tr>
352 <td><code>zip.endsWith(&quot;.zip&quot;)</code></td>
353 <td><code>(boolean) true</code></td>
354 </tr>
355 <tr>
356 <td><code>zip.endsWith(&quot;.jar&quot;)</code></td>
357 <td><code>(boolean) false</code></td>
358 </tr>
359</table>
360<p>You can also execute arbitrary code when not debugging by using a scrapbook page.
361 Search the Eclipse documentation for &quot;scrapbook&quot;.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
363
Scott Main9d8dd802009-04-21 19:17:59 -0700364<h3>Running DDMS Manually</h3>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
Scott Main9d8dd802009-04-21 19:17:59 -0700366<p>Although the recommended way to debug is to use the ADT plugin, you can manually run
367DDMS and configure Eclipse to debug on port 8700. (<strong>Note: </strong>Be sure that you
368have first started <a href="{@docRoot}guide/developing/tools/ddms.html">DDMS</a>). </p>
369
370
371<!-- TODO: clean this up and expand it to cover more wizards and features
372<h3>ADT Wizards</h3>
373
374<p>Notice that the "New Android Project" wizard has been expanded to use the multi-platform
375capabilities of the new SDK.</p>
376
377<p>There is now a "New XML File" wizard that lets you create skeleton XML resource
378files for your Android projects. This makes it easier to create a new layout, a new menu, a
379new strings file, etc.</p>
380
381<p>Both wizards are available via <strong>File > New</strong> and new icons in the main
382Eclipse toolbar (located to the left of the Debug and Run icons).
383If you do not see the new icons, you may need to select <strong>Window > Reset
384Perspective</strong> from the Java perspective.</p>
385-->