Setup Android Emulator in KVM Virtual Machine 2
It is possible to run the Android development environment inside a KVM virtual machine. Below is how.
Create a Virtual Machine
I used virt-manager to install an x64 Ubuntu 10.04.1 Server virtual machine. KVM x64_AMD. Why? Because I had the x64 Ubuntu Server ISO already available.
- 15GB of storage
- 2GB of RAM (we may need more later, but 2GB seems the min to run 1 Android VM)
- 2 CPUs
- Connected the network to a pre-existing bridge
- Virtio for both storage and networking
Let that install run and only select ssh server for the optional questions. This is the Ubuntu x64 Server, not a desktop. I suppose you could use a desktop install, but do you really want all that bloat? The server install will mean we get to fix a few things later, but things that didn’t fit in 10GB will fit in 4.3GB when we’re done.
Use virt-manager to set the VM up. There are lots of tutors on the internet for setting up KVM virtual machines on the internet. You want a minimal install to minimize bloat.
Inside the VM
Boot the VM and we need to do some standard things before we get too far. We’re using the KVM console here. It is probably a little slow. We want to get out of this ASAP, but need to setup a few things before we never use this tool again.
- sudo passwd root (set the root password – use a password manager, random, 30+ characters)
- sudo -s (become root)
- apt-get update
- apt-get dist-upgrade (this will get a new kernel)
- vi /etc/network/interfaces (set a static IP)
- reboot
When the machine comes back up, ssh -X in from your favorite desktop. Copy/paste will work better, so things will go much quicker. System response will be 10x faster too, assuming your desktop is on the same LAN as the server.
Install a Few Packages
We don’t need an X-Server, but we do need all the X-Client libraries along with a few other maintenance things that every server should have. Satellite email, cron, a lite programmers editor, xterm and we need the ia-x32 compatibility libraries for Android development to work.
As root:
- apt-get purge nano (I HATE that editor)
- apt-get install postfix heirloom-mailx cron geany xterm ia32-libs
- vi /etc/aliases (forward all email to yoor personal account on a normal mail server)
- newaliases
- vi /etc/apt/sources.list (uncomment 2 lines for partner repos to work)
- apt-get update (to see some new package options)
- apt-get install sun-java6-jdk sun-java6-jre
- Visit the Android SDK page and download it. We have android-sdk_r11-linux_x86.tgz here.
- While we wait for the download of 270GB, set the JAVA_HOME for everyone inside /etc/environment
- sudo vi /etc/environment
- Add JAVA_HOME=“/usr/lib/jvm/java-6-sun-1.6.0.24” to the file. Obviously the specific version needs to match the version on your system. ls /usr/lib/jvm/java* will tell you which version was installed.
- move ~root/andro*tgz ~userid/
- chown userid.userid ~userid/and*tgz
- Logout from the root account
Login as “Your” Account
Now we need to be “you” and login with X-Forwarding enabled.
- ssh -X userid@kvm_android_machine
- $ tar zxvf andro*tgz
- $ cd android-sdk-linux_x86/
Here’s what you have:
jp@android55:~/android-sdk-linux_x86$ ls
add-ons extras platform-tools SDK Readme.txt tools
docs platforms samples temp
jp@android55:~/android-sdk-linux_x86$ ls tools/
adb_has_moved.txt dmtracedump hprof-conv NOTICE.txt zipalign
android draw9patch layoutopt proguard
ant emulator lib source.properties
apkbuilder etc1tool mksdcard sqlite3
ddms hierarchyviewer monkeyrunner traceview
jp@android55:~/android-sdk-linux_x86$ - run tools/android &
The Android emulator environment should display on your desktop PC (provided it is running an X-Server). If you aren’t running an X-Server, delete that virus (you probably call it MS-Windows) and install Linux. ;) I’ll be happier. Oh, and
Create an Android Virtual Machine
Inside the emulator, Android versions 1.5 – 3.1 (at the time this article was written) should be available. You don’t need to set anything other than the name (whatever you like – usually based on the program, not the version of Android), then you may want to manually set the screen size. On my system, the screen size is scaled to be .65 the requested size, so a 12 inch tablet screen could be useful on a desktop. The default screen size may be just fine, however.
Issues
After all this, you will probably notice a few things.
- Sound – what’s that?
- Microphone – huh? How do I plug in a microphone to a virtual machine?
- Slow – I’ve noticed that this VM with 2 Cores of a relatively speedy Core i5-750 CPU runs at 100% when the VM is active.
- Booting takes about 2-4 minutes. Ouch.
- Mouse actions take the place of using your finger, so start thinking like you are on a tablet. Mouse inputs are often delayed in my experience.
- Cell plans, GPS, rotation, probably aren’t gonna work either. There is no substitute for a real Android device.
Anyway, you have an Android emulator that can browse the web and do many of the things that an Android tablet can.
Why Not Just Load these Tools Directly in the Base Operating System?
Simple, Java is too dangerous to have on an internet connected PC for daily use. Java is a major method to infect computers running any operating system by nasty people out on the internet. By making this VM specific to Android emulation and development, we’ve reduced the risks for our other daily-use environments.
It is also easy to share this complete environment with someone else. Just shutdown the VM and copy the file over to their PC. Be certain they are on a different subnet when they bring the VM up or your IP addresses and hostnames will collide, which is never good. Before you give them this VM, you may want to change the networking back to DHCP and change both the root and userid passwords … or not. A knowledgeable Linux admin won’t need to know those anyway to gain access and setup everything.
Lastly, having a perfect backup of the entire virtual machine is extremely simple.
Great post. I wasn’t familiar with virt-manager prior to this. I used to use VirtualBox. Now I feel like I know a little more about KVM…
Igor: Glad to help with a tiny bit of illumination.
VirtualBox is useful for desktop virtualization – more like VMware Workstation or Player.
Virt-Manager is more like VMware vSphere to manage VMs either locally or on remote systems.
KVM is more like VMware ESX, ESXi, Xen, or QEMU for server-side virtualization.
KVM + virt-manager sorta bridges desktop/server virtualization, but I wouldn’t recommend someone looking for desktop virtualization go with KVM + virt-manager. The graphics capabilities are more like local VNC connections and simply are not ready for desktop graphics performance expectations … at this point (2/2012).
OTOH, if you want a desktop that you can access over the internet (though a VPN please!), then KVM + virt-manager + a VPN will let you securely access a remote desktop over VNC. Again, don’t expect fantastic desktop performance, but sometimes having no data at your current location is critical.