Hey guise I want to install an app on several devices in Genymotion by using terminal in ubuntu. Each time I do this:
adb install package.app
It gives me the below error:
error: more than one device/emulator
- waiting for device -
error: more than one device/emulator
I also read about
adb -s udid uninstall package.app
but this is not the solve I want. I don't want to use udid and also I don't know which device has my app from before?
Any suggestions?!
adb help is not extremely clear on what to do:
-d - directs command to the only connected USB device...
-e - directs command to the only running emulator...
-s <serial number> ...
-p <product name or path> ...
The flag you decide to use has to come before the actual adb command:
adb -e install path/to/app.apk
You cannot pass adb commands when there are more than one device connected and running at the same time ; this is a limitation with adb.
Wow finally!
I found this command as my answer:
adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install app.apk
Thanks to sivze
From a shell on my PC, I can run adb shell cmd package list packages, and get a list of all installed packages. I would like to run this and similar commands locally on my Android phone (Nexus 6P) in a terminal emulator (currently using Termux).
If I open the same shell with /system/bin/sh, and then try to run /system/bin/cmd package list packages, nothing happens (no errors, just outputs nothing and reloads the prompt).
If I run /system/bin/cmd -l the list of options appears as expected. $PATH and $LD_LIBRARY_PATH are the same in both environments. One major difference is that echo $USER returns "shell" from adb shell, but returns my local username from /system/bin/sh launched from Termux.
Is there any way to replicate the behavior of commands run from adb shell in a terminal emulator locally on Android?
Edit:
My device is rooted, and I am OK with root only solutions.
The problem is Termux. By design, Termux runs only (or is mostly?) the Linux command line programs that you install from within Termux using apt or the newer "native" package management interface, e.g. apt install bsdtar. What you need to run the adb shell commands is a terminal emulator that can truly access the underlying Android file system, not just the Termux that is practically a chroot save for the fact that it's aware it's not running commands from the filesystem root /.
As a simple test, run the following command:
which ls
It should return something like /system/bin/ls. But if it returns something like /data/data/com.termux/files/usr/bin/applets/ls then you have to change your terminal emulator to something else. I suspect that Termux was designed to take into account the more restrictive shell execution policies that Google put into place after KitKat or the Android 4.X.
The Android distribution I'm using, LineageOS 14.1, comes with a built-in shell emulator that allows me to run commands found in /system/bin/ls.
I don't have a rooted Nougat device handy, but something like the following may be a close enough approximation to adb shell (assuming you are using SuperSU):
env -i USER=shell "$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su)" shell --context u:r:shell:s0 --shell /system/bin/sh --command COMMAND
I (very briefly) tested it from Termux on a rooted Marshmallow device.
To elaborate:
the -i flag is used to start with an empty environment
USER=shell isn't specifically required, but for some reason su refuses to run with a completely empty environment
$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su) points to the full path of the su binary on your device and can be hardcoded if you prefer
shell instructs the su binary to login as the shell user (the same as adb shell)
--context u:r:shell:s0 sets the appropriate SELinux context
--shell /system/bin/sh instructs SuperSU to use the system shell rather than it's own sush shell
Another option would be to actually run adb from the device, connecting to itself over TCP. If you need some functionality that is only available via adb (e.g. in my case it was adb forward) then this may be your only option. Unfortunately this isn't particularly convenient.
I wasn't able to find success with any publicly available adb binaries, so I build it myself with a few minor changes. You can see the sources I used and the changes I made at https://github.com/shakalaca/fastboot-adb-android and https://github.com/brbsix/fastboot-adb-android, respectively.
Once you have adb installed, here's an abbreviated list of commands I used to connect to the device:
# Add iptables rules to block external connections to port 9999'
su root iptables -N adbd
su root iptables -A adbd -i lo -p tcp -m tcp --dport 9999 -j ACCEPT
su root iptables -A adbd -p tcp -m tcp --dport 9999 -j DROP
su root iptables -A INPUT -j adbd
# Necessary in order to display authorization prompt
su shell setprop ro.debuggable 1
su shell setprop service.adb.tcp.port 9999
su root start adbd
adb connect 127.0.0.1:9999
adb wait-for-local-device
To shut down:
adb kill-server
su root stop adbd
su shell setprop ro.debuggable 0
su shell setprop service.adb.tcp.port 0
su root iptables -D INPUT -j adbd
su root iptables -F adbd
su root iptables -X adbd
So I tried this recently...if you're rooted you can use a terminal emulator.
su
then the command you want without "adb shell" part of it.
i tried the command "adb shell dumpsys deviceidle force-idle" in order to force device into doze.
I did this on the device via terminal emulator as:
"dumpsys deviceidle force-idle" and it did take effect.
also the dumpsys batterystats command worked.
be careful with commands with extensive text output, as the screen will be flooded with the output and will be unresponsive for some time.
EDIT
I originally answered this without the termux tag in mind. This worked for me while trying to execute shell commands on a vanilla emulator and saw this question while researching, so I tried to answer it differently.
You almost had it there in your question. You only need to execute sh:
int result = -1;
try {
final Process shell = Runtime.getRuntime().exec("sh");
final DataOutputStream commands = new DataOutputStream(shell.getOutputStream());
commands.writeBytes("write a series");
commands.writeBytes("of commands here");
commands.writeBytes("exit\n");
commands.flush();
result = shell.waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
If result == 0 the commands were succesful, else otherwise
Only rooted android
Busybox must be installed (though you can try without it)
Just write the normal command without the prefix adb
I am trying to do a logcat to a file using adb shell by following command -
adb shell "nohup logcat -f /storage/sdcard0/myLog.txt -v time &"
If I do a ps | grep logcat, I don't see the logcat command. Even I tried to see nohup command, but it is not there. So somehow above command does not work.
However if I perform the command in 2 steps it works fine -
adb shell
nohup logcat -f /storage/sdcard0/myLog.txt -v time &
I can see the process using ps and logcat continues to record to the file even if I disconnect adb shell. Now I would like the first command to work, since I am using python scripts to issue commands via ADB. It is possible to change the python scripts, however I would like to know if I am doing anything wrong in issuing the first command and if it is possible to make it work.
try
adb logcat
not
adb shell logcat
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.
I read this page
http://hariniachala.blogspot.com/2011/09/android-application-ui-testing-with.html
and when I executed this code
./adb -d shell monkey -p package_name --port 1080 &
./adb -d forward tcp:1080 tcp:1080
telnet localhost 1080
I faced the following problem
C:\Users\subhi\Desktop>adb -d shell monkey -p package_name --port 1080
error: device not found
what is the simplest method to do that ?
probably the reason why ADB is not finding a device is that you are forcing the search for a physical one (-d option). If you want to interact with an emulated device you should use -e or even no option (ADB should resolve it automatically).
Summing up, if I understood your problem, my suggestion is to launch an AVD, wait until boot and use:
adb shell monkey -p <app.package.name> --port <X> &
adb forward tcp:<X> tcp:<X>
telnet localhost <X>
where
<X>
is the port number you want to use.