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

Fully automated Android 8.1 guest on Ubuntu 17.10 host

# Download the source. Takes several minutes.
curl https://storage.googleapis.com/git-repo-downloads/repo >repo
chmod a+x repo
./repo init -b android-8.1.0_r1 --depth 1 -u https://android.googlesource.com/platform/manifest
./repo sync -c  -j $(($(nproc) - 2)) --no-tags --no-clone-bundle

# Do the actual build. Takes minutes / hours.
. build/envsetup.sh
lunch aosp_x86_64-eng
USE_CCACHE=1 CCACHE_DIR=ccache make -j $(($(nproc) - 2))

# Run the emulator.
emulator -show-kernel

The out/ build directory takes up about 90Gb, and the rest of the tree about 40Gb, excluding CCACHE.

About 1-2 minutes after starting the emulator, the home screen shows:

and if you press enter on the host terminal Android was launched from, you get a shell on to the Android system on your host terminal:

enter image description here

Notes:

  • ./repo init -b MUST point to a tag. master branch is always broken, and so were -release branches.

    The list of tags can be found at: https://android.googlesource.com/platform/manifest or by cloning that repo.

    There are likely two reasons why branches are always broken:

    • Android is developed behind closed doors and code dropped. Therefore Google and OEM devs already have a ton of paches on top of the public "master", and have already fixed the problem.

      For the same reason it is likely useless to try and report any build errors on master: they have already been reported and fixed. Also I dare you to even find the right official place to report build failures.

    • repo sync on a branch simply pulls whatever latest version of all 650 git repos makes up AOSP for the given branch, without syncing them like submodules. Therefore nothing guarantees that they are compatible. Tags however fetch a specific tag of all repos.

  • --depth 1 and sync -c --no-tags --no-clone-bundle were an attempt to make the painfully slow clone faster. Not sure how successful it was. See also: AOSP repo sync takes too long

  • We use lunch aosp_x86_64-eng instead of ARM because it runs much faster due to host x86 virtualization extensions including KVM.

    To build an ARM version instead, just use lunch aosp_arm-eng instead.

    Furthermore, the ARM image was buggy, possibly due to the slowness? When the GUI starts (if you are lucky), it shows "System UI isn't responding". See also: Process system isn't responding in android emulator

    All the "usual high level things" that you do in Java / C++ native APIs should just work equally on x86 and ARM in theory, so it should not matter unless you are hardcore enough to manually touch some assembly.

  • -show-kernel links the terminal to a serial, i.e. you see boot messages, and get a shell at the end, which is very useful to debug things.

  • type emulator shows that it is just an alias to the emulator without any arguments. Run custom ROM on Android Emulator asks how to pass some arguments to explicitly select your ROM.

    The emulator -help targets are surprisingly insightful:

    emulator -help
    emulator -help-build-images
    emulator -help-disk-images
    

    You can determine the exact QEMU command line arguments given with:

    emulator -verbose | grep 'emulator: argv'
    

    as mentioned at: How to show which options are passed to QEMU when launching the android emulator?

    This shows some custom options e.g. -android-hw, so they must have forked QEMU: QEMU vs Android emulator: command line options The source moves location every 5 minutes apparently: Modifying Android emulator source code


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.


Just to correct a few things from BMB's post (which was very useful to me, it saved my project) :

  • git clone git://android.git.kernel.org/kernel/common.git (the kernel missed) ;
  • git checkout -t origin/android-goldfish-2.6.29 -b goldfish (the same) ;
  • make ARCH=arm goldfish_defconfig (idem) ;
  • make ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi- (idem)
  • emulator -avd my_avd -kernel mydroid/kernel/common/arch/arm/boot/zImage (here I added an avd in the command, it didn't work without for me).