How can I script genymotion emulator to launch a given avd, headless? How can I script genymotion emulator to launch a given avd, headless? android android

How can I script genymotion emulator to launch a given avd, headless?


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


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.shVM=6a5d9245-b751-47aa-b38d-989c5f1a9cfbecho "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.shVM=6a5d9245-b751-47aa-b38d-989c5f1a9cfb#find mac of vm#http://stackoverflow.com/questions/10991771/sed-to-insert-colon-in-a-mac-address# Update arp tablefor i in {1..254}; do ping -c 1 192.168.56.$i 2&>1; doneMAC=`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 ':' notationIP=`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.shVM=6a5d9245-b751-47aa-b38d-989c5f1a9cfbecho "VM is \"$VM\""VBoxManage snapshot $VM restore snap1 &VBoxHeadless -s $VM &IP=`./genymotion-detect-ip.sh`echo $IP#adb tcpip 5555adb 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 rootadb 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 rootadb connect $IP:5555adb 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.


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.shs3_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.shsource geny_devices.shVM=${s3_43}# Hopefully performance improvement ;) Not really necessary# for in in {1..254}; #     do ping -c 192.168.56.$1 2&>1;# doneMAC=`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.shsource geny_devices.shVM=${s3_43}echo "VM is \"$VM\""VBoxManage snapshot $VM restore snap1 &# script geny_start.shsource geny_devices.shVM=${s3_43}echo "VM is \"$VM\""VBoxManage snapshot $VM restore snap1 &VBoxHeadless -s $VM &IP=`./geny_ip.sh`echo ">>>>>>" $IPadb tcpip 5555adb 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 rootadb connect $IP #:5555#script geny_stop.sh IP=`./geny_ip.sh`adb rootadb connect $IP:5555adb shell reboot -p &

```