blob: 8c482ee40ce57992e07de63fefec5100533df969 [file] [log] [blame]
page.title=In Eclipse, with ADT
@jd:body
<p>The Android Development Tools (ADT) plugin for Eclipse adds powerful extensions to the Eclipse integrated development environment. It allows you to create and debug Android applications easier and faster. If you use Eclipse, the ADT plugin gives you an incredible boost in developing Android applications:</p>
<ul>
<li>It gives you access to other Android development tools from inside the Eclipse IDE. For example, ADT lets you access the many capabilities of the DDMS tool: take screenshots, manage port-forwarding, set breakpoints, and view thread and process informationd irectly from Eclipse.</li>
<li>It provides a New Project Wizard, which helps you quickly create and set up all of the basic files you'll need for a new Android application.</li>
<li>It automates and simplifies the process of building your Android application.</li>
<li>It provides an Android code editor that helps you write valid XML for your Android manifest and resource files.</li>
</ul>
<p>To begin developing Android applications in the Eclipse IDE with ADT, you first need to download the Eclipse IDE and then download and install the ADT plugin. To do so, follow the steps given in <a href="{@docRoot}sdk/1.1_r1/installing.html#installingplugin">Installing the ADT Plugin</a>, in the installation documentation included with your SDK package. </p>
<p>Once you've installed the ADT plugin, you begin by creating an Android
project and then set up a launch configuration. After that, you can write, run, and debug
your application. </p>
<p>The sections below provide instructions assuming that you have installed the ADT plugin
in your Eclipse environment. If you haven't installed the ADT plugin, you should do that
before using the instructions below. </p>
<a name="creatingaproject" id="creatingaproject"></a>
<h2>Creating an Android Project</h2>
<p>The ADT plugin provides a New Project Wizard that you can use to quickly create an
Eclipse project for new or existing code. To create the project, follow these steps:</p>
<a name="existingcode"></a>
<ol>
<li>Select <strong>File</strong> &gt; <strong>New</strong> &gt; <strong>Project</strong></li>
<li>Select <strong>Android</strong> &gt; <strong>Android Project</strong>, and press <strong>Next</strong></li>
<li>Select the contents for the project:
<ul>
<li>Select <strong>Create new project in workspace</strong> to start a project for new code.
<p>Enter the project name, the base package name, the name of a single Activity class to create as a stub .java file, and a name to use for your application.</p></li>
<li>Select <strong>Create project from existing source</strong> to start a project from existing code. Use this option if you want to build and run any of the sample applications included with the SDK. The sample applications are located in the samples/ directory in the SDK.
<p>Browse to the directory containing the existing source code and click OK. If the directory contains a valid Android manifest file, the ADT plugin fills in the package, activity, and application names for you.</p>
</li>
</ul>
</li>
<li>Press <strong>Finish</strong>.</li>
</ol>
<p>The ADT plugin creates the these folders and
files for you as appropriate for the type of project:</p>
<ul>
<li>src/&nbsp;&nbsp;&nbsp;A
folder that includes your stub .java Activity file.</li>
<li>res/&nbsp;&nbsp;&nbsp;A folder for your
resources.</li>
<li>AndroidManifest.xml&nbsp;&nbsp;&nbsp;The
manifest for your project. </li>
</ul>
</ol>
<a name="launchconfig" id="launchconfig"></a>
<h2>Creating a Launch Configuration </h2>
<p>Before you can run and debug your application in Eclipse, you must create a launch configuration for it. A launch configuration specifies the project to launch, the Activity to start, the emulator options to use, and so on. </p>
<p>To create a launch configuration for the application, follow these steps as appropriate for your Eclipse version:</p>
<ol>
<li>Open the launch configuration manager.
<ul>
<li>In Eclipse 3.3 (Europa), select <strong>Run </strong>&gt;
<strong>Open Run Dialog... </strong>or <strong>Run </strong>&gt;
<strong>Open Debug Dialog... </strong>as appropriate.
</li>
<li>In Eclipse 3.4 (Ganymede), select <strong>Run </strong>&gt;
<strong>Run Configurations... </strong>or <strong>Run </strong>&gt;
<strong>Debug Configurations... </strong>as appropriate.
</li>
</ul>
</li>
<li>In the project type list on the left, locate the <strong> Android Application</strong> item and double-click it (or right-click &gt; <strong>New</strong>), to create a new launch configuration.</li>
<li>Enter a name for your configuration.</li>
<li>On the Android tab, browse for the project and Activity to start.</li>
<li>On the Target tab, set the desired screen and network properties, as well as any other <a href="{@docRoot}guide/developing/tools/emulator.html#startup-options">emulator startup options</a>.</li>
<li>You can set additional options on the Common tab as desired.</li>
<li>Press <strong>Apply</strong> to save the launch configuration, or press <strong>Run</strong> or <strong>Debug</strong> (as appropriate).</li>
</ol>
<h2 id="sign_in_adt">Setting Up Application Signing</h2>
<p>As you begin developing Android applications, you should understand that all
Android applications must be digitally signed before the system will install
them on the emulator or an actual device. </p>
<p>The ADT plugin helps you get started quickly by signing your .apk files with
a debug key, prior to installing them on the emulator. This means that you can
compile your application and install it on the emulator without having to
generate your own private key. However, please note that if you intend to
publish your application, you <em>must</em> sign the application with your own
private key, rather than the debug key generated by the SDK tools. </p>
<p>To sign your applications, the ADT plugin requires the Keytool utility
included in the JDK. To set up your development environment for
signing, you need to make sure that Keytool is available on your
machine that the ADT plugin knows how to find it. </p>
<p>In most cases, you can tell the SDK build tools how to find Keytool by making
sure that your JAVA_HOME environment variable is set and that it references a
suitable JDK. Alternatively, you can add the JDK version of Keytool to your
PATH variable.</p>
<p>If you are developing on a version of Linux that originally came with Gnu
Compiler for Java, make sure that the system is using the JDK version of
Keytool, rather than the gcj version. If keytool is already in your PATH, it
might be pointing to a symlink at /usr/bin/keytool. In this case, check the
symlink target to make sure that it points to the keytool in the JDK.</p>
<p>In all cases, please read and understand <a
href="{@docRoot}guide/publishing/app-signing.html">Signing Your
Applications</a>, which provides an overview of application signing on Android
and what it means to you as an Android application developer. </p>
<a name="installingrunningdebugging" id="installingrunningdebugging"></a>
<h2>Running and Debugging an Application</h2>
<p>Once you've set up the project and launch configuration for your application, you can run or debug it as described below.</p>
From the Eclipse main menu, select <strong>Run</strong> &gt; <strong>Run</strong> or <strong>Run</strong> &gt; <strong>Debug</strong> as appropriate, to run or debug the active launch configuration. </li>
<p>Note that the active launch configuration is the one most recently selected in the Run configuration manager. It does not necessarily correspond to the application that is selected in the Eclipse Navigation pane (if any).</p>
<p>To set or change the active launch configuration, use the launch configuration manager. See <a href="#launchconfig">Creating a Launch Configuration</a> for information about how to access the launch configuration manager.</strong>.</p>
<p>Running or debugging the application triggers these actions:</p>
<ul><li>Starts the emulator, if it is not already running. </li>
<li>Compiles the project, if there have been changes since the last build, and installs the application on the emulator. </li>
<li><strong>Run</strong> starts the application. </li>
<li><strong>Debug</strong> starts the application in "Wait for debugger" mode, then opens the Debug perspective and attaches the Eclipse Java debugger to the application.</li>
</ul>
<h2 id="tips">Eclipse Tips </h2>
<h3>Executing arbitrary Java expressions in Eclipse<a name="arbitraryexpressions" id="arbitraryexpressions"></a></h3>
<p>You can execute arbitrary code when paused at a breakpoint in Eclipse. For example,
when in a function with a String argument called &quot;zip&quot;, you can get
information about packages and call class methods. You can also invoke arbitrary
static methods: for example, entering <code>android.os.Debug.startMethodTracing()</code> will
start dmTrace. </p>
<p>Open a code execution window, select <strong>Window</strong>&gt;<strong>Show
View</strong>&gt;<strong>Display</strong> from the main menu to open the
Display window, a simple text editor. Type your expression, highlight the
text, and click the 'J' icon (or CTRL + SHIFT + D) to run your
code. The code runs in the context of the selected thread, which must be
stopped at a breakpoint or single-step point. (If you suspend the thread
manually, you have to single-step once; this doesn't work if the thread is
in Object.wait().)</p>
<p>If you are currently paused on a breakpoint, you can simply highlight and execute
a piece of source code by pressing CTRL + SHIFT + D. </p>
<p>You can highlight a block of text within the same scope by pressing ALT +SHIFT
+ UP ARROW to select larger and larger enclosing blocks, or DOWN ARROW to select
smaller blocks. </p>
<p>Here are a few sample inputs and responses in Eclipse using the Display window.</p>
<table width="100%" border="1">
<tr>
<th scope="col">Input</th>
<th scope="col">Response</th>
</tr>
<tr>
<td><code>zip</code></td>
<td><code>(java.lang.String) /work/device/out/linux-x86-debug/android/app/android_sdk.zip</code></td>
</tr>
<tr>
<td><code>zip.endsWith(&quot;.zip&quot;)</code></td>
<td><code>(boolean) true</code></td>
</tr>
<tr>
<td><code>zip.endsWith(&quot;.jar&quot;)</code></td>
<td><code>(boolean) false</code></td>
</tr>
</table>
<p>You can also execute arbitrary code when not debugging by using a scrapbook page.
Search the Eclipse documentation for &quot;scrapbook&quot;.</p>
<h3>Running DDMS Manually</h3>
<p>Although the recommended way to debug is to use the ADT plugin, you can manually run DDMS and configure Eclipse to debug on port 8700. (<strong>Note: </strong>Be sure that you have first started <a href="{@docRoot}guide/developing/tools/ddms.html">DDMS</a>). </p>