12

I am investigating some issues which happen during my app's startup process, but DDMS won't start the debug mode until the process has started, is there a way I can capture the events earlier?

androidkc
  • 689
  • 2
  • 7
  • 16

4 Answers4

19

I know that this is a couple of years late, but for any future searches:

Putting WaitForDebugger into your code is one way.

Unlocking developer options (by tapping on the build number in system information on the android device) in Settings allows us to select an application for debugging and then opt to wait for a debugger whenever the program is launched. This allows us do the equivalent of adding and removing WaitForDebugger without modifying and reinstalling the code each time.

Michael Hays
  • 6,878
  • 2
  • 21
  • 17
14

For Android Studio, here is what worked for me:

  1. Add

android.os.Debug.waitForDebugger();

Where you want to start debugging.

  1. Then add a breakpoint just after it in your code
  2. Compile your app and pass it to your device
  3. Restart your device
  4. Once it's up, attach the debugger:enter image description here

  5. Start debugging

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
3

Android can wait for the debugger to attach to your application before the app gets launched. This is a developer option called Wait for debugger.

Steps

  • Enable developer options (tap build version 7 times)

  • Enable USB debugging

  • Install your application onto your device using debug mode

  • In developer options: Press Select debug app and select the app

  • Enable Wait for debugger, as shown in screenshot: enter image description here

  • Launch your app:

    • e.g. If testing app launch from terminated state from a push notification, send that push notification to the device.
    • e.g. If testing app launch from Google Assistant, use the Google Assistant to trigger this.
  • The app would not launch yet, instead a dialog would show up: enter image description here

  • Attach the debugger, by pressing the Attach Debugger to Android Process button enter image description here

My situation

For anyone interested/ for my future reference: I wanted to debug my Android application receiving a push notification message when the app was in the terminated state. It was actually a Flutter app running on Android, so this is relevant for both Android and Flutter.

enter image description here

I have revoked the API key revealed in this GIF.

Tip

If Wait for debugger is enabled, sometimes you need to detach or close Android Studio's debugger and re-attach it if you want to handle a subsequent application launch successfully. Otherwise, the app would never launch.

Notice, I send a push notification from a device (left device, iOS), and the push notification causes the app to launch on the (right device, Android). Then I attach the debugger, and the program pauses at the breakpoint I set inside FirebaseMessagingReceiver.

I wrote another version of these steps here.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
  • I know that this only duplicates a much older answer (https://stackoverflow.com/a/30533066/334719), but that answer is woefully insufficient in detail. – Auspex Apr 15 '23 at 10:19
1

You should implement your own Application class which extends Application and override the methods onCreate and so.. . This class will be your starting point of your app.

also set it as your application in the manifest.

Libin
  • 16,967
  • 7
  • 61
  • 83
  • add there the `android.os.Debug.waitForDebugger();` Statement. like in @hiddeneyes02 -Answer – Samuel Jan 10 '23 at 12:58