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.
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
I have an Android app that needs to perform a certain task one time after it's installed, and then never again. It uses SharedPreferences to record that it's performed the task successfully.
In order to test this behavior, I run the task via Android Studio on a real device, connected over USB. The problem is, every time I run the app via Android Studio, it reinstalls. As such, it seems to lose its persistent memory (SharedPreferences).
It makes sense that the app's SharedPreferences should be wiped when it's reinstalled. But how do you run an app in the Android Studio debugger without reinstalling it and wiping the persistent memory? I looked at the Android docs for running an app from Android Studio, and from the command line, but none of them seemed to offer a way to run it with a debugger connected without reinstalling.
One option I see to accomplish this would be by closing and re-launching the app from the device itself, and then attaching the debugger in Android Studio to the app's process. But this would mean the debugger misses the first few seconds or so of the process's activity. My app's task gets done in the first microseconds. I should still be able to view the logs from those first few seconds, but not to have full debugger control.
Any ideas?
if you directly run app from android studio from same machine it will not reinstall it updates the app so your sharedpreference will not wiped it remain same. For more info about sharedpreferances http://developer.android.com/reference/android/content/SharedPreferences.html
for debug from first step you can use debug option which run app directly in debug mode (shift+f9)
https://developer.android.com/tools/debugging/debugging-studio.html
This has to be a dumb question, but in Android Studio 0.3.2 I'm attempting to stop my app at a particular spot so I can test a particular automatic resume feature the next time I restart the app (to imitate a failure). However, when I hit the stop button in the debugger, it just disconnects and the app keeps on running. Am I misunderstanding what the stop button does? Is there any way to legitimately kill the app from the debugger like you can for iOS apps in XCode?
The stop button simply disconnects the debugger from your device.
To accomplish what you are trying to test you will have to press the home button which will go through the activity life cycle until onStop(). When you open your app again it will onStart() then onResume().
If you need more assistance just let me know.
Android Studio and Android uses different approach than Xcode and iOS
Android Studio stops debugger instead of application that is why you should manage application/activity/fragment lifecycle on OS level. For example you can open another app or press home button or navigate to another screen
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.