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, entering android.os.Debug.startMethodTracing() will start dmTrace.

Automatic and manual target modes

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:

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 will always be deployed to that AVD. If it's not already running, then a new emulator will be launched.

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.




(java.lang.String) /work/device/out/linux-x8 6-debug/android/app/android


(boolean) true


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

0 0

Post a comment