Running specific commands in monkey not working - android

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.

Related

Is there any command for installing an app in Genymotion?

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

How can I tell when avd is completely launched?

I'm running a jenkins job that starts an avd device and uses calabash-android to run tests against it.
emulator -avd phone
How can I tell when this device is completely started so that I know I can start the tests without getting a device connection error?
I tried
adb wait-for-device
but that won't work because according to the adb docs
"Note that this command does not cause adb to wait until the entire system is fully booted. For that reason, you should not prepend it to other commands that require a fully booted system."
Right now I have a hard sleep, what would be a better way to script this?
Thank you.
I presume you are launching your tests via adb. If so, adb supports wait-for-device to block until the device is available (online).
adb wait-for-device
Okay, figured it out. The command I was looking for was
adb shell getprop sys.boot_completed
The full script for using the avd tool with jenkins looks like this (note: the output is all pumped into a textfile so that it doesn't tie up the script, there's probably a better way to handle this part of it)
Launch the sim
$ANDROID_HOME/tools/emulator -avd phone -gpu on -memory 2500 1>~/test.txt 2>&1 &
Wait for the sim to finish launching
A=$($ANDROID_HOME/platform-tools/adb shell getprop sys.boot_completed | tr -d '\r')
while [ "$A" != "1" ]; do
sleep 2
A=$($ANDROID_HOME/platform-tools/adb shell getprop sys.boot_completed | tr -d '\r')
done
Unlock sim
$ANDROID_HOME/platform-tools/adb shell input keyevent 82 1>~/test.txt 2>&1 &

Android Stop Emulator from Command Line

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.

How to use ADB Shell when Multiple Devices are connected? Fails with "error: more than one device and emulator"

$ adb --help
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
$ adb devices
List of devices attached
emulator-5554 device
7f1c864e device
$ adb shell -s 7f1c864e
error: more than one device and emulator
Use the -s option BEFORE the command to specify the device, for example:
adb -s 7f1c864e shell
For multiple Emulator, use the process's IP and port as the id, like:
adb -s 192.168.232.2:5555 <command>
See How to get the Android Emulator's IP address?
But if there is only a single Emulator, try:
adb -e <command>
See also http://developer.android.com/tools/help/adb.html#directingcommands
adb -d shell (or adb -e shell).
This command will help you in most of the cases, if you are too lazy to type the full ID.
From http://developer.android.com/tools/help/adb.html#commandsummary:
-d - Direct an adb command to the only attached USB device. Returns an error when more than one USB device is attached.
-e - Direct an adb command to the only running emulator. Returns an error when more than one emulator is running.
Another alternative would be to set environment variable ANDROID_SERIAL to the relevant serial, here assuming you are using Windows:
set ANDROID_SERIAL=7f1c864e
echo %ANDROID_SERIAL%
"7f1c864e"
Then you can use adb.exe shell without any issues.
To install an apk on one of your emulators:
First get the list of devices:
-> adb devices
List of devices attached
25sdfsfb3801745eg device
emulator-0954 device
Then install the apk on your emulator with the -s flag:
-> adb -s "25sdfsfb3801745eg" install "C:\Users\joel.joel\Downloads\release.apk"
Performing Streamed Install
Success
Ps.: the order here matters, so -s <id> has to come before install command, otherwise it won't work.
Hope this helps someone!
I found this question after seeing the 'more than one device' error, with 2 offline phones showing:
C:\Program Files (x86)\Android\android-sdk\android-tools>adb devices
List of devices attached
SH436WM01785 offline
SH436WM01785 offline
SH436WM01785 sideload
If you only have one device connected, run the following commands to get rid of the offline connections:
adb kill-server
adb devices
The best way to run shell on any particular device is to use:
adb -s << emulator UDID >> shell
For Example:
adb -s emulator-5554 shell
As per https://developer.android.com/studio/command-line/adb#directingcommands
What worked for my testing:
UBUNTU BASH TERMINAL:
$ adb devices
List of devices attached
646269f0 device
8a928c2 device
$ export ANDROID_SERIAL=646269f0
$ echo $ANDROID_SERIAL
646269f0
$ adb reboot bootloader
WINDOWS COMMAND PROMPT:
$ adb devices
List of devices attached
646269f0 device
8a928c2 device
$ set ANDROID_SERIAL=646269f0
$ echo $ANDROID_SERIAL$
646269f0
$ adb reboot bootloader
This enables you to use normal commands and scripts as if there was only the ANDROID_SERIAL device attached.
Alternatively, you can mention the device serial every time.
$ adb -s 646269f0 shell
This gist will do most of the work for you showing a menu when there are multiple devices connected:
$ adb $(android-select-device) shell
1) 02783201431feeee device 3) emulator-5554
2) 3832380FA5F30000 device 4) emulator-5556
Select the device to use, <Q> to quit:
To avoid typing you can just create an alias that included the device selection as explained here.
User #janot has already mentioned this above, but this took me some time to filter the best solution.
There are two Broad use cases:
1) 2 hardware are connected, first is emulator and other is a Device.
Solution : adb -e shell....whatever-command for emulator and adb -d shell....whatever-command for device.
2) n number of devices are connected (all emulators or Phones/Tablets) via USB/ADB-WiFi:
Solution:
Step1) run adb devices THis will give you list of devices currently connected (via USB or ADBoverWiFI)
Step2) now run adb -s <device-id/IP-address> shell....whatever-command
no matter how many devices you have.
Example to clear app data on a device connected on wifi ADB I would execute:
adb -s 172.16.34.89:5555 shell pm clear com.package-id
to clear app data connected on my usb connected device I would execute:
adb -s 5210d21be2a5643d shell pm clear com.package-id
For Windows, here's a quick 1 liner example of how to install a file..on multiple devices
FOR /F "skip=1" %x IN ('adb devices') DO start adb -s %x install -r myandroidapp.apk
If you plan on including this in a batch file, replace %x with %%x, as below
FOR /F "skip=1" %%x IN ('adb devices') DO start adb -s %%x install -r myandroidapp.apk
Create a Bash (tools.sh) to select a serial from devices (or emulator):
clear;
echo "====================================================================================================";
echo " ADB DEVICES";
echo "====================================================================================================";
echo "";
adb_devices=( $(adb devices | grep -v devices | grep device | cut -f 1)#$(adb devices | grep -v devices | grep device | cut -f 2) );
if [ $((${#adb_devices[#]})) -eq "1" ] && [ "${adb_devices[0]}" == "#" ]
then
echo "No device found";
echo "";
echo "====================================================================================================";
device=""
// Call Main Menu function fxMenu;
else
read -p "$(
f=0
for dev in "${adb_devices[#]}"; do
nm="$(echo ${dev} | cut -f1 -d#)";
tp="$(echo ${dev} | cut -f2 -d#)";
echo " $((++f)). ${nm} [${tp}]";
done
echo "";
echo " 0. Quit"
echo "";
echo "====================================================================================================";
echo "";
echo ' Please select a device: '
)" selection
error="You think it's over just because I am dead. It's not over. The games have just begun.";
// Call Validation Numbers fxValidationNumberMenu ${#adb_devices[#]} ${selection} "${error}"
case "${selection}" in
0)
// Call Main Menu function fxMenu;
*)
device="$(echo ${adb_devices[$((selection-1))]} | cut -f1 -d#)";
// Call Main Menu function fxMenu;
esac
fi
Then in another option can use adb -s (global option -s use device with given serial number that overrides $ANDROID_SERIAL):
adb -s ${device} <command>
I tested this code on MacOS terminal, but I think it can be used on windows across Git Bash Terminal.
Also remember configure environmental variables and Android SDK paths on .bash_profile file:
export ANDROID_HOME="/usr/local/opt/android-sdk/"
export PATH="$ANDROID_HOME/platform-tools:$PATH"
export PATH="$ANDROID_HOME/tools:$PATH"
Running adb commands on all connected devices
Create a bash (adb+)
adb devices | while read line
do
if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device=`echo $line | awk '{print $1}'`
echo "$device $# ..."
adb -s $device $#
fi
done
use it with
adb+ //+ command
you can use this to connect your specific device :
* adb devices
--------------
List of devices attached
9f91cc67 offline
emulator-5558 device
example i want to connect to the first device "9f91cc67"
* adb -s 9f91cc67 tcpip 8080
---------------------------
restarting in TCP mode port: 8080
then
* adb -s 9f91cc67 connect 192.168.1.44:8080
----------------------------------------
connected to 192.168.1.44:8080
maybe this help someone
Here's a shell script I made for myself:
#! /bin/sh
for device in `adb devices | awk '{print $1}'`; do
if [ ! "$device" = "" ] && [ ! "$device" = "List" ]
then
echo " "
echo "adb -s $device $#"
echo "------------------------------------------------------"
adb -s $device $#
fi
done
For the sake of convenience, one can create run configurations, which set the ANDROID_SERIAL:
Where the adb_wifi.bat may look alike (only positional argument %1% and "$1" may differ):
adb tcpip 5555
adb connect %1%:5555
The advance is, that adb will pick up the current ANDROID_SERIAL.
In shell script also ANDROID_SERIAL=xyz adb shell should work.
This statement is not necessarily wrong:
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
But one can as well just change the ANDROID_SERIAL right before running the adb command.
One can even set eg. ANDROID_SERIAL=192.168.2.60:5555 to define the destination IP for adb.
This also permits to run adb shell, with the command being passed as "script parameters".

How to use android monkey

I am a little confused on how to actually use monkey, i thought you were supposed to power on your emulator and go into terminal editor and type:
$ adb shell monkey -p insert.my.package.name.here -v 500
since that did not work (error, adb: permission denied)
i tried monkey -p insert.blah.blah -v 500 and this comes up that it has been killed, what am I doing wrong?
It's probably trying to talk to a device over your USB port.
You probably just need to add an -e in there to tell adb to connect to the emulator:
$ adb shell monkey -p insert.my.package.name.here -v 500
(Or -s serialnum if you have more than one emulator running.)
See Directing commands in the ADB docs.
It's well explained here:
https://android.googlesource.com/platform/development/+/master/cmds/monkey/README.NETWORK.txt
it's a tool for testing apps, and the port indicates which porto to connect (binds to localhost) to issue remote commands

Categories

Resources