Wednesday, June 5, 2013

Using a VirtualBox Android Emulator




I am now using Genymotion Emulators, 

The slowness of the Android emulator has been the butt of many a joke since it was first unveiled. While Google has done a commendable job of making each iteration of the emulator faster, it is still painfully slow. Most Android developers simply debug to a device and avoid the headache. But that is still far from an ideal solution. I for one, don't want my phone tied to my dev box. Also when I give talks, it is nice to be able to easily project my work, holding my phone in the air for the audience is not an ideal solution.

I ran across a blog post by Chris Muir of Oracle, which offered a way out of emulator hell. The solution is to use a VirtualBox VM with Android on it instead of the Android emulator. Before you go all out and start putting together your own VM, be forewarned that entails a lot of work. You have to setup an x86 VM, deploy Android O/S to it, install software, etc. A lot of work. Luckily, the good folks at AndroVM have already done this work. The difference is amazing. I have tried it on both Mac and Windows.

Assumptions
This post is not to help you with setting up your Android development environment. I am assuming that your have already done that. The Android Device Bridge, which is included in the Android Developer Tools (ADT), should be installed and available on the command line (Windows) or terminal (Mac/Linux).

To begin, we need to download VirtualBox from VirtualBox.org. This is Oracle's excellent and free virtual machine app, and yeah I know a company named Innotek originally created it, then Sun bought them and Oracle bought Sun. Once you have VirtualBox installed proceed to AndroVM and download the VMs. My recommendation is to grab the following:

  •  androVM_vbox86tp_4.1.1_r6.1-20130222-gapps-houdini-flash.ova
  •  androVM_vbox86p_4.1.1_r6.1-20130222-gapps-houdini-flash.ova
These are the tablet (tp) and phone (p) versions of Android 4.1.1 with Google apps and other stuff. You will also be able to download, assuming you have a Google account, stuff from the Play Store. Once you have the VMs downloaded as OVA files you will need to import them.



With both VMs imported into VirtualBox I would recommend that you clone each and only use the cloned version. This is just a safety precaution against accidentally screwing up one of VMs.




To clone a VM simply right click it. Choose clone. Give the new machine a name. Mine's is x86Phone and x86Tablet, for the phone and tablet respectively. Choose the Full clone.






Then click the Clone button. It shouldn't take too long for the new VM to be available. Then right click one of the new VMs and click Settings... Click the Network tab. I have only been able to get the Bridged Adapter working. Change the connection so it matches the one below. A network connection is critical, without it you won't be able to deploy and debug your apps to the VM. 




 Click the Start arrow. In just a few seconds, yes seconds, your VM should spin all the way up. Unlock it and find the AndroVM Configuration program. It is usually on the home page in the lower left hand corner. Click it. It should display your emulator's IP address in the upper left corner. If it doesn't, STOP. You may need to go back to the network settings and jiggle them. 



In order to deploy to the VM, the ADB needs to know that it exists. Simply open a command or terminal window and type the following command: 


adb connect <ip address>



Note that the ip address is the same one shown by the AndroVM Configuration app. Now that the ADB knows it is there. Let's deploy something to it. First, we need to change our configuration to point to a USB device and not the emulator.



And we have a fast emulator. And it works like any other emulator except it is really fast.

Beware. This is not a Google sponsored project, so don't expect any kind of support. Also AndroVM has recently been acquired and it will be re-released in a few months as a commercial product. So if you are at all interested in a fast emulator, go and download it now!