July 14, 2014

Using a VMware Android emulator in Visual Studio

This is not a "manual" on how to create a VMware android virtual machine!
This is just a hint on how to use a virtual andoid in your Visual Studio environment... I'm not going to explain how to create the android vmware, just how to "connect" to it...
For android x86 install instructions, please goto the xmodulo site.


My problem


I'm using Xamarin to create mobile applications. When you install the development tools for your Visual Studio instance, it installs (among many other things) the "AVD Manager"
This "Android Virtual Device Manager" allows you to create virtual android devices to test your applications. In theory this is a nice feature. You don't need an android device to test your app... In practice however it sucks...
The virtual androids are extremely slow and -more of a problem- have maximum available memory issues when emulated in a windows environment.


problem solved?


The first step was to download a x86 android ISO and create a android virtual machine.

This is a relatively easy excercise.

There was a little caveat with the network adapter. It should be changed to "vlance" before the first start of the virtual machine, but other then that it was easy enough.

The virtual android was up and running within 30 min. including downloading the ISO.
However... as always this was too easy... For good reasons -which I will not elaborate on- my development environment is also running in a virtual machine! How did I connect the virtual android to the Visual Studio enviroment in another virtual machine?


Problem solved!


Turns out it's just a small trick...
When Visual Studio starts (with Xamarin installed)  it searches for local(!) available android devices. To connect to the (remote) android virtual machine you should open the adb command prompt:

ADB command prompt


At the command prompt you connect to your virtual android using the command:
adb connect 192.168.1.110
Where 192.168.1.110 is the ip-adress of my android emulator. (The IP will probably be different in your environment ;-) You can check if you successfully connected using
adb devices
If you are connected, it should list the vm in the devices list.

You can find the android emulator ip by running the android console with ALT-F1**, then run netcfg to get the ip address. With ALT-F7** you return to the android GUI.. 

If all turned out okay, your vmware device should be listed in your available devices for debugging:
VMware, virtual android device
  
** NOTE: In my android (4.3) ALT-F1/F7 switched console/gui, however I've read CTRL-F1/F7 might do the trick in different x86 ISO's/versions.


Hints and tips...


The vmware emulator is certainly not a perfect solution, but it is a lot faster than the standard java SDK emulators. And if you're not using all kinds of sensory input it's a real alternative!
When your unable to connect, check firewall settings. An android adb connection typically connects on port 5555. Make sure it's not blocked.
In your android emulator you might need to set your netwok configuration to make this all work (see the batch file below)

You can very easily change your device screen size on the android emulation. Open the console (ALT-F1) and set your desired resolution:
wm size 1280x800

I've create a batch file to connect to my emulator(s). The batchfile has a dependence on Visual Studio Environment variables. It's located in the vs folder
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC

The shortcut on the desktop uses the %comspec% variable:
%comspec% /c ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\connectvmwaredroid.bat""

If I did not include the envronment variables, the connection got lost when Visual Studio starts up? It -most likely- resets the adb..

 @ECHO OFF  
 ECHO ==================================================================  
 ECHO In the android vmware: press ALT+F1 to open console  
 ECHO Then enter netcfg to find the emulator IP  
 ECHO If eth0 is not UP then enter (in the android emulator console):  
 ECHO netcfg eth0 up  
 ECHO netcfg setprop net.dsn1 8.8.8.8  
 ECHO ALT-F7 to close the console  
 ECHO ==================================================================  
 ECHO - set dev environment settings...  
 if not exist "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat" goto missing  
 call "%~dp0bin\x86_amd64\vcvarsx86_amd64.bat"  
 cd %LocalAppData%\Android\android-sdk\platform-tools  
 ECHO - disconnect all devices...  
 adb kill-server  
 SET /P emu_ip=Enter the emulator ip:   
 :connect  
 ECHO - Connecting emulator...  
 rem ping %emu_ip%  
 adb connect %emu_ip%  
 adb devices  
 SET /P emu_ip=Next emulator ip (x to exit):   
 IF "%emu_ip%" == "x" goto done  
 goto connect  
 :missing  
 ECO Could not set environment variables. Connection to emulator failed...  
 :done  

Hope this post was helpfull to anyone :-)