Thursday, December 01, 2011

Android 4.0 (Ice Cream Sandwich) on Virtualbox

Yesterday to my utter delight I realized that the Ice Cream Sandwich version of Android (4.0) can run in Virtualbox. I've been contemplating programming an Android app for the tablet form factor but don't quite have the resources to purchase a spanking new ICS tablet, and a virtually free virtual machine fits the pocketbook much better.

For Android ports, I normally turned to the venerable Android-x86 project. Indeed, they have just released an ICS port designed for the AMD Brazos platform, but the build apparently does not support internet connectivity or use of a mouse, which of course makes use within a virtual machine on a laptop rather challenging. Fortunately, I found another blog posting that referenced a Japanese site whose developer has provided a .vdi virtual hard disk file for loading directly within Virtualbox. Best of all, this not only includes mouse support and networking, but also hooks up with ADB for Android app development.

To set up the Virtualbox environment, I followed these steps:
  • Download the pre-made .vdi file with Android 4.0.
  • Create a new virtual machine with the following settings:
    • OS Type Linux 2.6
    • 512MB RAM
    • Acceleration: VT-x/AMD-V (which is apparently required for ICS at least on this build), nested paging, and PAE/NX
    • Bridged network adapter (but see below)
    • 2GB hard drive (not required)
    • Hard disk pointing to the .vdi file. Note that the disk will be attached by default to a SATA controller, but the disk should be removed from there and attached to the IDE Controller instead.
The virtual machine booted and worked out of the box! Next, I needed to connect the virtual machine to ADB so that I could load up my app for testing:
  • Open a command-prompt in the directory of adb.exe (ie Shift-right-click in android-sdk-windows\platform-tools and choose the "Open command window here" item).
  • Enter adb connect [ip-address]. This turns out to be the most difficult step because unlike using netcfg in the Android-x86 builds, I haven't found an easy way to determine the local IP address. One method is to log into your router and check the DHCP client list. The list usually shows IP addresses for the various MAC addresses of devices connected to the router. You can find the MAC address of your device by right-clicking on the networking icon in the Virtualbox status bar, choosing "Network adapters...", and then opening the "Advanced" section.
One of the challenges I've faced while on the road is the inability to connect while completely disconnected from the internet or any router. In this case, I need a way to make a solely local network connection from ADB to the virtual machine. Fortunately, this posting describes a simple method to make that connection:
  • In the same network adapters page from within Virtualbox, change the "Attached to" selection to "NAT".
  • Choose "Port forwarding", and in the "Host Port" and "Guest Port" sections, type "5555". If you have multiple virtual machines and want to connect to each of them, you could increment the host port for each of the forwarding rules while keeping the same guest port for each device (eg host port 5556, guest port 5555 on a second virtual machine).
  • From the command-prompt in the directory of adb.exe, enter adb connect localhost.

The Android 4.0.1 device should pop up in the DDMS devices listing from within the Eclipse development environment, and packages should transfer to ICS with glee.
Post a Comment