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?
4 Answers
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.

- 6,878
- 2
- 21
- 17
-
Exactly what I wanted. I needed to have the debugger attached to an app before it starts, when invoked from Google Assistant. – Chad Mx Dec 15 '20 at 03:26
-
For Android Studio, here is what worked for me:
- Add
android.os.Debug.waitForDebugger();
Where you want to start debugging.

- 2,562
- 1
- 31
- 58
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
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:
Attach the debugger, by pressing the Attach Debugger to Android Process button
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.
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.

- 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
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.

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