Tuesday, June 5, 2012

Android on a Virtual Machine

The Android emulator that is shipped with the Android SDK is very slow to load and work with. 
Fortunately, there is a faster alternative: Android-x86 on a virtual machine.
This guide will show you how to install Android-x86 on VirtualBox and connect it to adb.

Downloading and installing prerequisties

  1. Download and install VirtualBox.
  2. Download android-x86-2.3-RC1-eeepc.iso from the Android x86 project site

Creating a virtual machine in VirtualBox

  1. Open VirtualBox and press the New button to open the "Create New Virtual Machine" wizard.
  2. [Welcome to the New Virtual Machine Wizard]
    Next
  3. [VS Name and OS Type]
    Name: Android
    Operating System: Linux
    Version: Other
    Next
  4. [Memory]
    Base Memory Size: 256MB
    Next
  5. [Virtual Hard Disk]
    Select Create new hard disk
    Next
  6. [Welcome to the virtual disk wizard]
    Select VDI (Virtual Disk Image)
    Next
  7. [Virtual disk storage details]
    Select Fixed Size
    Next
  8.  [Virtual disk file location and size]
    Location: select a directory for a 4GB file
    Size: 4GB
    Next
  9. [Summary] (of the "Create New Virtual Disk" wizard)
    Next
  10. [Summary] (of the "Create New Virtual Machine" wizard)
    Next
  A new virtual machine named "Android"  will show up in the virtual machines list in VirtualBox.
  

Configuring the virtual machine

  1. Select the newly created "Android" virtual machine and press the Settings button.
  2. In the Storage tab, press the Add CD/DVD Device icon, press Choose Disk, and select the android iso image file you downloaded earlier.
  3. In the Audio tab, select SoundBlaster 16 as the Audio Controller.
  4. Press the OK button to save all settings.

 Installing Android-x86 on the virtual machine

  1. Select the "Android" virtual machine and press the Start button.
    The virtual machine will start with the Android-x86 iso disk image in its virtual disk device, and a menu will show up.
  2. Select Installation - install Android-x86 to harddisk. The cfdisk utility will show up.
  3. Using the right and left arrow keys, select New and press Enter.
  4. Using the right and left arrow keys, select Primary and press Enter.
  5. Leave the size field as is and press Enter.
  6. Using the right and left arrow keys, select Bootable and press Enter.
  7. Using the right and left arrow keys, select Write and press Enter.
  8. Write yes to confirm the write operation and press Enter. The parition table will be written to disk.
  9. Using the right and left arrow keys, select Quit and press Enter. This will bring you back to the previous menu, this time a new menu item called sda1 will be added.
  10. Using the up and down arrow keys, select sda1 Linux VBOX HARDDISK and press Enter.
  11. Using the up and down arrow keys, select ext3 and press Enter.
  12.  Press Enter to confim disk formatting. Disk will be formatted.
  13. Using the right and left arrow keys, select Yes to install boot loader GRUB and press Enter
  14. If you intend to develop Android application on this virtual machine, which I'm sure you are, select Yes  using the right and left arrow keys to let /system directory be read and write, and press Enter.
    Androidx86 will be installed into the harddisk.
  15. Using the up and down arrow keys, select Create a fake SD card and press Enter.
  16. Enter 512 or other size for the fake SD card and press Enter. SD Card image will be created.
  17. Press Enter to Reboot the virtual machine. The machine will restart and the installation menu will show up again since the virtual Android-x86 disk is still attached to the VM (Virtual Machine).
  18. On the menu of VirtualBox select Devices, CD/DVD Devices, IDE Controller (IDE Primary Master), Remove disk from virtual drive. This will virtually eject the virtual Android-x86 disk from the VM.
  19. On the menu of VirtualBox select Machine, Reset and confirm the operation in the confirmation dialog that appears. The VM will be reset and than the GRUB boot loader will appear. If no action taken, Android will show up in a few seconds.
    Android-x86 on VirtualBox
  20. If the mouse pointer disappears when you move the mouse over the VM's window, than mouse integration should be disabled.
    On the menu of VirtualBox, select Machine, Disable Mouse Integration.
  21. When you click inside the VM's window, the mouse will be captured by VirtualBox to the sake of  the  VM. When you want your mouse back, click the right CTRL key.

Connecting the VM to adb

So far we have an Android VM on a virtual network, connected to the internet by NAT. VirtualBox acts as a router: it sends all requests from the VM as if they came from VirtualBox itself, listens for responses and feeds them back to the VM. The host (the PC) cannot "see" the VM since VirtualBox forwards only responses to the VM's requests into the VM. So how can adb that resides on the host, connect to the Android VM we have just created? 
Port Forwarding to the rescue. We will make a rule in VirtualBox such that every packet that is destined to port 5555 on the host, shall automatically be forwarded to port 5555 on the virtual machine. Using this rule, when we'll want to connect adb to the VM, we will actually connect to the host itself, ie 
adb connect 127.0.0.1  or adb connect localhost.
So let's get started:
  1. Close the VM's window. "Close Virtual Machine" dialog will appear. Select  Power off the machine and press Enter.
  2. On VirtualBox, select the "Android" VM and press the Settings button.
  3. Go to the Network tab, expand the Advanced options and press the Port Forrwarding button. The "Port Forwarding Rules" dialog will show up.
  4. Click the Inset new rule button. A new raw will appear in the rules list.
  5. Set Name: adb, Protocol: TCP, Host Port: 5555, Guest Port: 5555 and click the OK button.
  6. Open a terminal (on linux) or console (Windows: Start, Run, cmd) and navigate to directory platform-tools under the android-sdk root directory.
  7. Write adb connect localhost and press Enter. adb will be connected to our VM.
    You just have to select it in Eclipse or Intellij IDEA.