Debug Android Application on Real Device in Eclipse
Debug Android Application on Real Device in Eclipse
Here we will learn about how to Debug Android Application on Real Device in Eclipse. Here we will learn step by step procedure for how to debug an android program or application on a real Android device (Mobile phone/Tablet) or Emulator. For Android developers, Android Application/Program debugging is the most useful and important factor to verify and fix issues for an android application. We will learn about below details.
Debugging -Debug Android Application on a Real Device (Phone)
Debugging -Debug Android Application on a Emulator
Step by Step Debugging for Android Application Using Eclipse
How to Use Break Point in Android Application Debugging.
Inspect Variables and its Values at Runtime Debugging in Android.
Tools and Environment Required for Debugging an Android Application
- Eclipse Editor with ADT Plugin (I am using Eclipse Galileo); we can use any Eclipse version.
- Any Android Device (I am using Samsung Galaxy S), we can use any device.
- Supported USB driver must be installed for your Android device before start debugging.
- OS: Windows 7/XP/Windows 8 (I am using Windows 7), we can use any OS.
Steps to Debug an Android Application on a Real Android Device (Mobile Phone)
With an Android-powered device, we can develop and debug our Android applications just as we would on the emulator. Before you can start, there are just a few things to do:
1. Enable USB Debugging in Android Phone
First you need to turn on USB Debugging in your device. To turn on the USB debugging in the android device (Mobile Phone), Go to the below path and select the check box as shown below image. Path: Settings -> Applications -> Development -> USB debugging Note:
- On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.
- On Android 4.0 and newer, it’s in Settings > Developer options.
- On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.
2. Connect Your Android Device (Mobile Phone/ Tablet) to Your PC.
- At first your Device should be recognize on your PC. So first Connect your Device to your PC, then wait for 2/3 minutes. To verify the device connection you can use command: adb devices.
To verify the connection, open command prompt. Then in command prompt, go to your Android SDK path (in my case, The Android SDK path in my PC is “E:\Android\android-sdk-windows\platform-tools”), then type the command: adb devices Then if you will see command output like below List of devices attached M110d45688dd device Note: Here “M110d45688dd” device is nothing but the attached Samsung Galaxy S. Note: If your Android Device is not recognized on your PC. Then you can’t see the above output. That means proper USB device driver for your device has not been installed on your PC. So you need to install the USB driver for your device first. You may find the USB driver in the official site of your android phone manufacturer. Once the USB driver is installed, you can follow above steps.
- Open your Eclipse IDE. If your device is recognized in your PC, then you can also verify the same in Eclipse editor using DDMS option. The device will be listing in the left hand panel of DDMS. To see the device connection in Eclipse follow below steps in Eclipse. This step is optional, so you can also omit this step. This is just for your knowledge.
Path: Eclipse->DDMS. Note: If you are getting any error while connecting your android device, then you might like to follow my article ‘Some Common Android Development Errors with Solutions‘.
3. Declare Your Application as “debuggable” in Your Android Manifest File
When using Eclipse, you can skip this step, because running your app directly from the Eclipse IDE automatically enables debugging. In the AndroidManifest.xml file, add android:debuggable=“true” to the <application> element. You can add this line to your android project. Note: If you manually enable debugging in the manifest file, be sure to disable it before you build for release (your published application should usually not be debuggable). Here is AndroidManifest.xml for our attached ‘HelloWorld’ example.
<?xml version="1.0" encoding="utf-8"?>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<uses-sdk android:minSdkVersion="8" />
4. Deployment Target Selection Mode in Eclipse
Note: Most people are stuck in this stage, You might face this issue while debugging, so follow below steps. Issue: Previously, you may created few “Android Virtual Devices (AVD)” for testing, and set “Deployment Target Selection Mode” to “Automatically“, however, this will caused the application failed to debug on your connected device as this option will start the AVD emulator automatically. Solutions:
- In Eclipse right click on Android project, select “Run” -> “Run Configuration” -> “Android Application” -> “Target” tab -> “Deployment Target Selection Mode” -> set it to “Manual“, and you can select the device at runtime. Please Note: The steps might be different in your case as it depends upon the Eclipse version you are using. Some case it might be like: In Eclipse right click on Android project, select “Run As” -> “Run Configuratios…” -> “Android Application” -> “Target” tab -> “Deployment Target Selection Mode” -> set it to “Manual“
- Alternative, in “Deployment Target Selection Mode“, just deselect all the selected AVD.
- Press Apply and close the dialog for now. For more details please follow below image.
5. Setup Break Points in Android Code in Eclipse
For debugging an Android Application, if you want to stop the debugger anywhere in the code at runtime , then you need to set break points in the code. For example, we will put a break point at the java code line after the button click of ‘HelloWorld’ application as below. So when we will debug the application on device, the debugger will automatically break at that line where we set the break point. It will wait for the next execution until you resume the debugging or do next step by step debugging. To put a break point, Just double click on the left side bar at the line where you want to set the break point. Kindly See below image for more clarity. I have rounded in Red color border, where I have set the break point for debugging. You can also use key board shortcut.
6. Start Debugging Your Android Application Now
After inserted the break point at the line where you want to break the execution flow of your Android application, now it is time to start debugging. To start debugging, you can click on the debug option in Eclipse as below image or you can directly press ‘F11’ from your key board. Note: The option might be different. But don’t worry. I will show the path where you will find the Debug option. Goto: Eclipse->Run->Debug. Also you can click on the debug option button as below image. You can start debugging either way. Figure: Start Debugging from Debug Option Button Figure: Start debugging from Debug Option from Menu When you start the debugger, Eclipse shows you to switch to the Debug perspective once a stop point is reached. Press Yes in the corresponding dialog. Debug Options for Debugging Android Application The following picture displays the buttons and their related keyboard shortcuts.
7. Inspect Variables and its Values at Runtime
Once you start debugging, the debugger will stop and wait for your input at the line where you put the break point. An arrow will be display at the break point line. You can move the mouse cursor on any variable for which you want to inspect the values at run time. Also you can follow below steps for inspect value of any stack variables. You can also change value of a variable at Runtime. The Variables View shows fields and local variables from the current executing stack. If you are not able to see the below Variable View, then add it. Please note you need to run the debugger to see the variables in this view. For example we have used a Integer variable called ‘i’. we can see the runtime value and change the value of variable ‘i’ at runtime while debugging. Follow below image for more clarity. In the above you can change the value (3) of Integer variable ‘i’ to any other Integer value. It will reflect at the same time with out restart the application. Note: You can insert multiple break points while debugging Android application. Try to play with other debugging options and learn more about android application debugging and share your knowledge with us.
Debugging -Debug Android Program/Application on a Emulator
For Debugging on Emulator, The steps are almost same as real device debugging. Just omit Step-4 from above and follow other steps. That’s it.
Download android source code for debugging from here.
I hope this small tutorial will help you at its best.