How to compile the Android Goldfish kernel (and test it in the Android emulator)

Android kernel diagram

Android architecture

The Android project provides a fancy, QEMU-based emulator for testing components in software without needing to flash anything to a physical device. The Android “goldfish” kernel is specifically designed to run on the Android emulator, which simulates an ARM-based mobile device.

http://source.android.com/ gives an overview of how to initialize your build environment and compile an Android kernel, but it leaves out some crucial, time-saving details.

Quickstart

Compile the Goldfish Kernel

Initialize your build enviromnent, or verify that it’s set up correctly. Make sure you have a complete and functional toolchain to build for arm devices. Once your build environment is set, perform the following steps (adjusting for your desired kernel and arm-eabi-gcc versions accordingly):

To test your new goldfish kernel in the emulator, manually specify the name of a pre-existing AVD or set the ANDROID_PRODUCT_OUT environment variable:

To target an AVD you’ve already set up, add -avd <name> as an argument to the emulator. I also like to include the -verbose flag to see what kind of messages the emulator is emitting.

My Android SDK installation is in /opt and I’m using a 64-bit system, so the location of my emulator is /opt/android-sdk/tools/emulator64-arm. The SDK also provides emulators for x86 and MIPS architectures.

Detailed instructions

Git clone the goldfish kernel repository

Cloning android kernel repositories can be a little confusing because everything seems to download correctly, but the result of the
git clone
operation is an empty directory with a very large .git folder. As AOSP architect Jean-Baptiste Queru explained,

That’s done on purpose. There’s no notion of master branch in the kernel (and there’s not even a notion of current development branch), but the various tools we use don’t work if there’s no master branch, so we add an empty master branch to make the tools happy.

So an empty directory after a git clone operation is normal. To set everything right, enter the empty directory you just cloned and check out a branch. To see what branches are available, type
git branch -a
. When you’ve decided which branch you want, check it out with

where origin/android-goldfish-2.6.29 is the name of the remote branch and -b specifies the branch’s local name.

Export environment variables

Parts of the make process take environment variables as arguments. The following setup will compile the kernel for ARM:

Make the kernel for your architecture

export ARCH=arm
specified the target architecture, and configurations for each architecture are found in goldfish/arch/<ARCH>/configs. In the past, goldfish_defconfig was a good option, but it doesn’t boot in the emulator at this point.

To speed the compilation process, add the -jX flag after the make command, where X is the number of cores you would like to use for parallel compilation. At the end of the build (following some cool debugging information), the last line of output will be the path to the compressed kernel, zImage.

Test your new goldfish kernel in the emulator

To run the emulator with the Goldfish kernel you just compiled, issue the following command:

This assumes that you’ve set the ANDROID_PRODUCT_OUT environment variable, or that your emulator knows which AVD to use. If the emulator prints the “emulator: ERROR: You did not specify a virtual device name, and the system directory could not be found” message, try again after you export ANDROID_PRODUCT_OUT (instructions here).

Before

Android emulator with default goldfish kernel

Android emulator with default goldfish kernel

After

compile android goldfish kernel

Emulator running the compiled goldfish kernel

arm-eabi-gcc: Command not found

If you type “make” and get the error “arm-eabi-gcc: Command not found,” you either don’t have the arm toolchain installed on your system, or the toolchain’s components (arm-eabi-gcc etc) aren’t in your PATH.

The Android kernel guide recommends:

Check to make sure that the the prebuilt arm-eabi-gcc toolchain isn’t already on your system before reinstalling it in another directory. If it’s already on your system, adding it to your path will fix the “arm-eabi-gcc: Command not found” error.

Incoming search terms:

3 thoughts on “How to compile the Android Goldfish kernel (and test it in the Android emulator)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">