Running emulator after building Android from source

After much puzzling and encountering many of the same problems, I've found a way to get everything working from a new environment.

Environment

First of all, make sure you set your environment with the changes to ~/.bashrc that Android recommends, including:

export USE_CCACHE=1
ccache -M 10G

Follow the steps for downloading the Android source, if you haven't already done so.

Then set up some functions for the environment:

$ . build/envsetup.sh

You now should actually execute one of those functions to get the paths set correctly (as Pingzhong Li pointed out, this is not mentioned in the Android build instructions!):

$ set_stuff_for_environment

First build

Start building! For instance:

$ lunch full-eng
$ make -j4

(Here, 4 threads is ideal for my machine. Change as you see fit.)

When the build finishes, simply launch the emulator:

$ emulator

Subsequent builds

I've found that to get the system.img to rebuild, you need to remove the following files/directories:

out/target/product/generic/obj/PACKAGING/
out/target/product/generic/system.img

Then simply repeat:

$ make -j4
$ emulator

How to run Emulator step by step guide. Running emulator in downloaded android AOSP source code is as below :-

  • Step 1 If you have finished your build and generated System image correctly in current running Terminal(Ubuntu), Then it is stragiht forward. Just type below command in your terminal:- emulator

  • Step 2 If you have generated system image earlier and you have started a fresh terminal(Ubuntu) then run the following command one by one :-

    1. source build/envsetup.sh
    2. lunch 1 here 1 is my lunch type, you can replace it with yours like(7, 8 etc) and in the last
    3. emulator

Thats it it will lunch your emulator correctly. Thanks Guys Happy Coding !!!!


I do not know for which product you do your build but to run emulator you can use the following command:

out/host/linux-x86/bin/emulator -sysdir out/target/product/generic/ -system out/target/product/generic/system.img -ramdisk out/target/product/generic/ramdisk.img -data out/target/product/generic/userdata.img -kernel prebuilt/android-arm/kernel/kernel-qemu -sdcard sdcard.img -skindir sdk/emulator/skins -skin WVGA800 -scale 0.7 -memory 512 -partition-size 1024

Just copy it into .sh file into the root of your Android source folder and run this file. Or you can just run it but you should chdir to your Android source folder root at first.

And do not forget to create an sdcard image in the root folder with command mksdcard.


Just for reference I had this similar problem and after trying different things I found the solution to be running lunch(after running envsetup.sh) and picking the target in this case aosp_arm-eng. You have to do this everytime you start a new shell because it sets up certain environment variables the emulator needs to run the avd.Provided you have built the target.