page.title=Developing In Eclipse, with ADT @jd:body
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:
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 Installing the ADT Plugin.
If you are already developing applications using a version of ADT earlier than 0.9, make sure to upgrade to the latest version before continuing. See the guide to Updating Your ADT Plugin.
Note: This guide assumes you are using the latest version of the ADT plugin. While most of the information covered also applies to previous versions, if you are using an older version, you may want to consult this document from the set of documentation included in your SDK package (instead of the online version).
The ADT plugin provides a New Project Wizard that you can use to quickly create a new Android project (or a project from existing code). To create a new project:
Unless you know that you'll be using new APIs introduced in the latest SDK, you should select a target with the lowest platform version possible.
Note: You can change your the Build Target for your project at any time: Right-click the project in the Package Explorer, select Properties, select Android and then check the desired Project Target.
minSdkVersion attribute in the
<uses-sdk>
of your Android Manifest file. If you're unsure of the appropriate API Level to use,
copy the API Level listed for the Build Target you selected in the Target tab.Tip: You can also start the New Project Wizard from the New icon in the toolbar.
Once you complete the New Project Wizard, ADT creates the following folders and files in your new project:
src/<Android Version>/ (e.g., Android 1.1/)android.jar file that your application will build against.
This is determined by the build target that you have chosen in the New Project
Wizard.gen/R.java file
and interfaces created from AIDL files.assets/res/AndroidManifest.xmldefault.propertiesAn Android Virtual Device (AVD) is a device configuration for the emulator that allows you to model real world devices. In order to run an instance of the emulator, you must create an AVD.
To create an AVD from Eclipse:
Give it a name, a platform target, an SD card size, and a skin (HVGA is default).
Note: Be sure to define a target for your AVD that satisfies your application's Build Target (the AVD platform target must have an API Level equal to or greater than the API Level that your application compiles against).
Your AVD is now ready and you can either close the SDK and AVD Manager, create more AVDs, or launch an emulator with the AVD by selecting a device and clicking Start.
For more information about AVDs, read the Android Virtual Devices documentation.
Create multiple AVDs that each define a different device configuration with which your application is compatible, then launch each AVD into a new emulator from the SDK and AVD Manager. Set the target mode in your app's run configuration to manual, so that when you run your application, you can select from the available virtual devices.
Running your application from Eclipse will usually require just a couple clicks, whether you're running it on the emulator or on an attached device. The information below describes how to get set up and run your application from Eclipse.
Before you can run your application on the Android Emulator, you must create an AVD.
To run (or debug) your application, select Run > Run (or Run > Debug) from the Eclipse menu bar. The ADT plugin will automatically create a default launch configuration for the project. Eclipse will then perform the following:
By default, Android run configurations use an "automatic target" mode for selecting a device target. For information on how automatic target mode selects a deployment target, see Automatic and manual target modes below.
If debugging, the application will start in the "Waiting For Debugger" mode. Once the debugger is attached, Eclipse will open the Debug perspective.
To set or change the launch configuration used for your project, use the launch configuration manager. See Creating a Launch Configuration for information.
Be certain to create multiple AVDs upon which to test your application. You should have one AVD for each platform and screen type with which your application is compatible. For instance, if your application compiles against the Android 1.5 (API Level 3) platform, you should create an AVD for each platform equal to and greater than 1.5 and an AVD for each screen type you support, then test your application on each one.
Before you can run your application on a device, you must perform some basic setup for your device:
Read Setting up a Device for Development for more information.
Once set up and your device is connected via USB, install your application on the device by selecting Run > Run (or Run > Debug) from the Eclipse menu bar.
The run configuration specifies the project to run, the Activity to start, the emulator or connected device to use, and so on. When you first run a project as an Android Application, ADT will automatically create a run configuration. The default run configuration will launch the default project Activity and use automatic target mode for device selection (with no preferred AVD). If the default settings don't suit your project, you can customize the launch configuration or even create a new.
To create or modify a launch configuration, follow these steps as appropriate for your Eclipse version:
In the Target tab, consider whether you'd like to use Manual or Automatic mode when selecting an AVD to run your application. See the following section on Automatic and manual target modes).
You can specify any emulator options to the Additional Emulator Command
Line Options field. For example, you could add -scale 96dpi to
scale the AVD's screen to an accurate size, based on the dpi of your
computer monitor. For a full list of emulator options, see the Android Emulator
document.
By default, a run configuration uses the automatic target mode in order to select an AVD. In this mode, ADT will select an AVD for the application in the following manner:
However, if a "preferred AVD" is selected in the run configuration, then the application will always be deployed to that AVD. If it's not already running, then a new emulator will be launched.
If your run configuration uses manual mode, then the "device chooser" is presented every time that your application is run, so that you can select which AVD to use.
As you begin developing Android applications, understand that all Android applications must be digitally signed before the system will install them on an emulator or an actual device. There are two ways to do this: with a debug key (for immediate testing on an emulator or development device) or with a private key (for application distribution).
The ADT plugin helps you get started quickly by signing your .apk files with a debug key, prior to installing them on an emulator or development device. This means that you can quickly run your application from Eclipse without having to generate your own private key. No specific action on your part is needed, provided ADT has access to Keytool.However, please note that if you intend to publish your application, you must sign the application with your own private key, rather than the debug key generated by the SDK tools.
Please read Signing Your Applications, which provides a thorough guide to application signing on Android and what it means to you as an Android application developer. The document also includes a guide to exporting and signing your application with the ADT's Export Wizard.
You can execute arbitrary code when paused at a breakpoint in Eclipse. For example,
when in a function with a String argument called "zip", you can get
information about packages and call class methods. You can also invoke arbitrary
static methods: for example, entering android.os.Debug.startMethodTracing() will
start dmTrace.
Open a code execution window, select Window > Show View > Display 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().)
If you are currently paused on a breakpoint, you can simply highlight and execute a piece of source code by pressing CTRL + SHIFT + D.
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.
Here are a few sample inputs and responses in Eclipse using the Display window.
| Input | Response |
|---|---|
zip |
(java.lang.String)
/work/device/out/linux-x86-debug/android/app/android_sdk.zip |
zip.endsWith(".zip") |
(boolean) true |
zip.endsWith(".jar") |
(boolean) false |
You can also execute arbitrary code when not debugging by using a scrapbook page. Search the Eclipse documentation for "scrapbook".
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. (Note: Be sure that you have first started DDMS).