Skip to content

Instantly share code, notes, and snippets.

@yan12125
Created April 14, 2017 17:32
Show Gist options
  • Save yan12125/78a9004acb1bed5faf2ffd442163e2ef to your computer and use it in GitHub Desktop.
Save yan12125/78a9004acb1bed5faf2ffd442163e2ef to your computer and use it in GitHub Desktop.
Build a custom kernel for Android emulator
git clone https://android.googlesource.com/kernel/goldfish/ -b android-goldfish-3.18
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9
cd goldfish
export CROSS_COMPILE=x86_64-linux-android-
export ARCH=x86_64
export PATH=$PATH:/path/to/x86_64-linux-android-4.9/bin
make x86_64_ranchu_defconfig
make menuconfig  # enable overlayfs and namespaces support here
make -j8

Then create an Android virtual device via android avd. Here I use x86-64 image for API 24 (7.0 Nougat). Don't forget to use a larger internal memory size when creating the virtual device. (The default is 200MB and I use 2000MB) After that run the emulator with:

emulator -avd (avd_name) -kernel /path/to/goldfish/arch/x86/boot/bzImage -show-kernel
@ppoffice
Copy link

@harishvk27 Thanks. I built emulator images from the Android source master branch and booted with my custom kernel, and it worked out. It seems that they have fixed it in thread.cc.

I also used 3.10 kernel branch with the same configurations @yan12125 posted and booted it with Android N SDK 24.

@harishvk27
Copy link

One thing i am currently on is.. how to add network support..

Normal kernel which is part of ASOP when loaded on emulator.. internet is working fine.

But with custom kernel i don't see it working.

Is there a place in ASOP where i get to know the .config file ( menuconfig ) with which ASOP kernels are built?.

-thanks
harish

Copy link

ghost commented Nov 27, 2017

Hello,

@harishvk27: With the AOSP if you did not specify a TARGET_PREBUILT_KERNEL the common kernel is used by default.
You can export TARGET_PREBUILT_KERNEL=/your/default/goldfish/arch/$ARCH/boot/zImage

  • In BoardConfig.mk
    • Remove TARGET_NO_KERNEL if set
  • In your device Makefile, have something like
    ifeq ($(TARGET_PREBUILT_KERNEL),)
    LOCAL_KERNEL:= device/ti/panda/kernel
    else
    LOCAL_KERNEL:=$(TARGET_PREBUILT_KERNEL)
    endif
    PRODUCT_COPY_FILES := $(LOCAL_KERNEL):kernel

If that doesn't work you can replace the x86_64_defconfig config in the kernel folder ($AOSP/kernel/) with your own config and recompile the Kernel, you need also to remove the common Kernel that is used by default.

Best regards,

@llllIIIllll
Copy link

hello,
I run "adb devices",but it show "emulator 5554 offline"
emulator is use of AOSP
make aosp_arm64-eng
kernel 3.10 arm64
how to solve the problem?

@palratnesh05
Copy link

Hi I did not find #enable overlayfs and namespaces support here in menu

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make menuconfig
HOSTCC scripts/kconfig/mconf.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf Kconfig

*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

make completed successfully (02:23 (mm:ss))

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make -j4
make: x86_64-linux-android-gcc: Command not found
scripts/kconfig/conf --silentoldconfig Kconfig
make: x86_64-linux-android-gcc: Command not found
SYSHDR arch/x86/syscalls/../include/generated/asm/unistd_32_ia32.h
SYSTBL arch/x86/syscalls/../include/generated/asm/syscalls_32.h
HOSTCC scripts/basic/bin2c
CHK include/config/kernel.release
UPD include/config/kernel.release
SYSHDR arch/x86/syscalls/../include/generated/asm/unistd_64_x32.h
SYSTBL arch/x86/syscalls/../include/generated/asm/syscalls_64.h
WRAP arch/x86/include/generated/asm/clkdev.h
WRAP arch/x86/include/generated/asm/cputime.h
WRAP arch/x86/include/generated/asm/dma-contiguous.h
WRAP arch/x86/include/generated/asm/early_ioremap.h
WRAP arch/x86/include/generated/asm/mcs_spinlock.h
WRAP arch/x86/include/generated/asm/scatterlist.h
SYSHDR arch/x86/syscalls/../include/generated/uapi/asm/unistd_32.h
CHK include/generated/uapi/linux/version.h
UPD include/generated/uapi/linux/version.h
SYSHDR arch/x86/syscalls/../include/generated/uapi/asm/unistd_64.h
CHK include/generated/utsrelease.h
HOSTCC scripts/genksyms/genksyms.o
UPD include/generated/utsrelease.h
SHIPPED scripts/genksyms/parse.tab.c
SYSHDR arch/x86/syscalls/../include/generated/uapi/asm/unistd_x32.h
CC scripts/mod/empty.o
/bin/sh: 1: x86_64-linux-android-gcc: not found
scripts/Makefile.build:257: recipe for target 'scripts/mod/empty.o' failed
make[2]: *** [scripts/mod/empty.o] Error 127
scripts/Makefile.build:402: recipe for target 'scripts/mod' failed
make[1]: *** [scripts/mod] Error 2
make[1]: *** Waiting for unfinished jobs....
SHIPPED scripts/genksyms/lex.lex.c
SHIPPED scripts/genksyms/keywords.hash.c
SHIPPED scripts/genksyms/parse.tab.h
HOSTCC scripts/genksyms/parse.tab.o
kernel/Makefile:145: *** No X.509 certificates found ***
HOSTCC scripts/genksyms/lex.lex.o
HOSTLD scripts/genksyms/genksyms
Makefile:556: recipe for target 'scripts' failed
make: *** [scripts] Error 2
make: *** Waiting for unfinished jobs....

make failed to build some targets (2 seconds)

please help me how to resolve this problem?

@PGoski
Copy link

PGoski commented Jan 19, 2020

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make -j4
make: x86_64-linux-android-gcc: Command not found

checkout the correct branch in x86_64-linux-android-4.9 repository:

  1. cd x86_64-linux-android-4.9
  2. git checkout pie-b4s4-release

now kernel should start compiling

@johnnyboy18
Copy link

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make -j4
make: x86_64-linux-android-gcc: Command not found

checkout the correct branch in x86_64-linux-android-4.9 repository:

1. cd x86_64-linux-android-4.9

2. git checkout pie-b4s4-release

now kernel should start compiling

Thank You!

@ysun
Copy link

ysun commented May 6, 2020

Hi, as Android kernel is moving on to v5.4, do you have guide to boot Android emulator with customized kernel v5.4 ?
As I know, latest kernel use clang instead of gcc to compile. And android 5.4 have dozens of modules (.ko) packaged in ramdisk, which make it difficult to customize kernel.

@newbit1
Copy link

newbit1 commented Apr 21, 2021

Hi, as Android kernel is moving on to v5.4, do you have guide to boot Android emulator with customized kernel v5.4 ?
As I know, latest kernel use clang instead of gcc to compile. And android 5.4 have dozens of modules (.ko) packaged in ramdisk, which make it difficult to customize kernel.

Hi, I know your post was a while ago, but I ran into the same issue and fixed it. Check out my script rootAVD, there is a little Guide linked to XDA, on how to compile the AVD Kernel, its Modules and install the Modules into the ramdisk.img
It also installs Magisk on the same shot.

@demian-mint
Copy link

i have a black screen after start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment