Android Development with Ubuntu in VirtualBox
Posted by binglongx on March 4, 2013
If you are a Windows user but want to try Android Development on Linux, you do not have to set up a Linux box. Today’s PCs are powerful enough to install a virtual machine for that purpose. Both Oracle VirtualBox and VMware are fine. This is a summary for setting up Ubuntu 12.10 on VirtualBox for Android development, including NDK.
Install Ubuntu VM
- Install Oracle VirtualBox on Windows 7.
- It is possible that you just download a prebuilt virtual machine; I choose to install myself so as to have better control.
- Download Ubuntu Desktop 12.10 iso file. This file will be mounted as CDROM media, and will only be used once for installing the guest system in VirtualBox.
- Create a virtual machine in VirtualBox, using the above iso file under Storage as CDROM. I also check Enable 3D Acceleration under Display, and allow as many processors as possible.
- Install Ubuntu in the VM. Take a note of the username and password you choose!
- After Ubuntu installation is done, remove the iso from VM Settings | Storage.
Install Guest Additions
You should install VirtualBox Guest Additions in Ubuntu VM. Without Gest Additions, the Ubuntu VM has a lot of annoyances, such as a small screen size, unable to share clipboard with host OS, and so on. For unknown reason, the official approach does not work for me (some error message like “Unable to mount media due to lock protection”), and below was what I did:
- Locate VBoxGuestAdditions.iso, which comes with VirtualBox installation, for example C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso.
- In VM Settings | Storage, add this iso as CDROM media.
- Start the VM, run the following in Terminal:
sudo mkdir /media/cdrom
sudo mount /dev/cdrom /media/cdrom
sudo sh /media/cdrom/VBoxLinuxAdditions.run
- The above should install Guest Additions to the guest Ubuntu OS. Shut down VM.
- In VirtualBox VM Settings | General | Advanced, set Shared Clipboard to Bidirectional. Start VM.
- You should be able to copy and paste seamlessly between Ubuntu guest and Windows host. If you resize your guest os window, the Ubuntu screen should automatically fit the window. This indicates Guest Additions installed successfully.
- Remove the iso from VM Settings | Storage.
Eclipse and Android Development require Java. I did not try OpenJDK, thinking Oracle Java7 is better supported. Due to Oracle’s license, Ubuntu does not come with Oracle Java installed. We have to download and install it. Refer to Install Oracle Java 7 in Ubuntu via PPA Repository. In summary, you only need to run below in Ubuntu:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
The last two commands are to verify if Oracle Java7 was installed successfully.
Install Eclipse and Android Development Environment
Although Eclipse is not mandatory for Android Development, most people use it as the IDE. From my experience, I discourage installing Eclipse in Ubuntu using package management tools like Synaptic Package Manager. Because they tend to install Eclipse to system directories with root permissions, it creates a lot of problems when we need to update Eclipse with plugins and updates that are not available through those package management tools, for example, those for Android development.
This simplest solution is to install the ADT Bundle from Google, which includes Eclipse, ADT, and other Android development tools as all-in-one. Just download the zip file, and extract to a directory under your home directory, such as /home/myusername/adt_bundle. Do NOT install other components under the same directory – as you may know, Eclipse is fragile and some files there will be corrupt, and there are chances that you have to reinstall it. My simplest approach is, delete the adt_bundle directory altogether, then re-extract the Google ADT Bundle zip there.
It is a bit inconvenient to use the stand-alone Eclipse under your own home directory, so please create a Desktop shortcut to quickly launch Eclipse.
We are almost there. We can now run Eclipse and create/build Android applications. But we cannot run the applications because there is no device to run them. In a separate post, connection of physical device to Ubuntu in VirtualBox is discussed. If you do not have a device available, Google is fair enough to provide an emulator and create AVDs (Android Virtual Devices) using AVD Manager. You can launch AVD Manager from Eclipse menu Window | AVD Manager. Walk through Google’s tutorial to create the first Android project to verify the development environment.
If you need to update/install additional SDK APIs other than included in the ADT Bundle, run Android SDK Manager from Eclipse menu Window | Android SDK Manager.
Install Android NDK
If you only write Java Android applications, you do not need NDK. NDK is however a must if you need to write or use low-level or performance savvy C/C++ code for your Android application through JNI.
Installing NDK is very easy.
- Go to Google Android NDK page, download the NDK package file, and extract it anywhere. I choose to use a directory under home, such as /home/myusername/android_ndk. I avoid to put it in par with Android SDK under adt_bundle, for the reason mentioned earlier.
- In Eclipse, navigate to Window | Preferences | Android | NDK, then set the path to the extracted directory, click Apply and OK.
- Now in Eclipse, you can right click your Android project, choose Android Tools | Add NDK Support. This will create jni/Android.mk for you, and when you build/run your Android project, Eclipse will automatically run ndk-build for you. In earlier Eclipse, you have to resort to a terminal window to manually run ndk-build to build the native libraries.