31

Is there any way to launch by command line a given avd and have it registered via adb ?

I would also prefer to get the emulator launched headless.

I am looking for this to run tests quickly.

Vikalp Patel
  • 10,082
  • 6
  • 55
  • 92
Snicolas
  • 36,854
  • 14
  • 106
  • 172

7 Answers7

41

For others looking for non-headless command line startup:

/Applications/Genymotion.app/Contents/MacOS/player --vm-name "xxxx"

Get a list of vms:

$ VBoxManage list vms
"Galaxy Nexus - 4.2.2 - API 17 - 720x1280" {56d8e3aa-ecf8-483e-a450-86c8cdcedd35}

Where xxxx can be either the name or the id:

/Applications/Genymotion.app/Contents/MacOS/player --vm-name 56d8e3aa-ecf8-483e-a450-86c8cdcedd35
/Applications/Genymotion.app/Contents/MacOS/player --vm-name "Galaxy Nexus - 4.2.2 - API 17 - 720x1280"

You can kill it with a normal process kill:

ps | grep "Genymotion\.app/Contents/MacOS/player" | awk '{print $1}' | xargs kill
k s
  • 862
  • 8
  • 9
  • Nice, and how you deal with the IP address ? You will need it to get adb connected to genymotion.. – Snicolas Sep 18 '13 at 17:57
  • 1
    In genymotion settings make sure the "Path to Android SDK" is set and then make sure "When a virtual device starts, connect it automatically to ADB" is checked. When the device is started with the genymotion player command it will autoconnect it to ADB – k s Sep 18 '13 at 18:27
  • how did you get the parameter name for player ? Isn't there anything to run it headless ? – Snicolas Sep 18 '13 at 19:46
  • He get this from my SO post I think ;-) http://stackoverflow.com/questions/18768489/how-to-start-genymotion-device-with-shell-command/18773118#18773118 – eyal-lezmy Sep 19 '13 at 14:29
  • Btw, to answer your question about running the Genymotion player headless, there is no other parameter than vm-name. But I think the R&D is open to hear about this feature request so don't hesitate to push a mail to support@genymotion.com, explaning them how it is interesting for you and the good use cases it can bring. – eyal-lezmy Sep 19 '13 at 19:12
  • Sorry, I did not steal your answer ;) I got the info from genymobile support -- I wish I had seen your answer as that is what I was looking for but it did not show up in any of my stackoverflow searches. – k s Sep 20 '13 at 14:33
  • Killing the process will make it impossible to restore to a snapshot later. – Janusz Nov 22 '13 at 16:00
  • 2
    kill was overkill, "pkill player" will suffice. – Karioki Mar 05 '14 at 04:57
  • @ks No worries. I'm glad to see people helping each other on our product. Thank you. – eyal-lezmy Sep 01 '14 at 08:00
15

Here is a better procedure. It will require a first manual launch, but afterwards, you will get a blazing fast genymotion, up within seconds. The following scripts have been tested on macos x. They may need some more work for linux.

First, launch genymotion emulator normally via genymotion app. Then, get its sha1 from Virtual box :

VBoxManage list vms

Then, take a snapshot of it from command line :

#script genymotion-save.sh
VM=6a5d9245-b751-47aa-b38d-989c5f1a9cfb

echo "VM is \"$VM\""
VBoxManage snapshot $VM take snap1 

Then you can detect its ip using this script (most of its complexity comes from mac address conversion):

#script genymotion-detect-ip.sh
VM=6a5d9245-b751-47aa-b38d-989c5f1a9cfb

#find mac of vm
#http://stackoverflow.com/questions/10991771/sed-to-insert-colon-in-a-mac-address
# Update arp table
for i in {1..254}; do ping -c 1 192.168.56.$i 2&>1; done

MAC=`VBoxManage showvminfo "$VM" | grep MAC | grep Host | awk -F ":" '{print $3}' | cut -c 2-13`
#echo "MAC is $MAC"

MAC=`echo $MAC | sed -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/' | tr '[:upper:]' '[:lower:]'`
#echo "MAC is $MAC"

# Find IP: substitute vname-mac-addr with your vm's mac address in ':' notation
IP=`arp -a | sed "s/ \(.\):/ 0\1:/" | sed "s/:\(.\):/:0\1:/g"|sed "s/:\(.\):/:0\1:/g"|sed "s/:\(.\)$/:0\1/"|grep $MAC`
#echo "IP is $IP"

IP=`echo $IP | cut -d "(" -f2 | cut -d ")" -f1`
echo $IP

Now, you have all you need to start up the vm's snapshot from the command line and connect to it via adb (using root). You can do it with this script :

# script genymotion-start.sh
VM=6a5d9245-b751-47aa-b38d-989c5f1a9cfb

echo "VM is \"$VM\""
VBoxManage snapshot $VM restore snap1 &
VBoxHeadless -s $VM &

IP=`./genymotion-detect-ip.sh`
echo $IP

#adb tcpip 5555
adb connect $IP:5555

#restart adb as root to allow powering it off
#root mode is generally what we want from a headless emulator (to download emma files for instance)
adb root
adb connect $IP:5555

And finally you can also use a script to shutdown the emulator properly :

#script genymotion-stop.sh 
IP=`./genymotion-detect-ip.sh`

adb root
adb connect $IP:5555
adb shell reboot -p &

This is still a lot of scripting but it works fine and controls the genymotion emulator in an handy way.

Let's hope genymobile can make this eve easier in future releases.

Snicolas
  • 36,854
  • 14
  • 106
  • 172
  • These scripts work fine (thank you)... but the screen capture produces distorted images (both VBoxManage controlvm based captures and adb shell /system/bin/screencap based captures produce the same distorted images). Any way to get the proportional and elements on the screen to display properly when starting the vm this way? – David West Sep 17 '13 at 16:18
  • That's quite a good question. I guess geny mobile doesn't really take this use case into account till now. Maybe a better solution for you would be to use Virtual box with a framebuffer display. I guess it could work better as it would be a general X11 impl. – Snicolas Sep 17 '13 at 20:18
  • I'm guessing genymotion is manipulating the video output from virtual box to display it — I can't imagine how but there does not seem to be anyway of starting the vm instances via command line (headless or not) and getting the same display output that is produced when genymotion starts the instances. Resorting to trying various combinations of versions between VirtualBox, GenyMotion, and Android Devices in case I hit some corner case that no one else is experiencing but no luck yet. – k s Sep 18 '13 at 14:15
  • Rather than the detect-ip script, you could just use this `VBoxManage guestproperty get '6a5d9245-b751-47aa-b38d-989c5f1a9cfb' androvm_ip_management` This will return a nice ip and the single quotes around the vm name will give you proper values even if your vm name had spaces. You could optionally leave the uuid in there instead. – Sojurn Dec 07 '13 at 10:43
  • @Sojurn, would you write your own answser and provide reusable scripts ? – Snicolas Dec 07 '13 at 10:58
  • @Sojurn When I run your command, I get an error: "No value set!" – IgorGanapolsky Jan 31 '14 at 18:39
  • @Snicolas: Your restart script does not work for me. I get: `VBoxHeadless: error: The machine '.....' is already locked for a session (or being unlocked)` – Luis A. Florit Mar 09 '14 at 16:58
  • @Snicolas: I don't get an error if I run the commands out of the script. Problem is the `&` after the restore command. I think you should not add it. – Luis A. Florit Mar 09 '14 at 18:26
  • Just a linux shell ampersand to start a background command, otherwise your shell will be blocked by the VM being launched. If you don't need it and it works, that's fine. – Snicolas Mar 10 '14 at 16:53
  • Thank you for the scripts. I get the following error when trying this via Jenkins (but seemingly works fine if I try it straight from a terminal on the box itself): Error: failed to start machine. Error message: Failed to load unit 'HGCM' (VERR_SSM_UNEXPECTED_DATA) – NPike Jul 10 '14 at 21:41
2

I am running on Ubuntu and I modified Snicolas's answer and uploaded as Gist: https://gist.github.com/guneysus/410bb0e6b56d6f228555

The major differences are:

  • Finding IP method is not worked on Ubuntu. I came with another work-around
  • Defined devices in geny_devices.sh and source this file to pick VM easily:

```

# script geny_devices.sh

s3_43="e63063e8-a922-4832-8bcf-05362c3a1c9a"
nexus_44="45287ed9-2d5e-49a5-a0f9-82c29e7cc4b3"

# Samsung Galaxy S3 - 4.3 - API 18 - 720x1280" {e63063e8-a922-4832-8bcf-05362c3a1c9a}
# "Google Nexus 7 - 4.4.4 - API 19 - 800x1280" {45287ed9-2d5e-49a5-a0f9-82c29e7cc4b3}

#script geny_snap.sh
source geny_devices.sh
VM=${s3_43}

# Hopefully performance improvement ;) Not really necessary
# for in in {1..254}; 
#     do ping -c 192.168.56.$1 2&>1;
# done

MAC=`VBoxManage showvminfo ${VM} | grep MAC | awk -F ":" '{print $3}' | cut -c 2-13`
# echo "MAC is ${MAC}"

# On linux data returned from arp -a is like 
# ? (192.168.56.101) at 08:00:27:b0:7f:38 [ether] on vboxnet0
# ? (192.168.0.1) at 9e:a9:e4:d5:43:5b [ether] on eth2

# Find IP with 
IP=`arp -a | egrep vboxnet|grep -E -o  "([0-9]{1,3}[\.]){3}[0-9]{1,3}"`
# echo "IP is $IP"

IP=`echo $IP | cut -d "(" -f2 | cut -d ")" -f1`
# echo $IP|xclip
# echo -e "[OK] IP  \t:\t ${IP} 
# IP exported as global variable and to the clipboard."
echo $IP

# script geny_save.sh
source geny_devices.sh
VM=${s3_43}

echo "VM is \"$VM\""
VBoxManage snapshot $VM restore snap1 &

# script geny_start.sh
source geny_devices.sh
VM=${s3_43}

echo "VM is \"$VM\""
VBoxManage snapshot $VM restore snap1 &
VBoxHeadless -s $VM &

IP=`./geny_ip.sh`
echo ">>>>>>" $IP

adb tcpip 5555
adb connect $IP:5555

#restart adb as root to allow powering it off
#root mode is generally what we want from a headless emulator (to download emma files for instance)
adb root
adb connect $IP #:5555

#script geny_stop.sh 
IP=`./geny_ip.sh`

adb root
adb connect $IP:5555
adb shell reboot -p &

```

Community
  • 1
  • 1
guneysus
  • 5,360
  • 2
  • 36
  • 35
2

In distros GNU/Linux

It's easy

 cd genymotion/

In this folder,you need find file player

enter image description here

Now you need the device name

enter image description here

In your terminal, write this command,replacing NameDevice for your device name

 ./player --vm-name <NameDevice>

enter image description here

enter image description here

And now your emulator started

enter image description here

In GNU/Linux you can create access in the menu

enter image description here

enter image description here

Good Luck

David Hackro
  • 3,276
  • 5
  • 35
  • 56
1

Thanks to @k s answer I was able to launch the Geny motion emulator on Mac but I had to make few changes for Mac OS Sierra 10.12.6 and GenyMotion 2.10.0

/Applications/Genymotion.app/Contents/MacOS/player.app/Contents/MacOS/player --vm-name "xxxx"

and to kill it

ps | grep "/Applications/Genymotion\.app/Contents/MacOS/player\.app/Contents/MacOS/player" | awk '{print$1}' | xargs kill

Hope it helps someone.

Satheesh
  • 10,292
  • 6
  • 47
  • 89
1

Just in case anyone doesn't know about environment variables, looking for a non-headless and using Windows, you can check the commands by running the following command where it's installed your VirtualBox:

C:\Program Files\Oracle\VirtualBox list vms

Then you can run your desired device with something like the following:

C:\Program Files\Genymobile\Genymotion\tools player --vm-name "Google Nexus 4"

Of course, put that paths on your environment variable would be the better approach.

Felipe Augusto
  • 5,811
  • 7
  • 29
  • 60
-1

Command to launch genymotion from command line -

player --vm-name Nexus_4

if player is not already added to path, add it to path using below command in your ~/.bash_profile

export PATH=/Applications/Genymotion.app/Contents/MacOS/:$PATH

When more than one device is connect use 'adb -s' is used to redirect commands to particular device Once emulator is running they will be listed under adb devices

Example:

adb devices
List of devices attached 
192.168.56.101:5555 device

Send command to click on menu key on android device when multiple devices are connected:

adb -s 192.168.56.101:5555 shell input keyevent KEYCODE_MENU 
Felipe Augusto
  • 5,811
  • 7
  • 29
  • 60
Tejasvi Manmatha
  • 472
  • 6
  • 10