page.title=Debugging Tasks @jd:body
This document offers some helpful guidance to debugging applications on Android.
The Android SDK includes a set of tools to help you debug and profile your applications. Here are some tools that you'll use most often:
adb logcat or, from DDMS, select Device > Run
logcat.
{@link android.util.Log} is a logging class you can use to print out messages to the logcat. You can read messages in real time if you run logcat on DDMS (covered next). Common logging methods include: {@link android.util.Log#v(String,String)} (verbose), {@link android.util.Log#d(String,String)} (debug), {@link android.util.Log#i(String,String)} (information), {@link android.util.Log#w(String,String)} (warning) and {@link android.util.Log#e(String,String)} (error). For example:
Log.i("MyActivity", "MyClass.getView() — get item number " + position);
The logcat will then output something like:
I/MyActivity( 1557): MyClass.getView() — get item number 1
Logcat is also the place to look when debugging a web page in the Android browser. All browser bugs will be output to logcat with the {@code WebCore} tag.
For more information about all the development tools provided with the Android SDK, see the Tools document.
In addition to the above tools, you may also find the following useful for debugging:
With the Dev Tools application, you can enable a number of settings on your device that will make it easier to test and debug your applications.
The Dev Tools application is installed by default on all system images included with the SDK, so you can use it with the Android Emulator. If you'd like to install the Dev Tools application on a real development device, you can copy the application from your emulator and then install it on your device using ADB. To copy the application from a running emulator, execute:
adb -e pull /system/app/Development.apk ./Development.apk
This copies the .apk file into the current directory. Then install it on your connected device with:
adb -d install Development.apk
To get started, launch the Dev Tools application and select Development Settings. This will open the Development Settings page with the following options (among others):
These settings will be remembered across emulator restarts.
If you're developing a web application for Android devices, you can debug your JavaScript on Android using the Console APIs, which will output messages to logcat. If you're familiar debugging web pages with Firefox's FireBug or WebKit's Web Inspector, then you're probably familiar with the Console APIs. The Android Browser (and {@link android.webkit.WebChromeClient}) supports most of the same APIs.
When you call a function from the Console APIs (in the DOM's {@code window.console} object), you will see the output in logcat as a warning. For example, if your web page executes the following JavaScript:
console.log("Hello World");
Then the logcat output from the Android Browser will look like this:
W/browser ( 202): Console: Hello World :0
Note: All Console messages from the Android Browser are tagged with the name "browser" on Android platforms running API Level 7 or higher and tagged with the name "WebCore" for platforms running API Level 6 or lower.
Not all of the Console APIs available in Firefox or other WebKit browsers are implemented on Android. Mostly, you need to depend on basic text logging provided by functions like {@code console.log(String)}, {@code console.info(String)}, {@code console.warn(String)}, and {@code console.error(String)}. Although other Console functions may not be implemented, they will not raise run-time errors, but will simply not behave as you might expect.
If you've implemented a custom {@link android.webkit.WebView} in your application, then in order to receive messages that are sent through the Console APIs, you must provide a {@link android.webkit.WebChromeClient} that implements the {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback method. For example, assuming that the {@code myWebView} field references the {@link android.webkit.WebView} in your application, you can log debug messages like this:
myWebView.setWebChromeClient(new WebChromeClient() {
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("MyApplication", message);
}
});
The {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} method will be called each time one of the Console methods is called from within your {@link android.webkit.WebView}.
When the "Hello World" log is executed through your {@link android.webkit.WebView}, it will now look like this:
D/MyApplication ( 430): Hello World
Note: The {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback method was added with API Level 7. If you are targetting platforms running API Level 6 or lower, then your Console messages will automatically be sent to logcat with the "WebCore" logging tag.
adb shell, use "ps" to find the process you
want, and then "kill -3 ". The stack trace appears in the log file.
adb shell logcat -b radio
Also see the Troubleshooting document for answers to some common developing and debugging issues.
DDMS will assign a specific debugging port to every virtual machine that it finds on the emulator. You must either attach your IDE to that port (listed on the Info tab for that VM), or you can use a default port 8700 to connect to whatever application is currently selected on the list of discovered virtual machines.
Your IDE should attach to your application running on the emulator, showing you its threads and allowing you to suspend them, inspect their state, and set breakpoints. If you selected "Wait for debugger" in the Development settings panel the application will run when Eclipse connects, so you will need to set any breakpoints you want before connecting.
Changing either the application being debugged or the "Wait for debugger" option causes the system to kill the selected application if it is currently running. You can use this to kill your application if it is in a bad state by simply going to the settings and toggling the checkbox.