Starting an app in debug mode from another app - android

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.

Related

How to Attach debbuger to process from the first line without compile code every time

I'm trying save compiling time only for debug first app lines code.
There is solution to exit by backPress() and the process still running (and then i can attach debugger to process), but my debug scenario need to check app from start so i need to kill process before debug, which prevent me to attach debugger unless otherwise i'm compiling the app in debug mode.
Does any one know how to prevent compile in debug only for that?
go to settings -> Developer options -> Select app to be debugged -> choose your app
turn on 'Wait for debugger'
then when you launch the app you see this screen:
Now app wait for you to attach debugger and auto start when it's attached
It is actually start the app-process without start launcher activity

Android Studio: launch an activity of another app when debugging

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.

How to automatically connect the Eclipse debugger when the android app is restarted on the device?

I am using Eclipse. I have a breakpoint on onCreate that I want to catch in debug mode when my application is restarted by android because of multitasking. I can connect the Eclipse debugger to my restarted app on the DDMS perspective, but I am never fast enough to catch the breakpoint in onCreate. How to automatically connect the Eclipse debugger when the android app is restarted on the device?
Run the app in debug mode. Right click on project there will a option Debug as below Run as.
Run the app in debug mode. But don't forget to add break point first before running app in debug mode
is restarted by android because of multitasking
Not sure what you really mean by this, but if this imprecise description refers to asynchronous operations (i.e. using AsyncTask) then you may want to add this line before your breakpoint set in code that is run on other than main task:
Debug.waitForDebugger();
Please note that to make it work you need to launch your app in debugging mode (F11)

Android: how to debug app's start-up process

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.

Attach Eclipse debugger to restarted application

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.

Categories

Resources