Android developing in eclipse, with ADT

Source: Internet
Author: User
Tags configuration settings
Document directory
  • Running ddms manually
Developing in eclipse, with adtin this document
  1. Creating an android Project
  2. Running your application
    1. Creating an AVD
    2. Running an application
  3. Creating a custom run Configuration
  4. Setting up application signing
  5. Eclipse tips

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:

  • It gives you access to other android development tools from inside the Eclipse IDE.
    Example, ADT lets you access the specified capabilities of the ddms tool: take screenshots, manage
    Port-forwarding, set breakpoints, and view thread and process informationd irectly from eclipse.
  • It provides a new project wizard, which helps you quickly create and set up all of
    Basic files you'll need for a new Android Application.
  • It automates and simplifies the process of building your android application.
  • It provides an android code editor that helps you write valid XML for your android
    Manifest and resource files.
  • It will even export your project into a signed APK, which can be distributed to users.

To begin developing Android applications in the Eclipse IDE with ADT, you first need
Download the Eclipse IDE and then download and install the ADT plugin. To do so, follow
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
Updating your ADT plugin
.

Note:
This Guide assumes you are using the latest version
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 encoded in your SDK package (instead of the online version ).

Creating an android Project

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:

  1. SelectFile
    >New
    >Project
    .
  2. SelectAndroid
    >Android Project
    , And click
    Next
    .
  3. Select the contents for the project:
    • EnterProject name
      . This will be the name of the folder where your
      Project is created.
    • Under contents, selectCreate new project in Workspace
      .
      Select your project workspace location.
    • Under target, select an android target to be used as the project's build target.
      The build target
      Specifies which Android platform you 'd like your application built against.

      Unless you know that you'll be using new APIs introduced in the latest SDK, you shoshould
      Select a target with the lowest platform version possible, such as Android 1.1.

      Note:
      You can change your the build target for your
      Project at any time: Right-click the project in the package explorer, select
      Properties
      , SelectAndroid
      And then check
      The desired project target.

    • Under properties, fill in all necessary fields.
      • EnterApplication name
        . This is the human-readable title for your
        Application-the name that will appear on the Android device.
      • EnterPackage name
        . This is the package namespace (following the same rules
        As for packages in the Java programming language) where all your source code
        Will reside.
      • SelectCreate activity
        (Optional, of course, but common) and enter a name
        For your main activity class.
      • EnterMin SDK version
        . This is an integer that indicates
        The minimum API level required to properly run your application.
        Entering this here automatically setsminSdkVersion
        Attribute in
        <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.
  4. ClickFinish
    .

Tip:
You can also start the new project wizard fromNew
Icon in the toolbar.

Once you complete the new project wizard, ADT creates the following
Folders and files in your new project:

src/
Includes your stub activity Java file. All other java files for your application
Go here.
<Android Version>
/

(E.g ., Android 1.1/
)
Provided des android.jar
File that your application will build against.
This is determined by the build target that you have chosen in New project
Wizard

.
gen/
This contains the java files generated by ADT, such as your R.java
File
And interfaces created from aidl files.
assets/
This is empty. You can use it to store raw asset files. See
Resources and assets
.
res/
A folder for your application resources, such as drawable files, layout files, string
Values, etc. See
Resources and assets
.
AndroidManifest.xml
The android manifest for your project. See
The androidmanifest. xml
File
.
default.properties
This file contains project settings, such as the build target. This files is integral
To the project, as such, it shoshould be maintained in a source revision control system.
It shoshould never be edited manually-to edit project properties,
Right-click the project folder and select "properties ".
Running your application

Before you can run your application on the android emulator,
YouMust
Create an android Virtual Device (AVD ).
An AVD is a configuration that specifies the Android platform to be used on the emulator.
You can read more in the android virtual
Devices
Document, but if you just want to get started, follow the simple guide below
Create an AVD.

If you will be running your applications only on actual device hardware, you do not
Need an AVD-see
Developing on a device
For information
On running your applicaiton.

Creating an AVD

With ADT 0.9.3 and above, the android SDK and AVD manager provides a simple graphical interface
For creating and managing avds. (If you're using ADT version 0.9.1 or older, you must
Useandroid
Tool to create your avds-read the AVD guide
Creating an AVD
.)

To create an AVD with the AVD Manager:

  1. SelectWindow> Android SDK and AVD Manager
    , Or click the android SDK and AVD manager icon (a black device)
    In the eclipse toolbar.
  2. In the virtual devices panel, you'll see a list of existing avds. ClickNew
    To create a new AVD.
  3. Fill in the details for the AVD.

    Give it a name, a platform target, an SD card image (optional), and
    A skin (hvga is default ).

  4. ClickCreate AVD
    .

Your AVD is now ready and you can close the AVD manager.
In the next section, you'll see how the AVD is used
When launching your application on an emulator.

For more information about avds, read
Android Virtual Devices
Documentation.

Running your application

Note:
Before you can run your application, be sure that
You have created an AVD with a target that satisfies your application's build target.
If an AVD cannot be found that meets the requirements of your build target, you will see
A console error telling you so and the launch will be aborted.

To run (or debug) your application, selectRun
>Run
(Or
Run
>Debug
) From the eclipse Main Menu. The ADT plugin
Will automatically create a default launch configuration for the project.

When you choose to run or debug your application, eclipse will perform the following:

  1. Compile the project (if there have been changes since the last build ).
  2. Create a default launch configuration (if one does not already exist for the Project ).
  3. Install and start the application on an emulator or device (based on the deployment target
    Defined by the run configuration ).

    By default, Android Application run configurations use an "Automatic Target" Mode
    Selecting a device target. For information on how Automatic Target mode selects
    Deployment target, see automatic and manual
    Target Modes
    Below.

If debugging, the application will start in the "waiting for Debugger" mode. Once
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.

Creating a run Configuration

The run configuration specifies the project to run, the activity
To start, the emulator options to use, and so on. When you first run a project
AsAndroid 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:

  1. Open the run Configuration Manager.

    • In eclipse 3.3 (europa), selectRun
      >
      Open Run Dialog
      (OrOpen debug Dialog
      )
    • In eclipse 3.4 (Ganymede), selectRun
      >
      Run deployments
      (Or
      Debug configurations
      )
  2. ExpandAndroid Application
    Item and create a new
    Configuration or open an existing one.

    • To create a new configuration:

      1. SelectAndroid Application
        And clickNew Launch Configuration
        Icon above the List (or, right-clickAndroid Application
        And click
        New
        ).
      2. Enter a name for your configuration.
      3. In the android tab, browse and select the project you 'd like to run with
        Configuration.
    • To open an existing configuration, select the configuration name from the list
      Nested belowAndroid Application
      .
  3. Adjust your desired launch configuration settings.

    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 cocould 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.

Automatic and manual target modes

By default, a run configuration usesAutomatic
Target mode in order to select
An AVD. In this mode, ADT will select an AVD for the application in the following manner:

  1. If there's a device or emulator already running and Its AVD Configuration
    Meets the requirements of the application's build target, the application is installed
    And run upon it.
  2. If there's more than one device or emulator running, each of which meets the requirements
    Of the build target, a "device chooser" is shown to let you select which device to use.
  3. If there are no devices or emulators running that meet the requirements of the build target,
    ADT looks at the available avds. If one meets the requirements of the build target,
    The AVD is used to launch a new emulator, upon which the application is installed and run.
  4. If all else fails, the application will not be run and you will see a console error warning
    You that there is no existing AVD that meets the build target requirements.

However, if a "preferred AVD" is selected in the run configuration, then the application
WillAlways
Be deployed to that AVD. If it's not already running, then a new emulator
Will be launched.

If your run configuration usesManual
Mode, then the "device chooser"
Is presented every time that your application is run, so that you can select which AVD to use.

Signing your applications

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
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
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, youMust
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.

Eclipse tips executing arbitrary Java expressions in eclipse

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, enteringandroid.os.Debug.startMethodTracing()
Will
Start dmtrace.

Open a code execution window, selectWindow
>Show
View

>Display
From the main menu to open
Display window, a simple text editor. type your expression, highlight
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 ".

Running ddms manually

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
).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.