userimage

How To Speed-up your Android Emulator in Linux OS

Now a days Android ecosystem becomes the Largest Mobile Platform, many developers want to put their legs in this ecosystem. Android ecosystem gives its users a large variety of devices powered with different specs. This become headache for almost all Android developers in testing their application while developing and make sure that it will work proper in wide range of devices. So to do this, developers create different emulators with different specs and run their app. But are they happy with it?  I guess, No. Because the performance of Android Emulator is very slow, and if the application is doing some major works like server communication, animations, etc, then emulator sucks like anything.


Lets see how to speeding up your Android Emulator, so that it will run as fast as the real device.

This Post Is For
          Are you an Android Developer?
          Are you developing your applications in Linux platform?
          Is your emulator performance is very slow?

Those who has the answer Yes for all the above questions can get a solution from this post.

How To Achieve This?
          To achieve this you just need to configure your Emulator with some specific Android System Images and Hardware Drivers and let it take the advantage of Hardware Acceleration.

You can make your Emulator take the advantage of Hardware Acceleration in the following ways:

    By configuring it with Graphics Acceleration
    By configuring it with Virtual Machine Acceleration


Configuring Emulator With Graphics Acceleration:
          Graphics Accelerated Emulators will take the advantage of the system"s Graphics Processing Unit (GPU) to make the Emulator run faster and more responsive.

NOTE: This will only work if your PC has GPU. Otherwise instead of increasing the performance, it may decrease it.

Prerequisites:
          You need to install the following two things through Android SDK Manager:

    Android SDK Tools, Revision 17 or higher
    Android SDK Platform API 15, Revision 3 or higher (Android 4.0.3 or higher)

Are you running your emulator through command line? Type the following command to start an emulator:

                           emulator -avd <avd_name> -gpu on


NOTE: The <avd_name> should be the name of any AVD which has the Target more than Android 4.0.3.

 Are you running emulator through eclipse? Follow the below steps:

    Right click on your project folder, Go to Run As > Run Configurations...
    Click on Android Application > New_configuration.
    Click the Target tab.
    Select an AVD with Android 4.0.3 or higher.
    In the Additional Emulator Command Line Options field, enter: -gpu on
    Run your application using this Run Configuration.


          Instead of setting the Run Configuration each time you run your application, if you want to configure it once and use it for all time, then follow the below steps:

    Create a new AVD with Target as Android 4.0.3 or higher.
    In the Hardware selection, click New and select the option GPU emulation and set the value to yes.
    Click Create AVD. And you are done.


 Now you can use this AVD to run your application any number of time. Below is the sample screenshot of this:


                                         


Configuring Emulator With Virtual Machine Acceleration:
          Before doing anything you should first know whether your PC supports any of the following virtualization technologies or not.

    Intel Virtualization Technology (VT, VT-x, vmx) extensions.
    AMD Virtualization (AMD-V, SVM) extensions.


          To know whether your PC supports any of the above virtualization technologies, use the following command in the terminal:

    egrep -c "(vmx|svm)" /proc/cpuinfo

The output of the following command will be an integer value. If the value is:

    0 - Means your PC does not supports hardware virtualization. So you cannot use VM Acceleration for your emulator. You have to be satisfied with GPU Emulation explained above.
    1 or more - Yes, your PC supports hardware virtualization. So you can proceed with the below steps.


Prerequisites:
          You got confirmed that your PC supports hardware virtualization. so before proceeding further install the following two things through Android SDK Manager:

    Android SDK Tools, Revision 17 or higher
    Android x-86 based System Image
    Create an AVD with the above two things.


          Linux based OS supports hardware virtualization through a software package called Kernel-based Virtual Machine (KVM). So your next step is to install KVM on your PC.

Installing Kernel-based Virtual Machine (KVM):
          Open your Terminal and execute

    kvm-ok

You might get any of the following results:

    INFO: /dev/kvm exists
    KVM acceleration can be used

If everything Ok:
Congrats, your PC is ready to use hardware virualization. you just need to install KVM and use it.

    INFO: KVM is disabled by your BIOS
    KVM acceleration can NOT be used


                                



          You just Shut Down your PC and next time when you starts, just go to BIOS Setup  and enable the virtualization. After enabling it you can go forward to install KVM and use it. The following screenshot may help you a bit on enabling the virtualization in BIOS.


Now start installing the KVM package using the following command in terminal:

    sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils

After the installation successfully completes, add your user name to the following groups:

    sudo adduser `id -un` libvirtd
    sudo adduser `id -un` kvm


          You are almost done. Now you just need to restart your PC to make these changes be affected. But before going to any further steps lets verify whether your installation was successful or not.

Verifying KVM Installation:
          In the terminal, execute:

    virsh -c qemu:///system list

Are you getting an output like below? Then your installation was right.

    Id Name                 State
    ----------------------------------

You may also get an output something like this, then something is wrong. You need to fix this before you move on.

    libvir: Remote error : Permission denied
    error: failed to connect to the hypervisor


Almost Done:
          After installation of KVM succeed, you just need to make it used by Emulator. So lets do that.

 If you are running the emulator through command line, then execute the command as below:

        emulator -avd <avd_name> -qemu -m 512 -enable-kvm


If you are running the emulator using eclipse, then follow the below steps:


        Right click on your project folder, then go to RunAs > Run Configurations...
        On the Run Configurations, select your project configuration or create a new configuration.
        Click the Target tab.
        Select an AVD associated with any x-86 based system image.


In the Additional Emulator Command Line Options field, type:

            -qemu -m 512 -enable-kvm

References:
          Speeding-up Android Emulator in Intel PCs
          Installing KVM
          Using Android Emulator