I am trying to debug an Android application using command line and script tools (I've had enough of Android Studio eating my CPU).
To connect to my app, I do adb jdwp to get the pid and then adb forward tcp:8700 jdwp:<pid> to be able to connect to the app. But I'd like to script this more.
adb jdwp have two limitations: 1. It doesn't return, so I have to kill adb with ctrl-c, 2. it only lists pids. If there are more pids, there's no easy way to see which is my app.
Is there a way to get adb jdwp to behave: Get it to exit and to return applicationId along with the pid?
Here is an example script just like Struchu's without writing to a local file
Improvements
It starts the app
It gets the apps program id after starting it
It also uses adb over wifi given that your device is starting the hotspot
#!/bin/bash
PACKAGE="com.example.app"
ACTIVITY="$PACKAGE"/"$PACKAGE"".MainActivity"
#Connect over wifi
adb connect "$(ip r | grep default | cut -f3 -d\ )":5555
# adb -e connect to wifi device and start activity
adb -e shell am start -D -n "$ACTIVITY"
JDWP=$(adb -e shell ps | grep "$PACKAGE" | cut -f7 -d\ )
adb forward tcp:8000 jdwp:"$JDWP"
jdb -sourcepath app/src/main/java -attach localhost:8000
I managed to solve this issue with the following script (which will work provided your app is launched as a last process). As a future enhancement it could be integrated with gradle task runner to simplify the process even more.
#!/bin/bash
DEBUG_PORT=7777
SOURCE_PATH=app/src/main/java
FILE=/var/tmp/andebug-$(date +%s)
adb jdwp > "$FILE" &
sleep 1
kill -9 $!
JDWP_ID=$(tail -1 "$FILE")
rm "$FILE"
adb forward tcp:$DEBUG_PORT jdwp:$JDWP_ID
jdb -sourcepath $SOURCE_PATH -attach localhost:$DEBUG_PORT
Related
How can I debug my Android NDK project in C++, using the lldb debugger from the command line?
Probably you can try below: (This example steps are based on macOS)
run gdb server and attach process
//Below commands will suspend the execution on the running app, and waits for a debugger to connect to it on port 5045.
adb shell
// to get pid
root#generic_x86:/ # ps | grep <your-app-name>
u0_a54 6510 1196 800157 47442 ffffffff b662df1b S
<your-app-name>
root#generic_x86:/ # gdbserver :5045 --attach 6510 (PID)
Attached; pid = 6510
Listening on port 5045
//The process is now suspended, and gdbserver is listening for debugging clients on port 5045.
attach gdb debugger
//open a new terminal, e.g. terminal2, send below commands from this new terminal
//forward the above port to a local port on the host with the abd forward command
adb forward tcp:5045 tcp:5045
//launch gdb client from your android ndk folder
<your-ndk-home>/android-ndk-r16b/prebuilt/darwin-x86_64/bin/gdb
//Target the gdb to the remote sever
(gdb) target remote :5045
//now the process is successfully attached with the application for debugging, you can see below info from terminal 1.
Remote debugging from host 127.0.0.1
Preparation
Prepare an android device(Root privilege is not required, we will use its /data/local/tmp directory).
Install NDK, CMake, Ninja, adb, lldb, and put them in PATH env var.
Compile program with debugging info (i.e. keep the -g flag)
After compilation, copy them to your device's /data/local/tmp directory.
Copy NDK provided lldb-server to your android phone(prefer the 64bit one), and start it by:
./lldb-server platform --listen "*:10086" --server
10086 is port number, you may change it.
Forward port by running:
adb forward tcp:10086 tcp:10086
Get device name
adb devices #For me, it's 39688bd9
Install LLDB, adding its binary to PATH, typing these commands:
platform select remote-android
platform connect connect://39688bd9:10086
Among whith, 39688bd9 is my device id, 10086 is the port that I choose in previous steps.
Now, you're connected with lldb-server, thus just use lldb like locally:
file some_executable_file_with_debug_info
b main
r
With android-ndk-r25b, I had some luck with the below:
In shell window 1
adb push <ndk_dir>/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.6/lib/linux/aarch64/lldb-server /data/local/tmp
adb shell chmod +x /data/local/tmp/lldb-server
adb shell run-as <package_name> killall -9 lldb-server
sleep 1
adb shell run-as <package_name> cp /data/local/tmp/lldb-server /data/data/<package_name>/
adb shell am start -D -n "<package_name>/android.app.NativeActivity"
adb shell run-as <package_name> sh -c '/data/data/<package_name>/lldb-server platform --server --listen unix-abstract:///data/data/<package_name>/debug.socket'"
In shell window 2
# Get the pid of the process you are trying to debug
adb shell run-as <package_name> ps
lldb
> platform select remote-android
> platform connect unix-abstract-connect:///data/data/<package_name>/debug.socket
> attach <pid>
In shell window 3
# You will again need the pid of the process you are trying to debug
adb shell run-as <package_name> ps
adb forward tcp:12345 jdwp:<pid>
jdb -attach localhost:12345
Then go back to lldb running in window 2, and continue your process
I found this script to be useful:
https://github.com/iivke/flutter_android_lldb/blob/main/flutter_lldb.py
I try to find some way to kill unnecessary services/processes in Android from shell.
The problem is that after killing the process it starts again after few seconds!
for example I tried to kill batterywarning, but it keep starting again:
root#w812a_kk:/ # ps | grep batteryw
shell 17986 1 1044 364 c00601dc b6e9f094 S /system/bin/batterywarning
1|root#w812a_kk:/ # ps | grep batteryw
shell 17781 1 1044 364 c00601dc b6ee6094 S /system/bin/batterywarning
root#w812a_kk:/ # busybox killall batterywarning
root#w812a_kk:/ # ps | grep batteryw
1|root#w812a_kk:/ # ps | grep batteryw
shell 17986 1 1044 364 c00601dc b6e9f094 S /system/bin/batterywarning
I did find several methods to kill service/process in the following link, yet the process is starting again.
Android ADB stop application command like "force-stop" for non rooted device.
Is it something that can only be done in init.rc ?
Thanks,
This service is probably started as STICKY - it will be automatically restarted by OS after some predefined timeout.
You can check it by pulling the logcat from the device and grepping it for the name of the service you attempted to kill:
$ adb logcat -d > logcat.txt
$ grep -C 5 -i batterywarning logcat.txt
If you see something along the lines scheduling restart of crashed service in X seconds then you know for sure that the service is being restarted by OS.
If it is indeed the case, I doubt that you can kill it completely on a non-rooted device (neither I know how to achieve this on a rooted one).
kill a process when you have the process name (basically you don't have to search 'ps' for the process ID and then kill it with PID):
adb shell kill $(pidof com.android.phone)
Use the following command from terminal:
adb shell am crash [applicationId]
I would like to be able to trigger an Android heap dump from the command line. Is there a command for that?
Specifically, from the command line, not via Montior or DDMS GUIs
Maybe something like using ddms or adb, e.g. ddms -head-dump or adb shell heapdump? AFAICT monitor and ddms always start in GUI mode, and adb doesn't have a heap dump command.
Update: I tried this, it looked promising, but it doesn't work:
adb jdwp
adb forward tcp:8000 jdwp:1234 (substitute output of 1 for 1234)
jmap -dump:format=b,file=heapdump.hprof localhost:8000
But even the heap summary fails:
jmap -heap localhost:8000
Attaching to remote server localhost:8000, please wait...
Error attaching to remote server: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.net.SocketTimeoutException: Read timed out
In Android pre 3.0 you can use so called
kill -10 <pid> (more)
In Android 3.0 a new command-line tool has been added:
adb shell am dumpheap <pid> <output-file-name>; (more)
Detailed description
To get HPROF you need also change the format of it using hprof-conv
The easy way:
adb shell am dumpheap your.package.name /sdcard/dumpheap.hprof
The hard way (don't remember why I've added it originally):
adb shell am dumpheap $(ps | grep your.package.name | awk '{print $2}') /sdcard/dumpheap.hprof
If your device doesn't have awk try to use busybox awk.
After that pull the created file, convert it with hprof-conv and open it in Android Studio.
This question is identical to How to shut down Android emulator via command line.
However, after attempting the suggested solution from the first answer adb emu kill has not proven successful for me.
I am automating unit tests for an android application. My bash script runs on a headless machine. It creates an android device using android create avd and executes emulator with the -no-window attribute. It then compiles the test project, connects to the emulator using adb, installs the project and executes my tests. This all works fine.
Now I need to terminate the emulator process, and just like the referenced post, I am only able to do this using kill -9.
The Google tutorial Managing AVDs from the Command Line only mentions how to stop emulators within a GUI environment.
Any help is appreciated.
May be adb kill-server helps for you?
or
adb -s emulator-5544 emu kill, where emulator-5544 - emulator name.
For Linux users it will be
adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
FOR MAC:
Run:
ps -ax | grep emulator
which gives you a wide result. Something like:
6617 ?? 9:05.54 /Users/nav/Library/Android/sdk/emulator/qemu/darwin-x86_64/qemu-system-x86_64 -netdelay none -netspeed full -avd Nexus_One_API_29
6619 ?? 0:06.10 /Users/nav/Library/Android/sdk/emulator/emulator64-crash-service -pipe com.google.AndroidEmulator.CrashService.6617 -ppid 6617 -data-dir /tmp/android-nav/
6658 ?? 0:07.93 /Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/QtWebEngineProcess --type=renderer --disable-accelerated-video-decode --disable-gpu-memory-buffer-video-frames --disable-pepper-3d-image-chromium --enable-threaded-compositing --file-url-path-alias=/gen=/Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/gen --enable-features=AllowContentInitiatedDataUrlNavigations --disable-features=MacV2Sandbox,MojoVideoCapture,SurfaceSynchronization,UseVideoCaptureApiForDevToolsSnapshots --disable-gpu-compositing --service-pipe-token=15570406721898250245 --lang=en-US --webengine-schemes=qrc:sLV --num-raster-threads=4 --enable-main-frame-before-activation --service-request-channel-token=15570406721898250245 --renderer-client-id=2
6659 ?? 0:01.11 /Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/QtWebEngineProcess --type=renderer --disable-accelerated-video-decode --disable-gpu-memory-buffer-video-frames --disable-pepper-3d-image-chromium --enable-threaded-compositing --file-url-path-alias=/gen=/Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/gen --enable-features=AllowContentInitiatedDataUrlNavigations --disable-features=MacV2Sandbox,MojoVideoCapture,SurfaceSynchronization,UseVideoCaptureApiForDevToolsSnapshots --disable-gpu-compositing --service-pipe-token=--lang=en-US --webengine-schemes=qrc:sLV --num-raster-threads=4 --enable-main-frame-before-activation --service-request-channel-token= --renderer-client-id=3
10030 ttys000 0:00.00 grep emulator
The first (left) column is the process ID (PID) that you are looking for.
Find the PID in the first (top) row. In the above example, it's 6617.
Kill that process:
kill PID
In my case, the command is:
kill 6617
Usually, killing the first process in enough to stop the emulator, but if that doesn't work, you can:
5.1. try killing other processes as well.
5.2 kill with -9 (force kill):
kill -9 PID
To stop all running emulators we use this command:
adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
if
adb kill-server
doesn't work. Use :
adb emu kill
this will kill all the emulators
If multiple emulators are present then use:
adb -s * emu kill
Sometimes the command
adb -s emulator-5554 emu kill
did not work on my CI servers or desktops, for unknown reason.
I think on Windows it's OK to kill the process of qemu, just like
Taskkill /IM qemu-system-x86_64.exe /F /T
I can close it with:
adb shell reboot -p
The other answer didn't work for me (on Windows 7). But this worked:
telnet localhost 5554
kill
Why not just do
adb reboot bootloader
If you don't want to have to know the serial name of your device for adb -s emulator-5554 emu kill, then you can just use adb -e emu kill to kill a single emulator. This won't kill anything if you have more than one emulator running at once, but it's useful for automation where you start and stop a single emulator for a test.
adb kill-server will kill all emulators and restart the server clean.
None of the solutions worked for me. I had to go the telnet way including authentication:
AUTH=$(cat "$HOME/.emulator_console_auth_token")
expect << EOF
spawn telnet localhost 5554
expect "OK"
send "auth $AUTH\r"
expect "OK"
send "kill\r"
expect "OK"
send "exit\r"
EOF
The full script can be obtained with a free license from https://github.com/kullo/android-emulator-tools
Update: looks like this still does not reliably close the console and ADB ports (e.g. 5554,5555)
I use this one-liner, broken into several lines for readability:
adb devices |
perl -nle 'print $1 if /emulator-(\d+).device$/' |
xargs -t -l1 -i bash -c "
( echo auth $(cat $HOME/.emulator_console_auth_token) ;
echo kill ;
yes ) |
telnet localhost {}"
To automate this, you can use any script or app that can send a string to a socket. I personally like nc (netcat) under cygwin. As I said before, I use it like this:
$ echo kill | nc -w 2 localhost 5554
(that means to send "kill" string to the port 5554 on localhost, and terminate netcat after 2 seconds.)
This scrips can help you to kill All emulators at once:
Filter emulators (because you can have a mixing on physical and emus)
Kill all emus by ADB id
Disadvantage of this solution: if your emu just "stuck" you can't kill it with adb command and it required process kill. But that's very rare case.
while [ "`adb devices | grep -Eoh \"emulator-\d{0,4}\" | wc -l | tr -d ' '`" != "0" ]; do
echo "Connected emulators:"
adb devices | grep -Eoh "emulator-\d{0,4}"
for emulator in $(adb devices | grep -Eoh "emulator-\d{0,4}")
do
echo "Killing the emulator: $emulator"
adb -s "$emulator" emu kill | true
done
sleep 10;
done
echo "All emus has been killed"
List of devices attached
emulator-5584 host
emulator-5580 host
emulator-5576 host
emulator-5572 host
emulator-5568 host
emulator-5564 host
emulator-5560 host
C:\Users\Administrator>adb -s emulator-5584 emu kill
error: could not connect to TCP port 5584: cannot connect to 127.0.0.1:5584: No connection could be made because the target machine actively refused it. (10061)
NOTE: gui of emulator is not running but still it's showing
SOLUTION:
adb kill-server
start emulator using:
emulator.exe -netdelay none -netspeed full -avd Nexus_5X_API_19
On Linux when the process became unresponsive the only way I could terminate the emulator was using the command:
kill -9 `pidof adb`
which finds the process ID of adb and sends a kill -9 signal to it.
to get your devices name try to run this on Android Studio terminal
adb devices
after you get devices name, kill app with this comment
adb -s emulator-5554 emu kill
where
emulator-5554
is your device name
On Windows 10, with Android Studio 2021.1.1 patch 3, the adb -s emulator-5554 emu kill command does not work, adb being not recognized.
But here's the solution using the Tool/Device Manager. Simply select the active emulator and click on x to stop it.
Is there a way of running adb commands on all connected devices? To uninstall an app from all connected devices with "adb uninstall com.example.android".
The commands I am interested in is mainly install and uninstall.
I was thinking about writing a bash script for this, but I feel like someone should have done it already :)
Create a bash file and name it e.g. adb+:
#!/bin/bash
adb devices | while read -r line
do
if [ ! "$line" = "" ] && [ "$(echo "$line" | awk '{print $2}')" = "device" ]
then
device=$(echo "$line" | awk '{print $1}')
echo "$device" "$#" ...
adb -s "$device" "$#"
fi
done
Usage: ./adb+ <command>
Building on #Oli's answer, this will also let the command(s) run in parallel, using xargs. Just add this to your .bashrc file:
function adball()
{
adb devices | egrep '\t(device|emulator)' | cut -f 1 | xargs -t -J% -n1 -P5 \
adb -s % "$#"
}
and apply it by opening a new shell terminal, . ~/.bashrc, or source ~/.bashrc.
If you only want to run on devices (or only on emulators), you can change the (device|emulator) grep by removing the one you don't want. This command as written above will run on all attached devices and emulators.
the -J% argument specifies that you want xargs to replace the first occurrence of % in the utility with the value from the left side of the pipe (stdin).
NOTE: this is for BSD (Darwin / Mac OS X) xargs. For GNU/Linux xargs, the option is -I%.
-t will cause xargs to print the command it is about to run immediately before running it.
-n1 means xargs should only use at most 1 argument in each invocation of the command (as opposed to some utilities which can take multiple arguments, like rm for example).
-P5 allows up to 5 parallel processes to run simultaneously. If you want instead to run the commands sequentially, simply remove the entire -P5 argument. This also allows you to have two variations of the command (adball and adbseq, for example) -- one that runs in parallel, the other sequentially.
To prove that it is parallel, you can run a shell command that includes a sleep in it, for example:
$ adball shell "getprop ro.serialno ; date ; sleep 1 ; date ; getprop ro.serialno"
You can use this to run any adb command you want (yes, even adball logcat will work! but it might look a little strange because both logs will be streaming to your console in parallel, so you won't be able to distinguish which device a given log line is coming from).
The benefit of this approach over #dtmilano's & approach is that xargs will continue to block the shell as long as at least one of the parallel processes is still running: that means you can break out of both commands by simply using ^C, just like you're used to doing. With dtmilano's approach, if you were to run adb+ logcat, then both logcat processes would be backgrounded, and so you would have to manually kill the logcat process yourself using ps and kill or pkill. Using xargs makes it look and feel just like a regular blocking command line, and if you only have one device, then it will work exactly like adb.
This is an improved version of the script from 強大な. The original version was not matching some devices.
DEVICES=`adb devices | grep -v devices | grep device | cut -f 1`
for device in $DEVICES; do
echo "$device $# ..."
adb -s $device $#
done
To add in the ~/.bashrc or ~/.zshrc:
alias adb-all="adb devices | awk 'NR>1{print \$1}' | parallel -rkj0 --tagstring 'on {}: ' adb -s {}"
Examples:
$ adb-all shell date
$ adb-all shell getprop net.hostname
$ adb-all sideload /path/to/rom.zip
$ adb-all install /path/filename.apk
$ adb-all push /usr/local/bin/frida-server-arm64 /data/local/tmp/frida-server
Explanation: awk extracts the device id/host (first column: print $1) of every lines except the first one (NR>1) to remove the "List of devices attached" header line), then gnu parallel runs adb -s <HOSTNAME> <whatever-is-passed-to-the-alias> on whatever non-empty line (-r) in the order specified (-k, to avoid random order / fastest response order) and prepend each line with on <DEVICE>:\t for clarity, all in parallel (-j0, possible to set another number to define how many adb should be ran in parallel instead of unlimited).
:)
This is the highest result on Google, so for all Windows users coming here let me add this solution by User zingh (slightly modified to accept arbitrary commands, rather than "only" install
Batch file (adball.bat):
FOR /F "skip=1" %%x IN ('adb devices') DO start adb -s %%x %*
Call as:
adball uninstall com.mypackage
(%* takes all input parameters, my line above makes it so that all commands are passed to adb as they are, so that you can type multiple words, flags etc.)
Note: you can even use this directly from the Android Studio "run all" popup, if you install the Powershell-plugin. You can add adball to your path, then double-tap ctrl and run
powershell adball uninstall com.mypackage
adb wrapper supports selecting multiple targets for adb commands and parallel execution.
From its README:
# Installation
./install.sh ~/apps/android-sdk-linux
# Execute adb commands on all connected devices.
adb set-target all
# Execute adb commands on given devices.
adb set-target emulator-5554 C59KGT14263422
# Use GNU parallel for parallel install.
adb set-parallel true
(Disclaimer: I have written half of it)