Android Instant Apps: How to debug an instant app? - android

When I debug my instant app on Android Studio, the debugger attaches to instant app process and pauses execution at most breakpoints. However it seems to ignore breakpoints at my main activity's onCreate method. I've tried "Debug" and "Attach Debugger to Android Process" options. What am I missing?

Basic information about how to use Android Studio debugger to debug an Android app is available at Developer Documentation Debug Your App
.
Android Studio debugger works normally most of the time when debugging an instant app. However, you will notice that the debugger will fail to stop at breakpoints early in the app's lifecycle (such as Application.onCreate or Activity.onCreate) on devices running Android N and below.
When your instant app is up and running, it runs under your app's package name. However, there is a short period of time during app startup when it runs under a temporary package name, in the following form:
com.google.android.instantapps.supervisor.isolated[0-9]+
This temporary name is assigned by the runtime. Because Android Studio is not aware of this name, the debugger will not attach to this process.
The workaround is to find out this temporary name and use adb to set the app to debug. This can be done by running the following command in terminal before running your app. It will print out the name when your app starts.
=> adb shell 'while true; do ps | grep com.google.android.instantapps.supervisor.isolated; sleep 1; done'
u0_i6 31908 630 1121664 29888 0 00ea57eed4 R com.google.android.instantapps.supervisor.isolated15
Once you identify the package name, use the following command which will pause and make your instant app process to wait for the debugger. Then attach the debugger normally, but choosing the temporary process name in the Choose Process window by clicking on “Show all processes”.
=> adb shell am set-debug-app -w --persistent com.google.android.instantapps.supervisor.isolated15

I had also problems recently debugging instant apps.
with multiple log messages
"Waiting for application to start debug server"
in the debug window and after several retrials
"Could not connect to remote process. Aborting debug session."
The way I solved the issue is
by using the "Attach To Process" option from Android studio "Run" menu.

Related

Android Studio Debugger process finished with exit code 127

Recently upgraded to Android Studio 4.2.1
I can launch my app successfully but when I try to attach the debugger I get the error in the question title (see screenshot below).
The app just gets stuck on the "Waiting For Debugger" screen.
Everything worked as expected prior to upgrade.
This is the output from the debug log
$ adb shell am start -n "com.*.*/com.*.*.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.*.* | com.*.*.test
Waiting for application to come online: com.*.* | com.*.*.test
Connected to process 9869 on device 'amazon-kfmuwi-G0W0X8089256FRU1'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.*.* is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
Connecting to com.*.*
Now Launching Native Debug Session
com.intellij.execution.ExecutionFinishedException: Execution finished
Process finished with exit code 0
In logcat I also see
2021-05-19 12:55:32.855 10394-10394/? E/azon.kindle.cm: Not starting debugger since process cannot load the jdwp agent.
I have tried:
Restarting Android device
Restarting development device
Invalidating and Restarting Android Studio
Reinstalling Android Studio
My system is running Pop!_OS 20.10
Debug error
I've solved that by installing 'libncurses5' and 'ia32-libs' packages
This is a known issue and is provided on the android developer's official page. Here is the link https://developer.android.com/studio/known-issues?hl=de#native_debugger_crashes_with_debugger_process_finished_with_exit_code_127
It turns out it's a Linux-specific issue and is because Linux uses upgraded 'libncurses6' and android studio uses 'libncurses5' required to run the debugger.
I've solved this by installing 'libncurses5' only
sudo apt install libncurses5
Thanks to Max Elkin
I eventually solved this by rolling back to Android Studio 4.1.
I rolled back AS from 4.2 to 4.1.3 which solved the error
In case the accepted answer doesn't help, here's how to find the root cause:
Search for the Debug log settings action (via CTRL + SHIFT + A)
Set #com.jetbrains.cidr.execution.debugger:trace in the dialog (source)
Try run or attach the debbuger
Search for the Collect Logs and Diagnostic Data action (via CTRL + SHIFT + A)
Inspect the idea.log file inside the zip. It should show why the debugger fails.
For example, I saw this error brains.cidr.execution.debugger - LLDBFrontend: Fatal Python error: config_get_locale_encoding: failed to get the locale encoding: nl_langinfo(CODESET) failed
Which was fixed by setting this in ~/.zshrc (source)
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

Stop persistent debug hook

I have a real Android device that I am using to debug. In order to attach Android Studio to my application, I ran this on the device:
adb shell am set-debug-app -w my.package.name
This worked fine. However, the problem is that now, whenever I try to open the application on my device, it will wait for the debugger to attach.
Waiting for Debugger
My Application (my.package.name) is waiting for the debugger to attach.
This happens even after I disconnect the device from my computer. Also, I restarted my device and nothing changed.
This is definitely not related to Android Studio, because my device is not connected to Android Studio, so any solution involving restarting Android studio would not work.
After looking at this page, I tried:
adb shell am force-stop my.package.name
This stops the application. But when I reopen it, it still waits for the debugger to connect.
So, any idea how I can remove this debug hook?
You can set/clear the debug app through Developer Options in your device settings.
If you want to do it through adb, it looks like you could do am clear-debug-app to clear it: http://androidxref.com/8.0.0_r4/xref/frameworks/base/services/core/java/com/android/server/am/ActivityManagerShellCommand.java#166
case "set-debug-app":
return runSetDebugApp(pw);
case "clear-debug-app":
return runClearDebugApp(pw);

How do I debug UIAutomator scripts with Eclipse

From what I can see, when a UIAutomator script is compiled into a jar file and it gets run by using the adb shell command. I can see that there is a -e debug command line option which waits for a debugger to connect before starting but how do I connect this to the debugger from Eclipse so I can debug my UI Automator script?
I've worked it out. The process is a little long winded but it works!
The key to this is understanding how to use the Dalvik Debug Monitor Server (DDMS) and understanding remote debugging with Java and Eclipse. In brief follow the following steps:
Set up a DDMS perspective in Eclipse by clicking Window > Open Perspective > Other... > DDMS. You should see your device listed in the Devices tab, assuming you have an emulator/device running.
Set up a remote debugging configuration. To do this go to Run > Debug Configurations...
Right click on Remote Java Application from the left hand panel and click 'New' to create a new configuration.
In the connection properties use localhost and port 8700. In my case I am using an emulator that is running on my local development machine. The default port for DDMS is 8700. If this is not the case for your setup you can check what port needs to be from the DDMS perspective after the UI Automator script is run in debug mode. (See steps 7-9 below)
Ensure that the project you've selected is the UI Automation project that you will be running. In the "Source" tab you may also add the UI Automation project there also. (Not sure if this is mandatory or not)
Click "Apply" and then close.
Now we will start running the UI Automator script with the debug option using the command line. For my example the command is (all on one line):
adb shell uiautomator runtest AndroidUIAutomation.jar -c com.example.uiautomation.TestUiAutomation -e debug true
It will then say:
Sending WAIT chunk
In Eclipse go into the DDMS perspective. Under the Devices tab you should see a process with a little red bug symbol. Next to it will be a question mark. In the last column in the table there will be two port numbers such as 8602/8700. The port 8700 is the one you will connect your remote debugging session to. This is what should be configured in step 4 above.
Now you are ready to start remote debugging. Set a breakpoint somewhere in the UI Automator script. Then debug by going to Run > Debug Configurations... and then select the Remote Java Application configuration that you created earlier and then click on "Debug".
If everything has gone well, then you should be able to debug your UI Automator script!

How to debug signed Android app from Eclipse?

Android 2.2.
I need to debug my signed APK on my Nexus S. How can this be done using Eclipse?
I start the app on my phone and then...?
Set the debuggable=true in the manifest, export, install and sign the the app. Connect the device via USB, enable USB debugging. Then open the DDMS perspective, select the device and attach to your app's process (you will see the package name listed). If you have a rooted device, you can connect to any process if adb is running as root.
When device connect to your eclipse running mechine , set debuggable=true in manifest file and enable debug mode in android phone it can view current running log using logcat, otherwise
You can debug your running application using adb tools from the command line
adb logcat - View device log
will display the current logcat (debug messages)
adb logcat [ <filter-spec> ]
using you can filter only your given debug messages
for configure debug tool view
http://developer.android.com/guide/developing/tools/adb.html
In Android Studio stable, you have to add the following 2 lines to application in the AndroidManifest file:
android:debuggable="true"
tools:ignore="HardcodedDebugMode"
The first one will enable debugging of signed APK, and the second one will prevent compile-time error.
After this, you can attach to the process via "Attach debugger to Android process" button.
You have two ways ..
You can use Log.v or log.d or Log.i (Logging) in your code and get all those logs in logcat view in eclipse while your application runs on your device.
You can run (while debugging , pressing that insect icon on eclipse ) the application from eclipse on device, By putting breakpoints in your code you can debug your application.

How to send key events to a headless emulator in an instrumentation test?

We are currently working on an instrumentation test suite which runs on our build server, but while the tests pass on a dev machine using a normal Android emulator, the builds fail on the build server since there we only run a headless emulator with the -no-window flag.
The failure occurs when trying to invoke the InstrumentationTestCase.sendKeys() method to programmatically open the options menu. The error is:
Permission denied: injecting key event from pid 646 uid 10026 to window Window{43d55100 paused=false} owned by uid 1000
We then found out that there's a INJECT_EVENTS permission, but setting it in the manifest had no effect. In fact in the log we saw this output:
Not granting permission android.permission.INJECT_EVENTS to package com.qype.radar (protectionLevel=2 flags=0x6644)
Does that mean this permission is useless?
We also tried to let the instrumentation test app and the app under test share the same Linux user ID using android:sharedUserId and run in the same process (android:process -- we weren't sure if that was already the case), but still no luck.
Does this mean it's currently impossible to run instrumentations which contain key events on a headless emulator, or are we missing something?
I run the emulator without -no-window on headless machines by first running an Xvnc instance (i.e. fake X server) then starting the emulator in that DISPLAY.
More accurately, I get the Xvnc and Android Emulator Jenkins plugins to do this for me.
Unfortunately, unlocking the screen is still a concern before injecting UI events, but this is (hackily) resolved by automatically running a command like this (similar to this other answer you've seen):
echo "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" | nc -q1 localhost 5554
Edit:
I discovered that this method is far more reliable:
adb shell input keyevent 82
Some info about keycode 82.
I had similar problem with my test on the Hudson server. In my case the problem I solved by suggestion from Android SDK:
http://developer.android.com/guide/topics/testing/testing_android.html#UITestTroubleshooting
Important was that I had to enable permissions for main application too.

Categories

Resources