Monday, February 1, 2010

Debugging Android applications

I’ve been playing around with Google’s Android mobile operating system for the past month as part of a post-masters project I’m doing with the university. Though there are tons of articles and tutorials to get things started at the official Android Developer Center, you can easily get lost when finding how to do simplest of tasks.

Knowing how to debug your mobile applications is one of the fundamental things you should know as an Android (or any) developer. I use Eclipse IDE for development purposes, and the document simply states that I can view log messages from the “Log Cat” view. Few minutes into the development, I noticed that sometimes my log statements are not displayed in the Log Cat view at all.

In this article, I am giving a step-by-step guide on how to start debugging an Android application using the Eclipse IDE, along with how to write bebug statements and view them during run-time using the Log Cat view.

How to write log statements

Android provides a well-defined logging mechanism regardless of the IDE you use. There are five logging levels, as defined in the API here.

/* * Define a TAG (Generally the name of the class file) used when * displaying the log messages to know where they come from. * * NOTE: Use the Class#getShortName() if you want the displayed * name to be shorter. */ private static final String TAG = MyClass.class.getName(); public void someMethod() { Log.v(TAG, "verbose message to log"); // A verbose message Log.d(TAG, "debug message to log"); // A debug message Log.i(TAG, "info message to log"); // An information message Log.w(TAG, "warning message to log"); // A warning message Log.e(TAG, "some error message to log"); // An error message }

Now that you have a rough idea of how to write log messages, let us have a look at how to view them.

How to view log messages

There are two main ways to view the log messages that get generated when you run your application (either using the emulator that comes with Android sdk or using an actual device).

  1. Using Eclipse IDE and its Log Cat view
  2. Using telnet to debug without the use of Eclipse

Figure 1 (a) : Selecting the DDMS perspective

I am focusing only on using Eclipse IDE for now, and will write small guide on how to use telnet for debugging at some point in future.

Viewing log statements from Eclipse
  1. Open the DDMS perspective from Eclipse.
    • Select the DDMS option by navigating through Window –> Open Perspective –> Other … menu, as shown in Figure 1 (a).
    • This will bring take you to a new perspective shown in Figure 2. This perspective allows you to navigate the file structure, simulate location coordinates and more!
    • You can add this new perspective to your eclipse toolbar for easy access, by selecting DDMS by clicking on the “plus” icon shown in Figure 1 (b).

      Figure 1 (b) : Adding DDMS perspective

  2. Select the device from the devices panel, by left clicking on the device name (see Figure 2).
    • This action forces Eclipse (or rather, the ADT plugin for Eclipse used for Android development) to associate the Log Cat view with the selected device. From my personal experience, it seems like the plugin sometimes fail to make this happen automatically.

      Figure 2 : Selecting the device from the DDMS perspective

    • Figure 3 : Selecting the Log Cat view

  3. Open the Log Cat view in Eclipse
    • Select Log Cat view by navigating through Window –> Show View –> Other ... menu, as shown in Figure 3.
    • You will see a new pane been added to your views panel, which has an icon of letter “a” in a yellow background as shown in Figure 4.
    • The buttons to the right (V, D, I, W, E and clear etc) allows you to filter what types of log messages you want to view, as well as clear the log from the display menu. In this example, I’ve selected to view only warning messages and above (warnings displayed in orange colour, and errors in red).
  4. Run the application
    • Once the application is started, you will see some log messages starting to appear on the Log Cat view (Figure 4)

        Figure 4 : Viewing the log messages from the Log Cat view

Hope this helps to get you started with debugging Android applications!

Useful Links
  • API for Log class: http://developer.android.com/reference/android/util/Log.html
  • More information about Emulator: http://developer.android.com/guide/developing/tools/emulator.html
  • Download Eclipse: (Download the Eclipse for Java Developers) http://www.eclipse.org/downloads/
  • Download Android SKD:  http://developer.android.com/sdk/index.html

[Via http://thiranjith.wordpress.com]

No comments:

Post a Comment