Nuxeo/Blogs

Product & Development / All about the Nuxeo Platform, from strategy to feature highlights to dev tricks

How to speed up the Android Emulator by up to 400%

with 64 comments

I have been working on an Android SDK for the Nuxeo Content Management Platform in the past months (feel free to check out the early side of our developments in my previous blog post on that topic) and have worked more on it lately as we are on our way to making the first official release of this SDK. I wanted to share some feedback on a practical side of Android development related to test and emulation. After a few days of development, I found out that Android Emulator was a pain because it is far too slow.

First of all it is slow to boot, but even if you use Snapshot to speed up the start, the execution is also very slow, especially when using the debug mode.

Tweaking Qemu parameters did not help significantly, the emulator remains too slow, that’s probably why most Android developers seem to use a real device attached via USB.

Anyway, I wanted to find a solution, also because I want to be able to do a demonstration of an application via a beamer and don’t want to do the demonstration of a slow application.

Emulation vs. Simulation

I am not an Apple developer, but it looks like the iPhone simulator does not have the same issue as the Android Emulator.

One of the reasons for this is that it does not run a “real emulator”, because the CPU instruction used by the iPhone simulator is the same as the one used by the host (x86).

On the contrary, Android Emulator emulates a real ARM processor on top of an x86 processor. This is a lot of overhead.

At least for quick test and demonstration purposes, I don’t need to emulate an ARM processor, I just need to be able to run Android and my application.

Android_x86

Fortunately there is an open source project porting Android OS to x86 CPUs: http://www.android-x86.org/.

The project provides several images, and even if they logically cannot keep up to date with all the Android SDK releases, they provide an image for Android 2.3 that is an interesting target.

Setting up Android_x86 with VirtualBox

The first step is to download an ISO image of Android_x86.
I used android-x86-2.3-RC1-eeepc.iso that can be downloaded from http://www.android-x86.org/download.

The next step is to create a Virtual Machine to be able to run this image.

I used Virtual Box for that, but from what I read, QEmu can be used too.

So in Virtual Box, you have to create a new machine:

  • target OS: choose Linux

  • target OS version: others

  • I used 1GB for RAM and 1 CPU (and left the other options at default)

  • add a new hard drive: VDI drive, dynamically sized, 512 Mio

  • on the storage add a CDROM pointing to the iso image you just downloaded.

On the boot menu select install to hard disk.

Install

During the set up, you will have to:

  • create a new partition

  • format it to ext3

  • select this partition for Android installation

Once set up is completed:

  • shutdown the VM

  • remove the CDROM device pointing to ISO (in VirtualBox configuration dialog).

Boot the VM; you should now have a running Android x86 image.

But since it is by default configured for an Eee PC, this is not ideal to test applications targeting a smart phone.

We will now change the configuration to match a phone screen.

Shutdown the VM and VirtualBox.

The first step is to define custom resolution modes. I defined 3 modes:

VBoxManage setextradata "Android2.3" "CustomVideoMode1" "320x480x16"
VBoxManage setextradata "Android2.3" "CustomVideoMode2" "640x960x16"
VBoxManage setextradata "Android2.3" "CustomVideoMode3" "480x720x16"

where “Android2.3″ is the name of the VM in VirtualBox.

Now that we have declared new modes, we need to use them. For that we need to change the kernel parameters.

In order to do so, you need to restart the VM and on the boot menu choose the Debug mode so that Android boots in command line.

Start the VM.

Once started, we will remount the boot partition in read/write so that we can change the Grub configuration.

mount -o remount,rw /mnt

Then you can edit the menu.lst file.

vi /mnt/grub/menu.lst

Then duplicate the first menu entry (3 lines) and then edit the kernel parameters
(the first “title” entry and two following lines.)

Default parameters are:

quiet root=/dev/ram0 androidboot_hardware=eeepc acpi_sleep=s3_bios,s3_mode DPI=240 SRC=/android-2.3-RC1

parameters I used:

quiet root=/dev/ram0 androidboot_hardware=generic_x86 acpi_sleep=s3_bios,s3_mode DPI=240 UVESA_MODE=480x720 SRC=/android-2.3-RC1

Set what you want for the title.

If you want to be able to select the resolution at boot time, you can also use:

quiet root=/dev/ram0 androidboot_hardware=generic_x86 acpi_sleep=s3_bios,s3_mode vga=ask SRC=/android-2.3-RC1

Save your menu.lst (:wq) and reboot the VM using the VirtualBox menu.

You should now be able to start an Android VM that looks like a phone.

Quick tips for using the VM:

  • if you don’t see the mouse on the Android screen, use the VirtualBox menu to disable mouse integration (Host key + I)
  • the “Windows Key” corresponds to the Android Home button
  • Esc corresponds to the Android back button
  • F2 corresponds to the Android menu button
  • F3 corresponds to the Android search button
  • Alt+F1 => switch to console mode
  • Alt+F7 => switch to GUI mode

Connecting the Android x86 VM to AVD and Eclipse

The goal is to be able to use the new VM from Eclipse using AVD to test and debug your app.
For this to work, you need the network config to allow connection between the host (where you run Eclipse) and the VM.
For that, inside VirtualBox you have several options in the Network settings:
– use Bridge mode: it will work but may require the presence of a DHCP server on the host
– host only network: uses an internal network between the VM and the host; this is the simplest solution.

VB_HostConfig

Once you have setup the network, restart the VM and go to command line mode (Alt+F1), and type:

netcfg

It will output the current IP of the VM.
Typically, you will have something like 192.168.56.101 for the VM and the host will be bound to 192.168.56.1.

On the host, open a command line and cd into the android platform-tools directory:

./adb connect 192.168.56.101

This will register your VM as a new device:
Adb-connect

You can now, directly from Eclipse, run and debug your application in the Android x86 VM.

As you will see, the speed improvement is very very significant:

  • VM startup takes about 2s rather than 30s
  • Application run and debug is very fast (no lag like when using the ARM emulator).

Adding a SD card

You can use the documentation provided by http://www.android-x86.org/documents/sdcardhowto.

Using a file to fake a SD card

From the Android command line:

dd if=/dev/zero of=/data/sdcard.img bs=1024 count=65536 (64MB image)
losetup /dev/block/loop7 /data/sdcard.img
newfs_msdos /dev/block/loop7

Then restart the VM in debug mode, re-mount the partition in RW mode and edit menu.lst to add one parameter to the kernel:

SDCARD=/data/sdcard.img

Using a separated partition

This option requires a little more work.

You first have to create a new hard drive in VirtualBox and attach it to the VM.

Now start your VM in debug mode.
Use fdisk to create a new partition.
Once the partition is created, you have to format it:

newfs_msdos /dev/sdb1

Now edit the menu.lst file to add the parameter:

SDCARD=sdb1

Feedback on using Android_x86 vm as a testing environment

Usage

So far, the x86 VM works as expected and outside of the speed I did not notice any real difference.
All the Android projects have deployed correctly. Android API Sample project runs ok outside of the NDK.

The only visible point is that the Gallery app (Cooliris) is broken, I tested with a nightly build image, and it works a little bit better, but it’s still broken.

Another issue is that in about 10% of the cases, the VM does not boot and you have to reset the VM. Since boot is very fast this is not a pain, so I did not investigate further.

Speed

The difference of speed is really visible and significant.

Here are some figures to give an idea of the speed improvements :




















































































QEmu + Android ARM (2.3.3) VirtualBox + Android X86 (2.3.5) Nexus One phone (2.3.6)

Result Speed ratio Result Speed ratio Result
Boot
(in seconds, lower is better)
55 109% 5 1200% 60
Android API GUI test
(in seconds, lower is better)
35 71% 20 125% 25
Android API OpenGL Sprite test
(in ms, lower is better)
65 28% 8 225% 18
BrowserMark
(index, higher is better)
5500 15% 165000 446% 37000
Nuxeo SDK test
(in seconds, lower is better)
26 58% 14 107% 15









43%
226%

Android ARM emulator is about half the speed of a Nexus One, whereas the Android x86 VM is about twice as fast.

October 10th, 2011 at 10:21 pm

Posted in Product & Development

Tagged with

  • http://miamidesign.co Jose Tavares

    Thanks for sharing I’m looking forward to trying this out tonight!

  • Eric

    Thanks for the information! Adding custom video modes to VirtualBox was what I was missing.
    Have you looked into getting higher color depth working?

  • blackjid

    I cannot get the screen size as I want.
    I’ve followed all the steps, but always get a square screen
    any help?
    thanks

  • ELW

    can you upload a disk images with some 2.3.3 and a 3.0 installed?

  • http://goloersan.wordpress.com/ Mosh

    thanks, it works for me :D (1000+ thump Article), but i want to mention for windows user, that for sample
    adb connect 192.168.54.101:5555
    that they must write the port (5555) to allow adb to connect

  • quess

    The QEMU is so poor because you should be using kvm instead, and have a computer that support virtualization (Intel VT or AMD-V). Then you really will see some improvement!

  • http://adnansiddiqi.com Adnan

    why on a VM? why not on host system?

  • steveoooo

    Android is the VM. If you install on your host then you will have Android phone pop up when you turn on your pc. Might be fun good office prank.

  • ohaleck

    Works like a dream. So fast! Thanks (Ubuntu 10.04/Virtual Box)

  • anroid

    lowering the DPI giving more resolution: DPI=160

  • http://profile.typepad.com/6p0120a6447251970c Thierry Delprat

    I read somewhere that only 16bit modes were available, but I did not test with higher color depth.

  • http://profile.typepad.com/6p0120a6447251970c Thierry Delprat

    Be sure to add the custom modes in VirtualBox and also check that your kernel parameters are ok.
    You can edit the kernel parameters from the bootloader menu and check that the changes in menu.lst were taken into account.
    During my tests, I had once the issue that the changes in the menu.lst were not saved for real : may be a sync issue when restarting too quickly after having edited the file.

  • http://profile.typepad.com/6p0120a6447251970c Thierry Delprat

    The recipe was indeed only tested from Ubuntu and MacOS.
    Thanks for your feedback !

  • ohaleck

    Doesn’t work like a dream any more. When I try to run my app, it crashes and the logcat goes:
    D/AndroidRuntime( 2016): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    D/AndroidRuntime( 2016): CheckJNI is ON
    D/AndroidRuntime( 2016): — registering native functions —
    I/ethernet( 2016): Loading ethernet jni class
    D/dalvikvm( 1522): GC_EXPLICIT freed 165 objects / 11808 bytes in 18ms
    D/PackageParser( 1212): Scanning package: /data/app/vmdl52064.tmp
    W/PackageParser( 1212): No actions in intent filter at /data/app/vmdl52064.tmp Binary XML file line #22
    I/PackageParser( 1212): com.——-: compat added android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_PHONE_STATE
    D/PackageManager( 1212): Scanning package com.antennasoftware.volt.storefront.android.ui
    I/PackageManager( 1212): /data/app/com.———1.apk changed; unpacking
    E/PackageManager( 1212): Package com.——- has mismatched uid: 10048 on disk, 10052 in settings
    W/PackageManager( 1212): Native ABI mismatch from package file
    W/PackageManager( 1212): Package couldn't be installed in /data/app/com.———1.apk
    D/dalvikvm( 1212): GC_EXPLICIT freed 5336 objects / 311640 bytes in 10ms

  • Goran

    Really interesting. Working on a toshiba satellite using the spartan release with Bridge networking.
    Cannot debug an application that uses the maps library from google:
    Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY
    Any clue?
    Thanks

  • http://blogactivity.wordpress.com Santiago Lezica

    I have tried Android x86, mostly out of curiosity, but I never thought of using it for development. I mean, isn’t being an emulator the whole point of the emulator?
    I like the fact that the emulator creates an ARM environment, and I know it will be slow. I pay that price for having as precise a reproduction as possible of the behavior in an actual device.

  • http://bootdev.blogspot.com Edder

    Works great thanks, you made my day :)

  • Albert Lai

    IPhone simulator suffers with another issue. In fact, the simulator is running too fast, many problems cannot be discovered until the app goes into a real iPhone. So, we actually need something run as fast as the real device……

  • http://profile.typepad.com/irasescher Fernando Irías Escher
  • http://profile.typepad.com/irasescher Fernando Irías Escher

    Nice hack! But useless for me since I’m developing an app that uses the Google Maps API, and android_x86 does not have support for it: http://code.google.com/p/android-x86/issues/detail?id=47
    Great post though!

  • HG

    How to rotate the VM or virtual box?

  • dror

    Nice…
    One suggestion about connecting to ADB:
    Better use “NAT” network mode (default), which easily allows the device to connect to the internet.
    You then need to device (in VirtualBox Settings/Network) “port forwarding” from host:5555 to device 5555, and then “adb connect 127.0.0.1″ will connect the host to the device.

    • Guest

       how to do this ?

      • Anonymous

        * Power off the VM

        * Go to Settings > Network > Adapter 1

        * Tick “Enable Network Adapter”

        * “Attached to” => “NAT”

        * “Advanced > Adapter type” => “PCnet-FAST III …”

        * Click port forwarding

        * Set Name to whatever you want, Protocol to TCP, both host and guest ports to 5555

        Now you will be able to do this:

        > adb connect localhost #(or 127.0.0.1)

        and

        > adb shell

  • Andrei

    Worked like a charm, and it is indeed super fast.
    I cannot use it though, since animations in jQuery mobile cause the Android browser to crash. You pretty much cannot to any page transitions without a browser crash.
    Anyone solved this problem?

    • Tib

      It’s also my problem.. Do you have any news on this issue?

  • Carles Frias

    How can I save the menu.lst? I don’t understand your explanation…

  • konstantin

    where do you define custom resolution modes?

  • http://4activeweb.com mostafa

    thanks but i can’t connect to the device
    i got this message
    unable to connect to 192.168.56.100:5555
    can you help please

  • http://4activeweb.com mostafa

    i got this ip from the virtual box preference because inside the android if i open
    the command line and type netcfg it give me this
    lo up 127.0.0.1 255.0.0.0 0x00000049

  • Chas

    Can you give me an example where you saw this? I do a lot of testing using the simulator and it would benefit me to know what to look for.
    thanks

  • Manolo Garcia

    Only press ESC and type :wq

  • Sharique Abdullah

    I can not setup the network correct, somehow android VM does not detect the VBox network interface.
    Does it sound like I am missing something ?

  • Sharique Abdullah

    Apparently mostafa has the same issue.

  • Sharique Abdullah

    there is a VBoxManage program in your VirtualBox directory. Goto the command prompt, goto vbox’s installation folder, and run the three commands mentioned.

  • http://4activeweb.com mostafa

    unable to connect to 192.168.56.101:5555
    I use ubuntu 11.04
    any Idea

  • http://profile.typepad.com/tiry Thierry Delprat

    Please check the network configuration of your VM.
    Use private network or if you want to keep NAT solution, try the solution proposed by dror.

  • Jason Castellano

    I’m having a problem creating the file-based SD Card. The line below is throwing a syntax error when trying to evaluate the text in parens:
    dd if=/dev/zero of=/data/sdcard.img bs=1024 count=65536 (64MB image)
    Is this command supposed to be entered exactly as shown? It seems like an SDCard directory is created, but the size is off and nothing can be copied to it.
    Has anyone gotten this to work?

  • http://profile.typepad.com/ameenahmed Ameen Ahmed

    Thank you sooooooooooooooooooooooooo much!!! I am developing a game and it was quite annoying using the Emulator.. Thank u Thank u Thank u… 8-D

  • http://4activeweb.com mostafa

    i did the nat solution with port forwarding but i still miss something
    in the command line i see connected to 127.0.0.1:5555
    but i don’t see the device neither the eclipse Android Virtual Device Manger nor the create new AVD device
    see this pics
    http://4activeweb.com/Screenshot1.png
    http://4activeweb.com/Screenshot2.png

  • balh

    you use VI. ZZ shortcut saves.

  • Eduardo

    Press ESC and then type ZZ

  • Eduardo

    Does this only works for the version you used? Android 2.3 RC1? I followed your instructions but I used android 2.2.2 EEEPC. Everything went well, except that I can’t ping the host from the VM with android.

  • Lenciel

    Great article. Have you ever tried to use the android-x86 in VirtualBox with proxy set?

  • Flacohuesos23

    Gracias ¡¡¡ si me funciono……..

  • http://eazyigz.wordpress.com Igor Ganapolsky

    Does this work with Ice Cream Sandwich?  I had trouble with that build.

  • Free

    I think it’s cool you’ve explored this.  Really cool.  Still, I’m measuring a 1-second operation on a Acer A100 tablet that takes over 40 seconds on the emulator.  If I apply your technique it goes down to maybe 10 seconds w/r/t the actual device.  10-to-1 is still unacceptable for me to be an efficient developer on the Android platform.  The iOS simulator is nearly 1:1.  It’s unfortunate.

  • Creiss

    This totally rocks.   I saw a speed increase of 7x to 10x.

  • Victor Tsoukanov

    It seems to me they do not support ethernet for many android releases. At least I have started from the android ICS and could not connect via adb, then I was trying to use 
    Android-x86-2.3-RC1 I have connected successfully. See relaease notes, they support ethernet for this relaase only, hope they will fix this issue with later android relases. 

  • Imbibinebe

    Just perfect. Thank you so much ! 

  • ct

    Perfect. I’ll try it.
    But I wonder if I can integrate it with eclipse such as I compile it and run in emulator easily.
    Any suggestions?

  • Free

    After investigating further and thinking about it more, this isn’t a reasonable solution for professional Android developers.  VBox is a deep Android fork and it’s unclear if it’s consistently maintained, and how far it’s devaited from official releases.  See my blog post at http://tenacious33.wordpress.com/2012/04/05/improving-android-emulator-performance/

  • Yamato Shingen

    Everything works like a charm except:-
    when I type $adb devices on my MAC’s terminal…only a blank line is displayed. 

    netcfg on VirtualBox setting, displays IP address and port correctly.

    However when I try adb connect IP:port..
    unable to connect to IP:port

    when I type adb connect localhost:port…
    connected to 127.0.0.1:port.

    but no adb commands work.

    tried adb kill-server
    and adb start-server

    daemon is listening on 5037 port. etc.

    using:-
    android x86 2.2 generic.iso

    MAC laptop

    bridged connection (works inside the android emulator as well)

    Also tried checking the settings> applications> development> usb debugging.

    But no adb device is listed (I am supposing it should list some kind of an emulator)….

    Please Help. 

    thanks,

  • Eduardoanies

     Works great, but I have a problem, sound is not working, I am using Sound Blaster 16 as the audio controller
    any idea?

  • http://www.facebook.com/vip17 Lưu Vĩnh Phúc

    I’m using VMware. How can I create custom resolution modes like these virtualbox commands?
    VBoxManage setextradata “Android2.3″ “CustomVideoMode1″ “320x480x16″
    VBoxManage setextradata “Android2.3″ “CustomVideoMode2″ “640x960x16″
    VBoxManage setextradata “Android2.3″ “CustomVideoMode3″ “480x720x16″

    • Luiz Gustavo Sampaio

      Better to start using VirtualBox

  • Dfellany

    Well i too had problems with the emulator being so slow i started it at 512 ram size went too 600 faster but slower.. went too 700 and 800 so fast it wouldnt come on…   went too 400 then 300 ram size performance wise looks like i started up my phone …. But i finally but ram size too 250 and Bam the things runs like my phone at hand i would try 250 on ram size

  • neztime

    Android 4.x not work with NAT or Host-only or Bridge

  • ÜnLoCo MaX

    still cannot save menu.lst

    host: win7/win8
    what’s the (:wq) there man ? i even googled that with no good results !!
    help me save please ? :)) 

    • Enrique Yañez

      to enter :wq, you have to click “Esc” to change to command line and then introduce :wq and press ENTER, that will save the file

  • Luiz Gustavo Sampaio

    :wq is the command in VI to exit and save the file.
    http://www.cs.colostate.edu/helpdocs/vi.html

  • Ronald

    Worked like a charm for me! I downloaded the 4.2-RC2 version and got it working in vbox on my Mac OSX 10.8.4 (Mountain Lion) without any problems.

    The JSON feed I tried to pull in in my app was taking forever in the standard Android emulator and now it takes about 1 second (270Kb online file).

    Very cewl article – THNX!

  • The Nuxeo Lover

    It’s better to simply use a real device, it’s much more performent, even with a further 400% speed.