Running a full Android OS image on a desktop PC inside an Oracle VirtualBox virtual machine can be a valuable alternative to KVM and QEMU on Windows host systems, especially for those developers looking to develop and debug native code on Android.
In this section, we share some details about:
Building the Android 4.0.x VirtualBox installer for x86 from the official Google x86 VirtualBox target vbox-x86-eng (included in Android 4.0.1 source tree).
Using a Linux 2.6 kernel provided by Intel to add specific features for VirtualBox.
How to use installer.vdi to install Android ICS 4.0 into VirtualBox.
In addition to Google’s AVD emulator supported by Intel Hardware Accelerated Execution Manager (Intel HAXM) technology, Android for x86 VirtualBox is running a true virtualized Intel architecture–based environment. It thus provides another fast, high-performance tool for developers and partners for quick application development and testing. Booting Android 4.0.x in VirtualBox on a typical Intel Core i5 host system takes about 10 seconds. Speed, performance, and user experience account for the popularity of VirtualBox among Android developers, especially when targeting platforms based on Intel architecture. Availability of Android tablets and smartphones on the market based on Intel architecture is still somewhat limited, and it can be more convenient to start development using a virtual environment on the development host system instead of relying on USB debug communication. Especially with both the development host and the Android target device based on Intel architecture, this becomes a valid and interesting alternative.
Google x86 VirtualBox Build Targets for Android 4.x
If you have been using Google’s Android 4.0.x source repository before, you probably noticed that Google provides an x86 version of the VirtualBox target vbox_x86-eng. Using the lunch command before starting the build, the first three targets that Google provides for Android 4.0.x are:
With the vbox_x86-eng (#3) target, application developers and system developers alike can create android_disk.vdi and android installer.vdi packages. These can then be used to run Android 4.x inside VirtualBox for application development and system integration on Windows, Linux, and OS X.
Downloading the Source Tree and Installing the Repository
To install, initialize, and configure the repository, follow these steps (you can find more information at
$ mkdir ∼/bin
$ chmod a+x ∼/bin/repo
$ mkdir ANDROID_TOP_PATH
$ cd ANDROID_TOP_PATH
To get a list of available branches (from your Android repository checkout root), use this command:
$ git --git-dir .repo/manifests/.git/ branch –a
Run repo init to obtain a current list of the available repository sub-branches with all the most recent updates:
$ repo init -u
To use the Gerrit code-review tool, you will need an e-mail address that is connected to a registered Google account. Make sure this is a live address at which you can receive messages. The kernel version and build number will be assigned to your build, and information will be displayed on the Android/Settings/About Phone/ page.
A successful initialization will end with a message stating that Repo is initialized in your working directory. Your client directory should now contain a .repo directory where files such as the manifest are stored.
To pull down files to your working directory from the repositories, as specified in the default manifest, run
$ repo sync
By default, access to the Android source code is anonymous. To protect the servers against excessive usage, each IP address is associated with a quota.
Building a Custom Kernel with Mouse Support
Since Android is meant for touch screen devices, it doesn’t include support for a mouse pointer by default. Additionally it may not include a driver for hardwired Ethernet support, as most Android devices are exclusively using wireless radios for network communication. To add these capabilities, you need to rebuild the kernel with mouse support as well as any additional features that you require. To do so, follow these steps:
Download the Android x86 Emulator Image add-on through the Android SDK Manager.
Create a new folder and untar kernel_sdk_x86.tar.gz into it to create a folder with the kernel source tree.
Switch to the directory that holds your kernel files.
Now that you have the kernel source, the configuration needs to be modified to match the hardware being used as the VirtualBox host system and rebuilt. The menuconfig graphical user interface provided by the kernel sources will allow to do this conveniently:
$ cp ANDROID_TOP_PATH/your_kernel_path/arch/x86/configs/vbox_defconfig .config
$ make CC=gcc-4.4 CXX=g++-4.4 ARCH=x86 menuconfig
This will take a few seconds to compile and load. Once it does load, you can use
To enable mouse support, navigate to Device Driver ➤ Input Device Support ➤ Mice.
menuconfig can be used to ensure that all features required to support your application or system integration are available. For application developers, it is equally important to test and validate the application on default Android builds. Only then can maximum compatibility with Android devices from multiple different device manufacturers be guaranteed.
Having made the necessary change to the kernel configuration, you can now compile it. It doesn’t take too long, so I picked a low –j value. It is important to note that if you omit the CC and CCX parameters, the compile will terminate prematurely (on this setup), without an explicit error, as it will use version 4.6.
$ make CC=gcc-4.4 CXX=g++-4.4 ARCH=x86 –j8
The –j parameter provides the number of available cores for compilation. This example assumes a quad-core system with Intel Hyper-Threading Technology enabled.
After the build is completed successfully, the last line of the build log will say
Kernel: arch/x86/boot/bzImage is ready
Add Patched Kernel
The kernel image bzImage needs to be renamed to kernel-vbox and copied to /ANDROID_TOP_PATH/prebuilt/android-x86/kernel/kernel-vbox:
$ cp /ANDROID_TOP_PATH/kernel/arch/x86/boot/bzImage/ANDROID_TOP_PATH/prebuilt/android-x86/kernel/kernel-vbox
Reduce Compile Time Using CCACHE
You can greatly reduce the compile time for subsequent compilations by using the compiler cache. To set up a 50GB cache, do the following:
Install the CCcache program and create a directory for your ccache:
$ sudo apt-get install ccache
$ sudo mkdir /ANDROID_TOP_PATH/ccache
$ sudo chown $LOGNAME /ANDROID_TOP_PATH/ccache
Set up your environment variables for ccache support by modifying ∼/.bashrc:
$ sudo gedit ∼/.bashrc
Add the following:
Set the ccache sizes.
$ ccache -F 100000
$ ccache -M 50G
Build Android 4.0.x with New Kernel
To set up the environment:
$ /ANDROID_TOP_PATH/> source build/envsetup.sh
For ICS 4.0.1, you will see:
To ensure you pick a valid target when using the lunch command, it is convenient to simply do the following:
and pick the desired target from the list:
Which would you like? [full-eng]
Make sure you select 3. vbox_x86-eng.
$ make -j8
Build the VirtualBox Disk and Android Installer
Finally, build the Android_disk.vdi and installer_vdi together:
$ make android_disk_vdi installer_vdi -j8
If the build is successful, you will see this output in the build log:
Done with VirtualBox bootable disk image -[ out/target/product/vbox_x86/android_disk.vdi ]-
Done with VirtualBox bootable installer image -[ out/target/product/vbox_x86/installer.vdi ]-
With android_disk.vdi, you can select New ➤ Create New Virtual Machine in VirtualBox. You can use android_disk.vdi as an existing disk image to base the virtual machine on, which will now automatically boot Android 4.0.x.
Setting up the start parameters for VirtualBox is straightforward. Go to Settings ➤ About This Phone to see the build information shown in Figure 8-17.
Using an Android Installer Disk to Create a Large Virtual Partition
Even if the file size in VirtualBox is expanded to 550MB, it is still too small to load applications for testing. An Android installer disk is needed to install Android into a much larger partition (preferably larger than 4GB).
First select Machine ➤ New from the VirtualBox menu bar and use the Create New Virtual Machine Wizard to create a larger disk under the IDE controller configuration entry.
Then in Settings ➤ Storage of your virtual machine, add the installer.vdi as the primary IDE slave, as shown in Figure 8-18. Now you can install Android on the 4GB hard disk you just created.
Start the emulator.
Use F12 to get to the BIOS boot menu. Boot to the secondary drive.
Use grub to select the Install option: 2. Boot from the primary slave (see Figure 8-19).
When you see Done processing installer config, type reboot.
The first time you install on your target virtual disk, the install may fail. A message will be printed that tells you to run the installer again. Do so by using the following command:
After reboot, you can see that your Android is running from the larger disk that you create, and you can safely remove installer.vdi from storage under the VirtualBox IDE controller settings.
Serial port support in the virtual machine is enabled by default. However, the COM1 serial port needs to be initialized and configured before it can be used. The following instructions cause VirtualBox to create a named pipe called .vbox_pipe in your home directory. On the command line, enter:
$ VBoxManage modifyvm Android --uart1 0x03f8 4
$ VBoxManage modifyvm Android --uartmode1 server /home/user/.vbox_pipe
Alternatively from in the VirtualBox GUI, use the Serial Ports tab in the Virtual Machine Settings menu to enable COM1 as a host pipe. Select Create Pipe for it to be created as /home/user/.vbox_pipe.
To connect to this named pipe, use:
$ socat unix-client:$HOME/.vbox_pipe stdout
VirtualBox might not understand environment variables (such as $HOME), so you will have to specify a full explicit path, such as /home/user/.vbox_pipe.
The Ethernet port (eth0) is enabled for DHCP in the image. To connect to it via ADB, you will need to look up the DHCP address that has been assigned.
If you are using a bridged ethernet, you may obtain this address from a shell prompt, either from the serial port or from Developer Tools ➤ Terminal Emulator using this command:
If you are using a host-only adapter, vboxnet0, you should use the address 192.168.56.101.
Now you have a VirtualBox image running Android 4.0.x completely built from Google’s official vbox-x86 target (see Figure 8-20).