Android: how to debug app's start-up process
source link: https://stackoverflow.com/questions/16929391/android-how-to-debug-apps-start-up-process
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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.
For Android Studio, here is what worked for me:
android.os.Debug.waitForDebugger();
Where you want to start debugging.
- Then add a breakpoint just after it in your code
- Compile your app and pass it to your device
- Restart your device
Once it's up, attach the debugger:
Start debugging
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:
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.
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.
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.
Your Answer
Post as a guest
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK