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?
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:
Add
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
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.
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.
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.
Related
I am launching App B from App A with intent. I want to see debug mode logs in App B. If I launch App B directly I can do it. But if I have to launch it from App A, I cannot use debug. Is there a workaround for this.
To see logs:
Make sure you have Logcat opened and go to combo to switch between your debug apps are running currently to see each app's logs.
To enable debug mode:
You need to have both projects opened together and once you have launched App-B from App-A as you are doing, when App-B will be opened, you can attach debug mode from "Attach Debugger to Android Process" button on its own "Android Project B":
Tested on Android Studio 3.4
If it is not this, maybe we need more details...
There are two ways to do this. The official way is to set your breakpoint and then use this in your onCreate method:
Debug.waitForDebugger();
This pauses the app until a debugger is attached, at which point it will hit your breakpoint.
Alternatively you can put a sleep timer in your onCreate method, (e.g. TimeUnit.SECONDS.sleep(30)), which pauses your app long enough for you to attach a debugger.
I am debugging app with cordova and I have always wondered if there was a way to launch googles device inspect as soon as the app starts running on the device.
This screen is familier to most but just wondering if there is a more efficient way of opening the inspect for the web-view without having to wait for app to install then launching the inspector and lastly hitting refresh to get the get the network information.
So some call back like:
cordova run --device OS --launch device-inspect
GapDebug, which seems to be merely a wrapper around chrome inspect, promises a feature like this:
GapDebug detects an app termination and restart, such as during an app update, and automatically reconnects the app with the previous debug session. Quickly get back to debugging when the app closes on your device.
Taken from their features site...
To my knowledge there isn't.
If you make changes to the code you have to compile and install the app on your device or else you won't have the most recent build.
And if I'm not mistaken the way "Inspect" works is by attaching to a running process and you can only start to "listen" after the process has already been created.
Or as #Phonolog sugested, use another debugger
Let's say I have two apps in an Android Studio project: "S_App" is an app that exposes a service, but doesn't have any activities and no GUI. "C_App" is a client app with a GUI that starts the service in S_App.
I want to debug S_App, but I don't know how to do so directly in Android Studio. Since the app itself doesn't do anything and has no GUI, I would need to start C_App to make it do something. However, if I "debug C_App" from Android studio then only C_App is attached to the debugger, but not S_App, which will consequently not hit any breakpoints.
I know you can manually attach a debugger to a process, but that is tedious to do every single time. Isn't there a way I can configure Android Studio so that it attaches the debugger to S_App but actually launches an Activity in C_App? I saw in the "edit configuration" dialogue you can choose a custom activity to launch, but this is strictly limited to activities within the app you're debugging apparently.
Ok, I made an app for Android. And when we push the middle button or back(lefthandside) button of Android phone, we all know that Android apps still runs in the background.
So my code is this:
the first line(addEventListener) is in a private function which runs as soon as you open the app.
NativeApplication.nativeApplication.addEventListener(flash.events.Event.EXITING, onMyAppExit)
private function onMyAppExit(event:flash.events.Event):void{
trace("onMyAppExit is running");
saveProgress();
}
Basically, I want saveProgress() to run when the app ACTUALLY exits from running in the background. I noticed that my app actually exits when I open another app like Candy Crush. I guess the Android OS exits apps automatically when the apps are not being used and when the app you are using takes a lot of RAM. However, my code only works when I run my app in AIR Debug Launcher(Mobile). I know that because I see the trace in the function in my output window when I click on the x button on the right corner of the app window. But when I connect my Android phone to the computer and then ---> AIR3.8 for Android settings ---> Publish, and then I "Begin Remote Debug Session", and I open my app first, then open Candy Crush so that the Android OS automatically exits my app, I don't see the trace. So I finalized that the code didn't work on my phone.
I think you should use the event Event.DEACTIVATE,
when the app go to background you can save all so the os can kick your app off it's still safe :)
With that you have the event Event.ACTIVATE when the app go back on the foreground so you can handle it to revive your level.
I am testing the onsave/onrestore mthods of my android application.
To do this I phone my device and see that it kills the process and then I hang up. :)
I see that it restarts the application.
Question is: how do I cause it to restart in debug mode so I can step through the restore process?
Is there a way to tell it to automatically attach to the debugger when starting up?
Use android.os.Debug.waitForDebugger();
Basically, start via debugging. Exit out of your app. Set some break points. Enter back into your app (make sure that this line is hit, so put it in your onCreate or somewhere else) and it will re-attach to the running debugger.
I don't think there is a way to ensure the app restarts in debug mode. But if you are debugging your own app and don't mind adding debug code for testing you might want to add a Thread.sleep(5000) or something like this at an appropriate place in your startup methods. This should give you enough time to reconnect the debugger via the DDMS. Remove when you are done, of course ;)
There is a configuration option in Android settings > developer options > debugging > select app to be debugged. What it does is calling the eclipse debugger every time certain application is opened, if it's connected to the adb and the app's project is open.
Programmatically: Use waitForDebugger(). Documentation here.
Note that the method returns as soon as the debugger attaches, so it's best practice to place a breakpoint right after that call. Additionally, you can test the debugger attachment status using isDebuggerConnected().
In Eclipse: Open the DDMS perspective of eclipse, select the freshly-restarted app on your device, and then select the debug option. This will attach the debugger to the restarted instance.
On the Device: There is a configuration option under some* handsets that allows you to select an app to be debugged when USB debugging is configured. It's under the Developer Options in your device settings. This will attach the debugger automatically.
*For example, my Galaxy S4 has it, my HTC Rezound does not. I believe it might be a Jelly Bean specific option.