How To Debug Android Widgets

For most new android app developers (like me), it is a bit puzzling how to debug the android widgets or how to put a breakpoint in an android widget code. For normal applications, it is quite simple as when you press “debug” on the eclipse toolbar, it launches the emulator (or connects to existing emulator/device), syncs your application and launches its main activity and puts the control in your hands if you have put a breakpoint. For widgets, it seems a bit tricky because there is no activity to be launched, so eclipse just syncs your widget’s apk and installs it and that’s it. So, is it possible or not to debug widget code? and if yes, then is it very difficult? Fortunately, it is possible and very easy to do but just slightly less intuitive and I couldn’t find any information about it at Android developers website and learnt it by just fumbling around for a few minutes so thought of posting here for the benefit of other newbies like me.


So, all you need to debug your widget code is almost same as what you do for normal applications. Just follow the below steps:

1. Press “debug” on the eclipse menu (or “run” it doesn’t seem to matter)

2. Once the widget apk is sync’ed and installed onto your emulator/device, switch your eclipse workspace to DDMS mode. You can either do this by pressing the “DDMS” labeled button on your top right corner or if you can’t find it, then do it by going to “Window->Open Perspective->DDMS”.

3. Select the process name of your widget from the list of processes shown. By default, this list appears at top left of DDMS window. (See screenshot below). If you can’t see your widget’s process name in the list, it is possible that the widget is not added to the home screen yet. So, do so.

4. Press the green debug button above the process list (See screenshot below)

5. And that’s it. Now, if you had put a breakpoint in the code, do something that executes that piece of the code.

Screenshot to show how to debug android widgets in eclipse

Click To Enlarge

Let me know if this helped you out or if you see anything that I missed out. Happy coding :)

14 comments to How To Debug Android Widgets

  • Thanks brother! I can’t press Run or Debug because my Widget project contains no default activity. I am having to install (and reinstall a million times) the APK through the Device manager and then I can effectively get to the debug you mention in the DDMS view. How are you able to press run on your Widget project?

    Peace,
    Scott

  • @Scott: Well, I run one of my activities when my widget is touched, so I do that. Otherwise I also noticed that the widget update is called if you press the home key, so you can try that as well..

  • Scott

    Thanks brother! Do you understand the Intent/PendingIntent? When I add an “extra” (the widget id) to an intent wrapped in a PI attached to a widget instance, it appears all the widget instances share the last widget’s id. Are these intents shared or something?

    Scott

  • Actually the pending intent would get delivered to your widget’s underlying activity through onReceive() and then you have the option whether you want to update all instances, or just a particular one. Or do you mean that the widget id that you receive from extras in onReceive is always same, irrespective which widget the intent originated from? In that case, there might be a logical issue in your program where you are filling in the intent. It works fine for me.. I’m not an expert but If you want, I can take a look at the code if you can send the relevant snippets through the contact form..

  • Lakshmana

    Hi
    I did what you suggested. But pressing the green bug in DDMS is not doing anything for me. My widget does not have a activity. Only a service. How to debug.

  • Lakshmana

    It works now. But only when I include an activity in the widget and I put a breakpoint in the activity code. I cannot achieve a breakpoint in the onupdate() and the service.

  • Great tip, thanks! I’d never have figured that out.

  • Omkar S. Bapat

    Thanks a ton bro! Really helped a lot.

  • Roey

    Hi,

    Thanks….. i have been strugeling with that few hours.
    Great Tip.

  • Sergii

    Thanks a lot! It helps me find my stupid error :)

  • Marol

    i just confused where is the error if i press DDMS??can anybody tell me?

  • Jeyankondan

    Thanks a lot…

  • [...] Debugging Android Widget Posted in Uncategorized by jeansworld on October 20, 2010 Debugging your widget is not quite as automated as debugging a regular application, but luckily it’s not too difficult either. You just have to know what to do…so here are the instructions, from Shantanu’s Technophilic Musings [...]

  • [...] How To Debug Android Widgets通常のAndroid Appではeclipseの虫ボタンを押すだけでデバッグが始めれるのですが、Widgetの開発では、デバックする方法が簡単には見つからない・・・。ってことでグーグル先生に相談してみました。んで、見つけたサイトが冒頭のリンクです。簡単にその内容の説明。 [...]

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

SUBSCRIBE!





Tweet