28

I am able to pull down the latest android source code into a Ubuntu virtual machine 32-bit (Host: Windows 7 64-bit). The build completes without any errors.

Then I tried to follow these instructions, where it mentions that I should run the emulator on the root of my source code. However, when I tried that, I get an error stating that this command is not found.

So I went to the folder out/host/linux-x86/bin and I found out that there are couple files for emulator*:

  • emulator
  • emulator-arm
  • emulator_renderer
  • emulator-ui
  • emulator-x86

When I typed the emulator and emulator-x86 here, it also doesn't work. Here is the error I'm getting:

xxxx/out/host/linux-x86/bin$ ./emulator-x86
emulator: ERROR: You did not specify a virtual device name, and the system
directory could not be found.

If you are an Android SDK user, please use '@<name>' or '-avd <name>'
to start a given virtual device (see -help-avd for details).

Otherwise, follow the instructions in -help-disk-images to start the emulator

So when I run ./emulator-x86 -help-disk-images, I see the following:

If you are building from the Android build system, you should
have ANDROID_PRODUCT_OUT defined in your environment, and the
emulator shall be able to pick-up the right image files automatically.
See -help-build-images for more details.

I built this myself, so I would think that ANDROID_PRODUCT_OUT is set in my environment variables, but I don't see it. So I think that I should run some other shell script to get that set.

I looked at the img files, I saw couple at the location out/target/product/generic:

  • ramdisk.img
  • system.img
  • userdata.img

Could anyone shed some light on this and assist me on what I should do next? I am new to Android and I did some research on this but I couldn't find any similar issues.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
windfly2006
  • 1,703
  • 3
  • 25
  • 48

10 Answers10

29

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.

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
Yury
  • 20,618
  • 7
  • 58
  • 86
  • 3
    thanks. I followed your instruction, I got this: emulator: WARNING: could not load skin file 'sdk/emulator/skins/WVGA800/layout', using built-in one emulator: ERROR: Invalid or missing kernel image file: prebuilt/android-arm/kernel/kernel-qemu. there is no knernel folder under prebuilt/android-arm/ (I just have gdbserver folder) – windfly2006 Jan 26 '12 at 18:36
  • I just uses the "repo sync" to pull down all the source, any idea how I could get the kernel for prebuilt? – windfly2006 Jan 26 '12 at 18:38
  • I've checked all my sources. They all have this directory and this file. I think that your repo sync command have not finished properly. However just after the built you can check if everything is working just running command emulator. – Yury Jan 26 '12 at 18:47
  • 1
    If you set the ANDROID_PRODUCT_OUT variable on your development machine to /out/target/product/generic the emulator will know where to look for all the system images so that you do not need to specify them directly. An even better way is to run the script envsetup.sh that is located in the build folder of the android source and then run setpaths (supplied by that script). That will take care of setting up all paths and variables for your project allowing you to just type emulator on the command line after the build finishes. – BMB Jan 27 '12 at 09:37
  • Yes, I know about this. But for me it's easier to copy this script and run it everytime after the build. – Yury Jan 27 '12 at 10:29
  • actually I found out the kernel-qemu, however I found 2 and they are at different directories as you specified at your script: ./prebuilts/qemu-kernel/x86/kernel-qemu ./prebuilts/qemu-kernel/arm/kernel-qemu, not sure if this is due to that we have different version of Android source code and they changed the path. – windfly2006 Jan 27 '12 at 14:19
  • actually I sourced the envsetup.sh and then I ran setpaths, then I ran emulator, now, it could find the emulator now, however I still get error: You did not specify a virtual device name, and the system directory could not be found. With Yury's command, I am able to run the emulator. However emulator screen is dark after is it launched, I guess that it is a new problem. – windfly2006 Jan 27 '12 at 14:23
  • Yes, this is another problem. Something wrong with your build you should check errors that are shown during the build. – Yury Jan 27 '12 at 14:30
  • 7
    Thanks a lot, guys. Apparently I need to run the set_stuff_for_environment as well which is defined from the envsetup.sh. After I ran that, everything looks good now. The instruction page doesn't mention that at all. – windfly2006 Jan 27 '12 at 14:55
28

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
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • 1
    I did some steps unorderly but it worked. My `out` directory is at a different place. I `envsetup.sh`ed without `set_stuff_for_environment` and after successfully building I couldn't run emulator. But then `set_stuff_for_environment` without rebulding, it launched the emulator with latest build system. Thanks. – Fredrick Gauss Jun 10 '14 at 10:27
8

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 !!!!

nsndvd
  • 800
  • 8
  • 21
SAURABH_12
  • 2,262
  • 1
  • 19
  • 19
4

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.

redbandit
  • 2,132
  • 16
  • 12
3
#!/usr/bin/env bash

ANDROID_BUILD_OUT=/path/to/android/build/output/
ANDROID_SDK_LINUX=/path/to/android/sdk
ANDROID_BUILD=${ANDROID_BUILD_OUT}/android/target/product/generic

${ANDROID_SDK_LINUX}/tools/emulator \
    -sysdir ${ANDROID_BUILD} \
    -system ${ANDROID_BUILD}/system.img \
    -ramdisk ${ANDROID_BUILD}/ramdisk.img \
    -data ${ANDROID_BUILD}/userdata.img \
    -kernel ${ANDROID_SDK_LINUX}/system-images/android-18/armeabi-v7a/kernel-qemu \
    -skindir ${ANDROID_SDK_LINUX}/platforms/android-18/skins \
    -skin WVGA800 \
    -scale 0.7 \
    -memory 512 \
    -partition-size 1024
JP Ventura
  • 5,564
  • 6
  • 52
  • 69
3

On a mac, you can add the following lines into your ~/.bash_profile file. Change your disk image and src folders accordingly.

# start emulator
function startEmulator { hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android;
                         cd /Volumes/android/AndroidSrc;
                         source build/envsetup.sh;
                         lunch aosp_arm-eng;
                         /Volumes/android/AndroidSrc/out/host/darwin-x86/bin/emulator;  }

After that, create a new terminal and type:

startEmulator

Your emulator can be started. This works on mac.

1

Just type emulator on your shell and it will launch the emulator of the latest build as its path is set to the PATH variable of your shell.

keyser
  • 18,829
  • 16
  • 59
  • 101
Sai Kiran
  • 465
  • 9
  • 16
0
export MY_PWD=/work/ABC/Google_Android
export ANDROID_BUILD_TOP=/work/ABC/Google_Android
export PATH=$PATH:$MY_PWD/prebuilts/gcc/linux
export ANDROID_PRODUCT_OUT=$MY_PWD/out/target/product/generic

The above is my env setting. ANDROID_BUILD_TOP solved the

"emulator: ERROR: You did not specify a virtual device name, and the system
directory could not be found"

on my machine

pevik
  • 4,523
  • 3
  • 33
  • 44
harris
  • 1,473
  • 1
  • 17
  • 26
0

I performed this way

I modified the ./build/envsetup.sh file, I only changed the set_stuff_for_environment

function set_stuff_for_environment()
{
    setpaths
    set_sequence_number

    export ANDROID_BUILD_TOP=$(gettop)
    # With this environment variable new GCC can apply colors to warnings/errors
    export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
    export ASAN_OPTIONS=detect_leaks=0

    export ANDROID_PRODUCT_OUT=/var/www/android/out/target/product/generic_x86_64
    echo $ANDROID_PRODUCT_OUT
}

At the root of the project I created a file named start.sh

#!/usr/bin/env bash

ANDROID_BUILD_OUT=/var/www/android/out
ANDROID_SDK_LINUX=/opt/android-studio/sdk
ANDROID_BUILD=${ANDROID_BUILD_OUT}/target/product/generic_x86_64

sudo chmod -R 777 /dev/kvm

source build/envsetup.sh

set_stuff_for_environment

./prebuilts/android-emulator/linux-x86_64/emulator \
    -debug-init -logcat '*:v' -verbose \
    -sysdir ${ANDROID_BUILD} \
    -system ${ANDROID_BUILD}/system.img \
    -ramdisk ${ANDROID_BUILD}/ramdisk.img \
    -skindir ${ANDROID_SDK_LINUX}/platforms/android-28/skins \
    -skin WVGA800 \
    -partition-size 2000
    -scale 0.7 \
    -memory 2000 \
    -data ${ANDROID_BUILD}/userdata.img \
Jonatas Rodrigues
  • 57
  • 1
  • 2
  • 12
0

If you have the "android sdk" on your machine, then your "emulator" could be picked up from there instead of /out/.... dir. When you want to work with your "own" emulator, you can rename the "android sdk" directory. Then your "emulator" will be picked up.

Hope this helps you!

Regards Sammoh

Sammoh
  • 66
  • 3