Run Android Emulator with a Custom Kernel

Gabrio Tognozzi
2 min readJul 12, 2019

This is the story of how I got my AVD Device work with a custom kernel.

I will not describe here a global solution, of running a any Android Version on top of any Linux Kernel version, since even running the same steps with different <Adroid Version, Kernel Version> pairs will result in not working AVDevices. You will either get a black screen AVD or a SegFault error when booting. We all know that properly building a working Linux Kernel, specially in this corner cases, involves a little of magic. That said, I will only consider a specific <Adroid Version, Kernel Version> pair.

TLDR; I was only able to get the AVD work with Android Version 7,on top of the Linux Kernel branch android-goldfish-3.10-n-dev.

Show me the trick!

I will directly dive into the subject. Here we go, the needed steps follow:

  1. Cloning needed repositories: We need to download the Goldfish Linux Kernel ( specific version for emulated devices ), and the gcc compiler for Android:
git clone https://android.googlesource.com/kernel/goldfish/ -b android-goldfish-3.10-n-dev
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9

2. Setting up needed variables:
We now need to add the android’s gcc path to PATH environment variable.

export PATH=$PATH:$PWD/x86_64-linux-android-4.9/bin

3. Building the 3.10 dev Linux Kernel:
With the first line we let KBuild system create a default .config file, with the second line we actually start the building of the kernel

make ARCH=x86_64 x86_64_ranchu_defconfig
make -j16 ARCH=x86_64 CROSS_COMPILE=x86_64-linux-android-

4. Creating a proper AVD device:
I’ve used the x86_64 Linux Kernel ( intel architecture, 64bit registers addresses ), hence you will need to :

open android studio -> AVD Manager -> Create Virtual Device -> Pixel 2 -> Nougat API 24 ABI x86_64 Android 7.0 (Google APIs, because it permits to get root) -> Choose an AVD_NAME -> finish

5. Launch the AVD device:
In order to get your AVD run with our configuration we will go under Android/Sdk/emulator folder and launch the emulator as follows:

./emulator -verbose @AVD_NAME -kernel /path/to/repo/goldfish/arch/x86/boot/bzImage -show-kernel -qemu — enable-kvm

Conclusion

It is not a general solution, but this configuration let me go ahead with my work, and will be the foundation for dumping AVDs RAM images and inspect them with Volatility. I also think this is an interesting topic to talk about given that there is no directly reproducible tutorial about that subject on the internet as far as I know. If you can find more general solutions, you want to share something with us or you can simply teach me how to write inline code on Medium on Linux, commet below! See you soon ;)

--

--