Android official documentation: Use eclipse to debug Android source code

Source: Internet
Author: User
Tags stack trace

Official Document link: http://source.android.com/source/using-eclipse.html

 

 

This document will help you set up the Eclipse IDE for Android platform development.

Note: If you are looking for information on how to use eclipse to develop applications that run on Android, this is not the right page for you. you probably wocould find the eclipse page on developer.android.com more useful.

Basic setup

First, it's important to make sure the regular Android development system is set up.

cd /path/to/android/root make

Important: You will still be usingmakeTo build the files you will actually run (in the emulator or on a device ). you will be using eclipse to edit files and verify that they compile, but when you want to run something you will need to make sure files are saved in eclipse and runmakeIn a shell. The eclipse build is just for error checking.

Eclipse needs a list of directories to search for java files. This is called the "Java build path" and can be set with.classpathFile. We have a sample version to start you off.

cd /path/to/android/root cp development/ide/eclipse/.classpath .chmod u+w .classpath

Now edit that copy.classpath, If necessary.

Increase eclipse's memory settings

The android project is large enough that eclipse's Java VM sometimes runs out of memory while compiling it. Avoid this problem by editingeclipse.iniFile. On Apple OSX the eclipse. ini file is located

/Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini

Memory-related defaults (as of eclipse 3.4 ):

-Xms40m -Xmx256m -XX:MaxPermSize=256m

Recommended settings for Android development:

-Xms128m -Xmx512m -XX:MaxPermSize=256m

These settings set eclipse's minimum Java heap size to 128 MB, set the maximum Java heap size to 512 MB, and keep the maximum permanent generation size at the default of 256 MB.

Now start Eclipse:

eclipse

Now create a project for Android development:

  1. If eclipse asks you for a workspace location, choose the default.

  2. If you have a "welcome" screen, close it to reveal the Java perspective.

  3. File> New> JAVA Project

  4. Pick a project name, "android" or anything you like.

  5. Select "Create project from existing source", enter the path to your android root directory, and click Finish.

  6. Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner .)

Once the project workspace is created, eclipse shoshould start building. in theory, it shocould build with no errors and you shocould be set to go. if necessary, uncheck and re-check Project Build automatically to force a rebuild.

Note:Eclipse sometimes likes to addimport android.RStatement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. this will cause your make to break. look out for these erroneous import statements and delete them.

When you sync

Every time you repo sync, or otherwise change files outside of eclipse (especially the. classpath), you need to refresh eclipse's view of things:

  1. Window> show View> Navigator

  2. In the navigator, right-click on the project name

  3. Click Refresh in the context menu

Adding apps to the build path

The default.classpathCould des the source to the core system and a sample set of apps, but might not include the particle app you may want to work on. to add an app, you must add the app's source directory. to do this inside Eclipse:

  1. Project> Properties

  2. Select "Java build path" from the left-hand menu.

  3. Choose the "Source" tab.

  4. Click "add folder ..."

  5. Add your app'ssrcDirectory.

  6. Click OK.

When you're done, the "source folder" path in the list shold look like

android/packages/apps/YOURAPP/src

Depending on which app (s) You include, you may also need to includeothersrc/main/javaDirectories underandroid/dalvik/libcore. Do this if you find you cannot build with the default set.

Eclipse formatting

You can import files indevelopment/ide/eclipseTo make eclipse follow the android style rules.

  1. Select WINDOW> preferences> JAVA> code style.

  2. Use formatter> Import to importandroid-formatting.xml.

  3. Organize imports> Import to importandroid.importorder.

Debugging the emulator with eclipse

You can also use eclipse to debug the emulator and step through code. First, start the emulator running:

cd /path/to/android/root . build/envsetup.sh lunch 1    make       emulator

If the emulator is running, you should see a picture of a phone.

In another shell, start ddms (the Dalvik debug manager ):

cd /path/to/android/root ddms

You shoshould see a splufty debugging console.

Now, in eclipse, you can attach to the emulator:

  1. Run> open debug dialog...

  2. Right-click "remote Java application", select "new ".

  3. Pick a name, I. e. "Android-Debug" or anything you like.

  4. Set the "project" to your project name.

  5. Keep the host set to "localhost", but switch port to 8700.

  6. Click the "debug" button and you shoshould be all set.

Note that port 8700 is attached to whatever process is currently selected in the ddms console, so you need to sure that ddms has selected the process you want to debug.

You may need to open the debug perspective (next to the "Java" Perspective icon in the upper-right, click the small "open perspective" icon and select "debug "). once you do, You shocould see a list of threads; if you select one and break it (by clicking the "pause" icon), It shocould show the stack trace, source file, and line where execution is. breakpoints and whatnot shoshould all work.

Bonus Material

Replace CTRL with the apple key on Mac.

Shortcut Function
CTRL-shift-o Organize imports
CTRL-shift-T Load class by name
CTRL-shift-R Load non-class resource by name
Ctrl-1 Quick Fix
CTRL-e Recently viewed files
CTRL-space Auto complete
Shift-alt-R Refactor: Rename
Shift-alt-V Refactor: Move
Eclipse is not working correctly, what should I do?

Make sure:

  • You followed the instructions on this page precisely.

  • Your problems view doesn' t show any errors.

  • Your application respects the package/directory structure.

If you're still having problems, please contact one of the android mailing lists or IRC channels.

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.