Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug app when it's killed

My app has a bug on certain activities: when it restarts (when it's killed and then opened again with multitasker), it crashes. I assume it has something to do with my singleton, but I'm not sure. I'd like to pinpoint the problem, but I can't debug since the debugger stops when I kill the application (I'm using adb shell am kill <package-name>).

Does anyone know how I can debug this problem? (Or give me a hint what my problem might be for that matter)

like image 972
Frederick Eskens Avatar asked Apr 21 '16 08:04

Frederick Eskens


2 Answers

You can add the following code to the onCreate() method of your main activity:

android.os.Debug.waitForDebugger();

This will pause your app till a debugger is attached to the process.

Then :

  • Run your app
  • Put your app in background (say, with the home button)
  • Kill the process or use an app like MemoryPump to fill the RAM and have your app killed by Android
  • Switch back to your app with the multitask pan (the app will start and wait for the debugger)
  • Attach your android studio debugger to you app (icon in the Android Studio toolbar somewhere between the 'run app' icon and the 'stop application' icon)
  • Debug your app
like image 73
Christophe Maillot Avatar answered Sep 21 '22 02:09

Christophe Maillot


I would use DDMS (Dalvik Debug Monitor Server (DDMS) to capture the stack trace, etc.. after your kill and restart our app.

(FYI: It is already installed along with the rest of the Android toolset)

While it is not going to allow you to debug you app via VS/XS, it can help you pin the area of your app that is the cause. From there I would add logging around the trouble area (i.e. the old-fashion printf style debugging)

enter image description here

http://developer.android.com/tools/debugging/ddms.html

To launch from Visual Studio:

enter image description here

From Xamarin Studio:

enter image description here

like image 23
SushiHangover Avatar answered Sep 20 '22 02:09

SushiHangover