How to Compile the Android Aosp Kernel and Test It with the Android Emulator

How to compile the Android AOSP kernel and test it with the Android Emulator?

Since August 2009 the kernel is no longer part of the standard repo manifest that you get when you follow the instructions to download the source code for the android open source project. The steps that are needed to successfully download, build and run a specific kernel on the emulator are as follows:

  • Get the Android kernel either by adding it to your repo manifest or manually by running:

    git clone https://android.googlesource.com/kernel/goldfish.git
  • Check out the correct branch for working with the emulator, i.e. goldfish:

    git checkout -t origin/android-goldfish-2.6.29 -b goldfish
  • Generate the emulator configuration (qemu emulator runs arm code, i.e. an arm config):

    make ARCH=arm goldfish_defconfig

    • if that doesn't work, try make ARCH=arm goldfish_armv7_defconfig
  • Now build the kernel using the cross compilation tools distributed with the open source project:

    make ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-
  • The kernel built this way should end up in the arch/arm/boot folder of your kernel tree (where you put the code from git clone)
  • To run the emulator with your kernel there are two alternatives, either copy it to the prebuilt kernel folder of the open source project to replace the standard kernel. The other option is to start the emulator with the kernel option set:

    emulator -kernel mydroid/kernel/common/arch/arm/boot/zImage

Note that I have used the default paths in the above description, you need to change them to what applies to your setup. It has been a some time since last time I tested this but I think it should work.

Some extra information:
In the standard Android open source distribution the kernel is distributed as a pre-built binary in the mydroid/prebuilt/android-arm/kernel folder and the source code is not included. The kernel source was removed from the default manifest for two reasons as I take it. One is that it takes a lot of bandwith and diskspace for a platform component that most people will not work with much. The other reason is that since the kernel is built with the kernel build system and not as part of the aosp build system it makes sense to keep it separated. The common branch for the kernel is the one used by the emulator. There are also branches for experimental, msm (Qualcomm platforms) and Omap (TI platform) and maybe some more. If you want to use the Android kernel with hardware these may be more interesting to you.

How can I build a kernel for the Android Emulator?

You can git clone common kernel source from android.git.kernel.org (kernel/common.git) .
After clone, checkout remote branch origin/android-goldfish-2.6.29.
Then, you can build a kernel image for the emulator. Use arch/arm/configs/goldfish_defconfig for configuration.

Running emulator after building Android from source

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 compiled AOSP, do I need AVD to run 'emulator'?

Bellow are the commands that successfully ran in my machine. Hope it may help you solving the issue.

$ export USE_CCACHE=1
$ ccache -M 10G
$ . build/envsetup.sh
$ set_stuff_for_environment
$ lunch full-eng
$ make -j16
$ emulator

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 -j16
$ emulator


Related Topics



Leave a reply



Submit