[GUIDE][Build|Mod|Update][kernel-ranchu][goldfish][5.4][5.10][GKI][ramdisk.img][modules][rootAVD][Android 11(R) 12(S)][AVD][Google Play Store API]

Search This thread

newbit

Senior Member
Nov 16, 2008
201
73
Hello Fellows,

with this Guide I would like to show you a much more easier and reliable way,
on how to build, mod and update your AVDs Kernel with its modules.
By using the official AOSP Build ENV.

The "classic" guide will remain at the end of this thread within a spoiler.

The Development Environment:
AVDs:
rootAVD - To install Magisk and the Kernels Modules into the ramdisk.img

Bash:
#############################################|#############################################
######                  Build an AVD Kernel and its Ramdisk Modules                  ######
######                        with the official AOSP Build ENV                       ######
#############################################|#############################################

#############################################|#############################################
######                  Install build dependencies, libs and tools                   ######
#############################################|#############################################

sudo apt-get install -y build-essential libssl-dev kernel-package libncurses5-dev bzip2 \
lib32z1 bison flex libelf-dev qt5-default qttools5-dev-tools qttools5-dev meld geany \
gtk+-2.0 libgtk-3-dev libwebkit2gtk-4.0-dev autogen libgtk2.0-dev libglade2-dev

#############################################|#############################################
######                             Get the AOSP repo bin                             ######
#############################################|#############################################

sudo wget https://storage.googleapis.com/git-repo-downloads/repo -O /usr/bin/repo
sudo chmod a+x /usr/bin/repo

Bash:
#############################################|#############################################
#####                          Android 10 (Q) Kernel 4.14                             #####
#####                 BRANCH=q-goldfish-android-goldfish-4.14-dev                     #####
#############################################|#############################################

#############################################|#############################################
###             Download Sources, Toolchain, Buildtools etc. (approx. 17GB)             ###
#############################################|#############################################

BRANCH=q-goldfish-android-goldfish-4.14-dev 
ROOTDIR=AVD-kernel-$BRANCH
mkdir $ROOTDIR && cd $ROOTDIR
repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b $BRANCH
repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)

#############################################|#############################################
###                 Make changes and enable features via the Menuconfig                 ###
###                     changes will be saved into the gki_defconfig                    ###
###                    i.e. USB 3.0, UHCI HCD for USB-Serial Adapters                   ###
###                       or USB Block Devices like /dev/block/sda1                     ###
###     Device Drivers -> USB support -> <*> xHCI HCD (USB 3.0) support                 ###
###                                      -*- Generic xHCI driver for a platform device  ###
###                                      <*> UHCI HCD (most Intel and VIA) support      ###
###                                      <*> USB Mass Storage support                   ###
###                                      <*> USB Attached SCSI                          ###
#############################################|#############################################

BUILD_CONFIG=goldfish/build.config.goldfish.x86_64 \
build/config.sh

#############################################|#############################################
###                                       1st run                                       ###
###                            Building the Kernel (bzImage)                            ###
#############################################|#############################################

BUILD_CONFIG=goldfish/build.config.goldfish.x86_64 \
SKIP_CP_KERNEL_HDR=1 \
build/build.sh -j$(nproc)

Files copied to /home/newbit/workdir/AVD-kernel-q-goldfish-android-goldfish-4.14-dev/out/x86_64/dist

#############################################|#############################################
###                                   (1+n)th Build run                                 ###
###                        Building only changes, not everything                        ###
#############################################|#############################################

BUILD_CONFIG=goldfish/build.config.goldfish.x86_64 \
SKIP_CP_KERNEL_HDR=1 \
SKIP_MRPROPER=1 \
build/build.sh -j$(nproc)

#############################################|#############################################
###                Copy bzImage as kernel-ranchu into the AVDs directory                ###
#############################################|#############################################

mv ~/path-to-avd-system-images/android-29/kernel-ranchu ~/path-to-avd-system-images/android-29/kernel-ranchu.backup
cp out/x86_64/dist/bzImage ~/path-to-avd-system-images/android-29/kernel-ranchu

#############################################|#############################################
###                       Install Magisk with the rootAVD script,                       ###
###                    download the USB HOST Permissions Module Zip                     ###
###        patch the fstab.ranchu to automount Block Devices like /dev/block/sda1       ###
#############################################|#############################################

./rootAVD.sh ~/Android/Sdk/system-images/android-29/google_apis_playstore/x86_64/ramdisk.img GetUSBHPmodZ PATCHFSTAB

Android_10_Q_01.png
Android_10_Q_02.png


Bash:
#############################################|#############################################
#####                           Android 11 (R) Kernel 5.4                             #####
#####                        BRANCH=common-android11-5.4-lts                          #####
#############################################|#############################################

#############################################|#############################################
###             Download Sources, Toolchain, Buildtools etc. (approx. 22GB)             ###
#############################################|#############################################

BRANCH=common-android11-5.4-lts
ROOTDIR=AVD-kernel-$BRANCH
mkdir $ROOTDIR && cd $ROOTDIR
repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b $BRANCH
repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)

#############################################|#############################################
###                       Preparing and modding the build.config                        ###
###                      add these lines to the BUILD_CONFIG file                       ###
###             common-modules/virtual-device/build.config.goldfish.x86_64              ###
#############################################|#############################################

BUILD_INITRAMFS=1
LZ4_RAMDISK=1
SKIP_CP_KERNEL_HDR=1

#############################################|#############################################
###                 Make changes and enable features via the Menuconfig                 ###
###                     changes will be saved into the gki_defconfig                    ###
###                   i.e. USB 3.0 or UHCI HCD for USB-Serial Adapters                  ###
###     Device Drivers -> USB support -> <*> xHCI HCD (USB 3.0) support                 ###
###                                      -*- Generic xHCI driver for a platform device  ###
###                                      <*> UHCI HCD (most Intel and VIA) support      ###
#############################################|#############################################

BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64 \
FRAGMENT_CONFIG=common/arch/x86/configs/gki_defconfig \
build/config.sh

#############################################|#############################################
###                                       1st run                                       ###
###                          Building the Modules and Kernel                            ###
#############################################|#############################################

BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64 \
build/build.sh -j$(nproc)

Files copied to ~/workdir/AVD-kernel-common-android11-5.4-lts/out/android11-5.4/dist

#############################################|#############################################
###               Copy bzImage & initramfs.img into the rootAVD directory               ###
###                                   and run rootAVD                                   ###
#############################################|#############################################

cp out/android11-5.4/dist/initramfs.img ~/rootAVD/
cp out/android11-5.4/dist/bzImage ~/rootAVD/
./rootAVD.sh ~/path-to-avd-system-images/android-30/ramdisk.img InstallKernelModules

[!] Installing new Kernel Modules
[*] Copy initramfs.img /data/data/com.android.shell/Magisk/tmp/initramfs
[-] Extracting Modules from initramfs.img
Detected format: [lz4_legacy]
Decompressing to [initramfs.cpio]
[*] Removing Stock Modules from ramdisk.img
[!] 5.4.61-android11-2-00064-g4271ad6e8ade-ab6991359
[!] Android (6443078 based on r383902)
[-] Installing new Modules into ramdisk.img
[!] 5.4.113-android11-2-g926c4200b8fc-dirty
[!] Android (7211189, based on r416183)
[*] Adjusting modules.load and modules.dep
[*] Repacking ramdisk ..

#############################################|#############################################
###                                   (1+n)th Build run                                 ###
###                        Building only changes, not everything                        ###
#############################################|#############################################

BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64 \
SKIP_MRPROPER=1 \
build/build.sh -j$(nproc)

#############################################|#############################################
###              Copy bzImage as kernel-ranchu into the AVDs directory                  ###
###            Once the modules are installed, just the kernel is needed                ###
#############################################|#############################################

cp out/android11-5.4/dist/bzImage ~/path-to-avd-system-images/android-30/kernel-ranchu

Android_11_R_01.pngAndroid_11_R_02.png

Bash:
#############################################|#############################################
#####                           Android 12 (S) Kernel 5.10                            #####
#####                        BRANCH=common-android12-5.10-lts                         #####
#############################################|#############################################

#############################################|#############################################
###             Download Sources, Toolchain, Buildtools etc. (approx. 22GB)             ###
#############################################|#############################################

BRANCH=common-android12-5.10-lts
ROOTDIR=AVD-kernel-$BRANCH
mkdir $ROOTDIR && cd $ROOTDIR
repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b $BRANCH
repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)

#############################################|#############################################
###                       Preparing and modding the build.config                        ###
###                      add these lines to the BUILD_CONFIG file                       ###
###         common-modules/virtual-device/build.config.virtual_device.x86_64            ###
#############################################|#############################################

SKIP_CP_KERNEL_HDR=1
FILES="
arch/x86/boot/bzImage
vmlinux
System.map
"
MAKE_GOALS="
bzImage
modules
"

#############################################|#############################################
###                Open the Menuconfig, make changes and enable features                ###
###                     changes will be saved into the gki_defconfig                    ###
###                         i.e. UHCI HCD for USB-Serial Adapters                       ###
###     USB 3.0 is finally standard built in the ranchu-kernel since Android 12 (S)     ###
###     Device Drivers -> USB support -> <*>  UHCI HCD (most Intel and VIA) support     ###
#############################################|#############################################

BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 \
FRAGMENT_CONFIG=common/arch/x86/configs/gki_defconfig \
build/config.sh

#############################################|#############################################
###                                    1st Build run                                    ###
###                          Building the modules and kernel                            ###
#############################################|#############################################

time BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 \
build/build.sh -j$(nproc)

Files copied to ~/workdir/AVD-kernel-common-android12-5.10-lts/out/android12-5.10/dist

real    33m16,742s
user    79m12,894s
sys        11m33,474s

#############################################|#############################################
###               Copy bzImage & initramfs.img into the rootAVD directory               ###
###                                   and run rootAVD                                   ###
###                  the AVD is running with the new Kernel and Modules                 ###
###                   complete the Magisk installation with EnvFixTask                  ###
#############################################|#############################################

cp out/android12-5.10/dist/initramfs.img ~/rootAVD/
cp out/android12-5.10/dist/bzImage ~/rootAVD/
./rootAVD.sh ~/path-to-avd-system-images/android-S/ramdisk.img InstallKernelModules

[!] Installing new Kernel Modules
[*] Copy initramfs.img /data/data/com.android.shell/Magisk/tmp/initramfs
[-] Extracting Modules from initramfs.img
Detected format: [lz4_legacy]
Decompressing to [initramfs.cpio]
[*] Removing Stock Modules from ramdisk.img
[!] 5.10.15-android12-0-00490-gfca78df78ef2-ab7137072
[!] Android (7037181, based on r407598)
[-] Installing new Modules into ramdisk.img
[!] 5.10.31-android12-1-gb0c3c31639b2-dirty
[!] Android (7211189, based on r416183)
[*] Adjusting modules.load and modules.dep
[*] Repacking ramdisk ..

./rootAVD.sh EnvFixTask

#############################################|#############################################
###                                   (1+n)th Build run                                 ###
###                        Building only changes, not everything                        ###
#############################################|#############################################

time BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 \
SKIP_MRPROPER=1 \
build/build.sh -j$(nproc)

#############################################|#############################################
###              Copy bzImage as kernel-ranchu into the AVDs directory                  ###
###            Once the modules are installed, just the kernel is needed                ###
#############################################|#############################################

cp out/android12-5.10/dist/bzImage ~/path-to-avd-system-images/android-S/kernel-ranchu
Android_12_S_01.pngAndroid_12_S_02.png

with this Guide I would like to show some steps on how to build your own Kernel for an Android Studio Device with CLANG.

The Development Environment:
### Install the following tools to work with and to build the kernel
Bash:
sudo apt-get install -y build-essential libssl-dev kernel-package libncurses5-dev bzip2 lib32z1 bison flex
sudo apt-get install -y libelf-dev libelf-devel or elfutils-libelf-dev
sudo apt-get install -y qt5-default qttools5-dev-tools qttools5-dev
sudo apt-get install -y geany git

### Gathering Informations about the Target Kernel
By starting the AVD over the command line, we can append some kernel and verbose options:
emulator -netdelay none -netspeed full -no-snapstorage -avd Pixel_4_API_30 -show-kernel -verbose

The terminal will show the kernel information which are important:
Code:
[    0.000000] Linux version 5.4.61-android11-2-00064-g4271ad6e8ade-ab6991359 ([email protected]) (Android (6443078 based on r383902) clang version 11.0.1 (https://android.googlesource.com/toolchain/llvm-project b397f81060ce6d701042b782172ed13bee898b79), LLD 11.0.1 (/buildbot/tmp/tmp6_m7QH b397f81060ce6d701042b782172ed13bee898b79)) #1 SMP PREEMPT Mon Nov 23 17:45:44 UTC 2020

ValueMeaning
Linux version 5.4.61​
Kernel VERSION.PATCHLEVEL.SUBLEVEL​
android11-2​
retagged Kernel branch and KMI generation number -> android11-5.4​
-g4271ad6e8ade
Local Version String (very important to avoid: disagrees about version of symbol module_layout)​
-ab6991359​
Automatically append version information to the version string​
build-user​
KBUILD_BUILD_USER​
build-host​
KBUILD_BUILD_HOST​
Android (6443078 based on r383902) clang version 11.0.1​
Android Clang/LLVM Prebuilt Version r383902

### Gathering more Informations about what is needed to build the kernel

To figure out what we need to build the kernel, one just needs to look into the build.config file inside the kernel source.
Inside the three build.config files we will find what we need to know.
Makefile:
BRANCH=android11-5.4
KMI_GENERATION=2
CC=clang
LD=ld.lld
NM=llvm-nm
OBJCOPY=llvm-objcopy
DEPMOD=depmod
CLANG_PREBUILT_BIN=prebuilts-master/clang/host/linux-x86/clang-r383902/bin
BUILDTOOLS_PREBUILT_BIN=build/build-tools/path/linux-x86
EXTRA_CMDS=''
STOP_SHIP_TRACEPRINTK=1
IN_KERNEL_MODULES=1
DO_NOT_STRIP_MODULES=1
Makefile:
ARCH=x86_64
CLANG_TRIPLE=x86_64-linux-gnu-
CROSS_COMPILE=x86_64-linux-androidkernel-
LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/bin
FILES="arch/x86/boot/bzImage
vmlinux
System.map"
Makefile:
DEFCONFIG=gki_defconfig
POST_DEFCONFIG_CMDS="check_defconfig"

Apparently we don't need the build.config.gki
Without further ado, the gathered informations from above will eventually lead up to:

### Download the kernel source, CLANG toolchain, Buildtools and GCC
Bash:
mkdir avdkernel5.4compile && cd avdkernel5.4compile
git clone -b android11-5.4 --single-branch https://android.googlesource.com/kernel/common
mkdir clang-r383902 && cd clang-r383902
wget https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/android-11.0.0_r28/clang-r383902.tar.gz
tar -xzf clang-r383902.tar.gz && cd ..
git clone https://android.googlesource.com/kernel/prebuilts/build-tools
git clone \
    -b android-11.0.0_r28 \
    --single-branch https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9

### Pull the Kernel .config from the AVD
Bash:
adb pull /proc/config.gz
gunzip -k config.gz

### Exports: (must be done in kernel-source directory)
Bash:
cd common
export ARCH=x86_64
export CLANG_TRIPLE=x86_64-linux-gnu-
export CROSS_COMPILE=x86_64-linux-androidkernel-
export LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=$(pwd)/../x86_64-linux-android-4.9/bin
export CLANG_PREBUILT_BIN=$(pwd)/../clang-r383902/bin
export BUILDTOOLS_PREBUILT_BIN=$(pwd)/../build-tools/linux-x86/bin
DEVEXPS="CC=clang LD=ld.lld NM=llvm-nm OBJCOPY=llvm-objcopy DEPMOD=depmod EXTRA_CMDS='' STOP_SHIP_TRACEPRINTK=1 DO_NOT_STRIP_MODULES=1"
export KBUILD_BUILD_USER=build-user
export KBUILD_BUILD_HOST=build-host
export PATH=$LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN:$CLANG_PREBUILT_BIN:$BUILDTOOLS_PREBUILT_BIN:$PATH
make $DEVEXPS mrproper
cp ../config .config

### add -g4271ad6e8ade to LOCALVERSION -> otherwise modules won't load => module xy disagrees about version of symbol module_layout
Bash:
make $DEVEXPS xconfig
LocalVersion.png

If you come back here later, this is the right place to mod your kernel.
I like the GUI Interface because you have a better overview and a search function.

### build it
Bash:
time make $DEVEXPS -j$(nproc)

### Boot the AVD ...
#### ... from the command line

Bash:
emulator -netdelay none -netspeed full -no-snapstorage -avd Pixel_4_API_30 \
    -no-snapshot-load \
    -show-kernel \
    -verbose \
    -ranchu \
    -kernel ~/avdkernel5.4compile/common/arch/x86/boot/bzImage

#### ... by replacing the stock kernel-ranchu
Code:
mv ~/Android/Sdk/system-images/android-30/google_apis_playstore/x86_64/kernel-ranchu \
    ~/Android/Sdk/system-images/android-30/google_apis_playstore/x86_64/kernel-ranchu-backup
cp ~/avdkernel5.4compile/common/arch/x86/boot/bzImage \
    ~/Android/Sdk/system-images/android-30/google_apis_playstore/x86_64/kernel-ranchu

### Compare the Kernel Verbose Messages
Code:
[    0.000000] Linux version 5.4.61-g4271ad6e8ade-00014-g158eae717346 ([email protected]) (Android (6443078 based on r383902) clang version 11.0.1 (https://android.googlesource.com/toolchain/llvm-project b397f81060ce6d701042b782172ed13bee898b79), LLD 11.0.1 (/buildbot/tmp/tmp6_m7QH b397f81060ce6d701042b782172ed13bee898b79)) #1 SMP PREEMPT Mon Jan 11 12:58:18 CET 2021

Thanks for reading
Cheers NewBit

Thanks and Credits to @nathanchance, for his wonderful
[REFERENCE] How to compile an Android kernel
brought me a lot of ideas and inspirations
 
Last edited:

reeferman

New member
Feb 1, 2021
3
1
Hello newbit,

I am currently following your guide but when I try to run
Bash:
make $DEVEXPS xconfig
and it resulted in the following error
Bash:
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
init/Kconfig:34:warning: 'GCC_VERSION': number is invalid
init/Kconfig:45:warning: 'CLANG_VERSION': number is invalid

did you encounter this?
 

newbit

Senior Member
Nov 16, 2008
201
73
Hello newbit,

I am currently following your guide but when I try to run
Bash:
make $DEVEXPS xconfig
and it resulted in the following error
Bash:
clang: error: no such file or directory: 'LD=ld.lld'
clang: error: no such file or directory: 'NM=llvm-nm'
clang: error: no such file or directory: 'OBJCOPY=llvm-objcopy'
clang: error: no such file or directory: 'DEPMOD=depmod'
clang: error: no such file or directory: 'EXTRA_CMDS='
clang: error: no such file or directory: 'STOP_SHIP_TRACEPRINTK=1'
clang: error: no such file or directory: 'DO_NOT_STRIP_MODULES=1'
...
init/Kconfig:34:warning: 'GCC_VERSION': number is invalid
init/Kconfig:45:warning: 'CLANG_VERSION': number is invalid

did you encounter this?

Hmm, looks weird. No I didn't. Also it is a bit taken out of context. It seems that your $DEVEXPS content gets repeated many times.
Do you have more background infos for me? Like your development environment and If you did change little things? Maybe a typo in the $PATH?
 
Last edited:

reeferman

New member
Feb 1, 2021
3
1
Pretty weird, but I was using kali to do it before and when I switched Linux Mint eveerything worked!:)
 
  • Like
Reactions: newbit

rkull

New member
Dec 2, 2021
3
0
Hi newbit!

Thanks for some great tutorials, unfortunately for me I can't get it to work properly. Hopefully, you're able to help me :)

I'm trying to build and install a custom kernel in an AOSP built emulator. I've been trying out the "official" guide at https://source.android.com/setup/build/building-kernels#customize-build, your rootAVD guide for Android 11 and the "classic" guide for Android 11. All of the guides give me the same result:

Code:
[    0.610588] Run /init as init process
[    0.611985] init: init first stage started!
[    0.612930] init: Loading module /lib/modules/dummy-cpufreq.ko with args ""
[    0.613579] dummy_cpufreq: disagrees about version of symbol module_layout
[    0.614170] init: Failed to insmod '/lib/modules/dummy-cpufreq.ko' with args ''
[    0.614793] init: LoadWithAliases was unable to load dummy_cpufreq
[    0.615366] init: [libfs_mgr]ReadFstabFromDt(): failed to read fstab from dt
[    0.616164] init: Using Android DT directory /proc/device-tree/firmware/android/
[    0.629196] init: bool android::init::BlockDevInitializer::InitDevices(std::set<std::string>): partition(s) not found in /sys, waiting for their uevent(s): metadata, super, vbmeta

After that, the emulator hangs for a short while and the it reboots to try again..

In your "classic" guide, you're warning about this:
### add -g4271ad6e8ade to LOCALVERSION -> otherwise modules won't load => module xy disagrees about version of symbol module_layout

When I checked the version of my running emulator i got this:
Code:
Linux version 5.4.50-01145-g056684c0d252-ab6656030 ([email protected]) (Android (6443078 based on r383902) clang version 11.0.1 (https://android.googlesource.com/toolchain/llvm-project b397f81060ce6d701042b782172ed13bee898b79), LLD 11.0.1 (/buildbot/tmp/tmp6_m7QH b397f81060ce6d701042b782172ed13bee898b79)) #1 SMP PREEMPT Mon Jul 6 18:09:10 UTC 2020

So I used xconfig to add the "-g056684c0d252" string and also made sure to use the r383902 clang toolchain to compile, but no luck.. After compilation and running with the new kernel, I get this version print:
Code:
[    0.000000] Linux version 5.4.162-g056684c0d252-android11-2-00102-gfe0ed45e42fe-dirty ([email protected]) (Android (6443078 based on r383902) clang version 11.0.1 (https://android.googlesource.com/toolchain/llvm-project b397f81060ce6d701042b782172ed13bee898b79), LLD 11.0.1 (/buildbot/tmp/tmp6_m7QH b397f81060ce6d701042b782172ed13bee898b79)) #1 SMP PREEMPT Fri Nov 26 10:38:38 UTC 2021

Can you see anything obvious from that?

When I tried with rootAVD, I'm getting this output when running the script:
Code:
./rootAVD.sh /aosp/out/target/product/myproduct/ramdisk.img InstallKernelModules
[!] and we are NOT in an emulator shell
[*] Set Directorys
[-] Test if ADB SHELL is working
[-] In any AVD via ADB, you can execute code without root in /data/data/com.android.shell
[-] Magisk installer Zip exists already
[*] Cleaning up the ADB working space
[*] Creating the ADB working space
[*] Push Magisk.zip into /data/data/com.android.shell/Magisk
[-] ./Magisk.zip: 1 file pushed, 0 skipped. 254.4 MB/s (6874374 bytes in 0.026s)
[*] create Backup File of ramdisk.img
[*] Push ramdisk.img into /data/data/com.android.shell/Magisk
[-] /ramdisk.img: 1 file pushed, 0 skipped. 1006.6 MB/s (903723 bytes in 0.001s)
[*] Push initramfs.img into /data/data/com.android.shell/Magisk
[-] ./initramfs.img: 1 file pushed, 0 skipped. 299.9 MB/s (2256062 bytes in 0.007s)
[*] Push rootAVD.sh into /data/data/com.android.shell/Magisk
[-] rootAVD.sh: 1 file pushed, 0 skipped. 243.4 MB/s (7826127 bytes in 0.031s)
[-] run the actually Boot/Ramdisk/Kernel Image Patch Script
[*] from Magisk by topjohnwu and modded by NewBit XDA
[!] We are in an emulator shell
[-] Api Level Arch Detect
[-] Device Platform: x64
[-] ARCH32 x86
[-] Device SDK API: 30
[-] First API Level: 28
[-] Switch to the location of the script file
[*] Extracting busybox and Magisk.zip ...
[-] Checking AVDs Internet connection...
[!] AVD is offline
[*] Re-Run rootAVD in Magisk Busybox STANDALONE (D)ASH
[!] We are in an emulator shell
[-] We are now in Magisk Busybox STANDALONE (D)ASH
[*] rootAVD with Magisk '23.0' Installer
[-] Get Flags
[*] System-as-root, keep dm/avb-verity
[-] Encrypted data, keep forceencrypt
[*] RECOVERYMODE=false
[-] KEEPVERITY=true
[*] KEEPFORCEENCRYPT=true
[*] No 64-Bit Binarys found, please consider Magisk Alpha
[-] copy all x86 files from /data/data/com.android.shell/Magisk/lib/x86 to /data/data/com.android.shell/Magisk
[*] Detecting ramdisk.img compression
[!] Ramdisk.img uses gzip compression
[-] taken from shakalaca's MagiskOnEmulator/process.sh
[*] executing ramdisk splitting / extraction / repacking
[-] API level greater then 30
[*] Check if we need to repack ramdisk before patching ..
[*] After decompressing ramdisk.img, magiskboot will work
Detected format: [gzip]
[-] Test patch status and do restore
[-] Checking ramdisk status
[-] STATUS=0
[-] Stock boot image detected
[-] Patching ramdisk
[*] adding overlay.d/sbin folders to ramdisk
Loading cpio: [ramdisk.cpio]
Create directory [overlay.d] (0750)
Create directory [overlay.d/sbin] (0750)
Dump cpio: [ramdisk.cpio]
[!] PATCHFSTAB=false
[!] Skipping fstab.ranchu patch with /dev/block/sda
[?] If you want fstab.ranchu patched, Call rootAVD with PATCHFSTAB
[!] AddRCscripts=false
[!] Skip adding *.rc scripts into ramdisk.img/sbin/*.rc
[?] If you want *.rc scripts added into ramdisk.img/sbin/*.rc, Call rootAVD with AddRCscripts
[!] patching the ramdisk with Magisk Init
Loading cpio: [ramdisk.cpio]
Add entry [init] (0750)
Add entry [overlay.d/sbin/magisk32.xz] (0644)
Add entry [overlay.d/sbin/magisk64.xz] (0644)
Patch with flag KEEPVERITY=[true] KEEPFORCEENCRYPT=[true]
Loading cpio: [ramdisk.cpio.orig]
Backup mismatch entry: [init] -> [.backup/init]
Record new entry: [overlay.d] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin/magisk32.xz] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin/magisk64.xz] -> [.backup/.rmlist]
Create directory [.backup] (0000)
Add entry [.backup/.magisk] (0000)
Dump cpio: [ramdisk.cpio]
[*] repacking back to ramdisk.img format
[!] Rename Magisk.zip to Magisk.apk
[*] Pull ramdiskpatched4AVD.img into ramdisk.img
[-] /data/data/com.android.shell/Magisk/ramdiskpatched4AVD.img: 1 file pulled, 0 skipped. 294.3 MB/s (1270228 bytes in 0.004s)
[*] Pull Magisk.apk into
[-] /data/data/com.android.shell/Magisk/Magisk.apk: 1 file pulled, 0 skipped. 358.8 MB/s (6342346 bytes in 0.017s)
[*] create Backup File of kernel-ranchu
[*] Copy ./bzImage (Kernel) into kernel-ranchu
[-] Clean up the ADB working space
[-] Install all APKs placed in the Apps folder
[*] Trying to install Apps/Magisk.apk
[*] Performing Streamed Install
[*] Success
[-] Shut-Down & Reboot (Cold Boot Now) the AVD and see if it worked
[-] Root and Su with Magisk for Android Studio AVDs
[-] Modded by NewBit XDA - Jan. 2021
[!] Huge Credits and big Thanks to topjohnwu, shakalaca and vvb2060
[-] Trying to shut down the AVD
[!] If the AVD doesn't shut down, try it manually!

Which is not the same as the one you're showing in the rootAVD guide. Is it anything I'm doing incorrect here or has the prints just changed?

Would be really awesome if you're able to help in any way :) let me know if you need more info!
 

rkull

New member
Dec 2, 2021
3
0
I was a little quick on asking questions :D Got it working! It was the Linux version that was off, the one running in the emulator was 5.4.50 while I was compiling 5.4.162.

When I checked out the commit that had 5.4.50, it all worked like a charm doing it with your "classic" guide!

Thanks again for the awesome guides! :)
 

newbit

Senior Member
Nov 16, 2008
201
73
I was a little quick on asking questions :D Got it working! It was the Linux version that was off, the one running in the emulator was 5.4.50 while I was compiling 5.4.162.

When I checked out the commit that had 5.4.50, it all worked like a charm doing it with your "classic" guide!

Thanks again for the awesome guides! :)
I am glade it worked for you. Sometimes you need some luck. I had quite some issues with the module versions, even If I added the local version right. So the best solutions so far is, to build the modules as well.
I see that your AVD is offline, make sure the Magisk Version is compatible with your AVD API Level.
Occasionally, even with the module versions right, the AVD boot looped, and I had to wipe the AVD and then
it worked. I don't have an explanation for this, just my observations.
 

rkull

New member
Dec 2, 2021
3
0
Yeah I'm building/installing a custom kernel for an AOSP build, so I'm not using an AVD. Just running the AOSP emulator build:
Code:
source build/envsetup.sh
lunch
emulator
Not packing the build into an AVD but maybe we'll do that in the future :)

When you say
So the best solutions so far is, to build the modules as well.
How's that done exactly? Do I need to do something more that just running
Code:
make $DEVEXPS -j$(nproc)

Or am I still stuck with version 5.4.50 of the kernel? :)

Not that I see the need for me to update the version, but it would be fun to know how it could be done :)
 

newbit

Senior Member
Nov 16, 2008
201
73
Yeah I'm building/installing a custom kernel for an AOSP build, so I'm not using an AVD. Just running the AOSP emulator build:

When you say

How's that done exactly? Do I need to do something more that just running
Code:
make $DEVEXPS -j$(nproc)

Or am I still stuck with version 5.4.50 of the kernel? :)

Not that I see the need for me to update the version, but it would be fun to know how it could be done :)

With the AOSP Build ENV I had to set
Code:
BUILD_INITRAMFS=1
to get the modules build and packed. Don't know how this is done the classical way. Checkout your output directory if you can find
any modules or initramfs.img

rootAVD provides you an option to download prebuild kernels and its modules from AOSP.
You can try this to get a higher Kernel version.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 2
    Hello Fellows,

    with this Guide I would like to show you a much more easier and reliable way,
    on how to build, mod and update your AVDs Kernel with its modules.
    By using the official AOSP Build ENV.

    The "classic" guide will remain at the end of this thread within a spoiler.

    The Development Environment:
    AVDs:
    rootAVD - To install Magisk and the Kernels Modules into the ramdisk.img

    Bash:
    #############################################|#############################################
    ######                  Build an AVD Kernel and its Ramdisk Modules                  ######
    ######                        with the official AOSP Build ENV                       ######
    #############################################|#############################################
    
    #############################################|#############################################
    ######                  Install build dependencies, libs and tools                   ######
    #############################################|#############################################
    
    sudo apt-get install -y build-essential libssl-dev kernel-package libncurses5-dev bzip2 \
    lib32z1 bison flex libelf-dev qt5-default qttools5-dev-tools qttools5-dev meld geany \
    gtk+-2.0 libgtk-3-dev libwebkit2gtk-4.0-dev autogen libgtk2.0-dev libglade2-dev
    
    #############################################|#############################################
    ######                             Get the AOSP repo bin                             ######
    #############################################|#############################################
    
    sudo wget https://storage.googleapis.com/git-repo-downloads/repo -O /usr/bin/repo
    sudo chmod a+x /usr/bin/repo

    Bash:
    #############################################|#############################################
    #####                          Android 10 (Q) Kernel 4.14                             #####
    #####                 BRANCH=q-goldfish-android-goldfish-4.14-dev                     #####
    #############################################|#############################################
    
    #############################################|#############################################
    ###             Download Sources, Toolchain, Buildtools etc. (approx. 17GB)             ###
    #############################################|#############################################
    
    BRANCH=q-goldfish-android-goldfish-4.14-dev 
    ROOTDIR=AVD-kernel-$BRANCH
    mkdir $ROOTDIR && cd $ROOTDIR
    repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b $BRANCH
    repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)
    
    #############################################|#############################################
    ###                 Make changes and enable features via the Menuconfig                 ###
    ###                     changes will be saved into the gki_defconfig                    ###
    ###                    i.e. USB 3.0, UHCI HCD for USB-Serial Adapters                   ###
    ###                       or USB Block Devices like /dev/block/sda1                     ###
    ###     Device Drivers -> USB support -> <*> xHCI HCD (USB 3.0) support                 ###
    ###                                      -*- Generic xHCI driver for a platform device  ###
    ###                                      <*> UHCI HCD (most Intel and VIA) support      ###
    ###                                      <*> USB Mass Storage support                   ###
    ###                                      <*> USB Attached SCSI                          ###
    #############################################|#############################################
    
    BUILD_CONFIG=goldfish/build.config.goldfish.x86_64 \
    build/config.sh
    
    #############################################|#############################################
    ###                                       1st run                                       ###
    ###                            Building the Kernel (bzImage)                            ###
    #############################################|#############################################
    
    BUILD_CONFIG=goldfish/build.config.goldfish.x86_64 \
    SKIP_CP_KERNEL_HDR=1 \
    build/build.sh -j$(nproc)
    
    Files copied to /home/newbit/workdir/AVD-kernel-q-goldfish-android-goldfish-4.14-dev/out/x86_64/dist
    
    #############################################|#############################################
    ###                                   (1+n)th Build run                                 ###
    ###                        Building only changes, not everything                        ###
    #############################################|#############################################
    
    BUILD_CONFIG=goldfish/build.config.goldfish.x86_64 \
    SKIP_CP_KERNEL_HDR=1 \
    SKIP_MRPROPER=1 \
    build/build.sh -j$(nproc)
    
    #############################################|#############################################
    ###                Copy bzImage as kernel-ranchu into the AVDs directory                ###
    #############################################|#############################################
    
    mv ~/path-to-avd-system-images/android-29/kernel-ranchu ~/path-to-avd-system-images/android-29/kernel-ranchu.backup
    cp out/x86_64/dist/bzImage ~/path-to-avd-system-images/android-29/kernel-ranchu
    
    #############################################|#############################################
    ###                       Install Magisk with the rootAVD script,                       ###
    ###                    download the USB HOST Permissions Module Zip                     ###
    ###        patch the fstab.ranchu to automount Block Devices like /dev/block/sda1       ###
    #############################################|#############################################
    
    ./rootAVD.sh ~/Android/Sdk/system-images/android-29/google_apis_playstore/x86_64/ramdisk.img GetUSBHPmodZ PATCHFSTAB

    Android_10_Q_01.png
    Android_10_Q_02.png


    Bash:
    #############################################|#############################################
    #####                           Android 11 (R) Kernel 5.4                             #####
    #####                        BRANCH=common-android11-5.4-lts                          #####
    #############################################|#############################################
    
    #############################################|#############################################
    ###             Download Sources, Toolchain, Buildtools etc. (approx. 22GB)             ###
    #############################################|#############################################
    
    BRANCH=common-android11-5.4-lts
    ROOTDIR=AVD-kernel-$BRANCH
    mkdir $ROOTDIR && cd $ROOTDIR
    repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b $BRANCH
    repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)
    
    #############################################|#############################################
    ###                       Preparing and modding the build.config                        ###
    ###                      add these lines to the BUILD_CONFIG file                       ###
    ###             common-modules/virtual-device/build.config.goldfish.x86_64              ###
    #############################################|#############################################
    
    BUILD_INITRAMFS=1
    LZ4_RAMDISK=1
    SKIP_CP_KERNEL_HDR=1
    
    #############################################|#############################################
    ###                 Make changes and enable features via the Menuconfig                 ###
    ###                     changes will be saved into the gki_defconfig                    ###
    ###                   i.e. USB 3.0 or UHCI HCD for USB-Serial Adapters                  ###
    ###     Device Drivers -> USB support -> <*> xHCI HCD (USB 3.0) support                 ###
    ###                                      -*- Generic xHCI driver for a platform device  ###
    ###                                      <*> UHCI HCD (most Intel and VIA) support      ###
    #############################################|#############################################
    
    BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64 \
    FRAGMENT_CONFIG=common/arch/x86/configs/gki_defconfig \
    build/config.sh
    
    #############################################|#############################################
    ###                                       1st run                                       ###
    ###                          Building the Modules and Kernel                            ###
    #############################################|#############################################
    
    BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64 \
    build/build.sh -j$(nproc)
    
    Files copied to ~/workdir/AVD-kernel-common-android11-5.4-lts/out/android11-5.4/dist
    
    #############################################|#############################################
    ###               Copy bzImage & initramfs.img into the rootAVD directory               ###
    ###                                   and run rootAVD                                   ###
    #############################################|#############################################
    
    cp out/android11-5.4/dist/initramfs.img ~/rootAVD/
    cp out/android11-5.4/dist/bzImage ~/rootAVD/
    ./rootAVD.sh ~/path-to-avd-system-images/android-30/ramdisk.img InstallKernelModules
    
    [!] Installing new Kernel Modules
    [*] Copy initramfs.img /data/data/com.android.shell/Magisk/tmp/initramfs
    [-] Extracting Modules from initramfs.img
    Detected format: [lz4_legacy]
    Decompressing to [initramfs.cpio]
    [*] Removing Stock Modules from ramdisk.img
    [!] 5.4.61-android11-2-00064-g4271ad6e8ade-ab6991359
    [!] Android (6443078 based on r383902)
    [-] Installing new Modules into ramdisk.img
    [!] 5.4.113-android11-2-g926c4200b8fc-dirty
    [!] Android (7211189, based on r416183)
    [*] Adjusting modules.load and modules.dep
    [*] Repacking ramdisk ..
    
    #############################################|#############################################
    ###                                   (1+n)th Build run                                 ###
    ###                        Building only changes, not everything                        ###
    #############################################|#############################################
    
    BUILD_CONFIG=common-modules/virtual-device/build.config.goldfish.x86_64 \
    SKIP_MRPROPER=1 \
    build/build.sh -j$(nproc)
    
    #############################################|#############################################
    ###              Copy bzImage as kernel-ranchu into the AVDs directory                  ###
    ###            Once the modules are installed, just the kernel is needed                ###
    #############################################|#############################################
    
    cp out/android11-5.4/dist/bzImage ~/path-to-avd-system-images/android-30/kernel-ranchu

    Android_11_R_01.pngAndroid_11_R_02.png

    Bash:
    #############################################|#############################################
    #####                           Android 12 (S) Kernel 5.10                            #####
    #####                        BRANCH=common-android12-5.10-lts                         #####
    #############################################|#############################################
    
    #############################################|#############################################
    ###             Download Sources, Toolchain, Buildtools etc. (approx. 22GB)             ###
    #############################################|#############################################
    
    BRANCH=common-android12-5.10-lts
    ROOTDIR=AVD-kernel-$BRANCH
    mkdir $ROOTDIR && cd $ROOTDIR
    repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b $BRANCH
    repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)
    
    #############################################|#############################################
    ###                       Preparing and modding the build.config                        ###
    ###                      add these lines to the BUILD_CONFIG file                       ###
    ###         common-modules/virtual-device/build.config.virtual_device.x86_64            ###
    #############################################|#############################################
    
    SKIP_CP_KERNEL_HDR=1
    FILES="
    arch/x86/boot/bzImage
    vmlinux
    System.map
    "
    MAKE_GOALS="
    bzImage
    modules
    "
    
    #############################################|#############################################
    ###                Open the Menuconfig, make changes and enable features                ###
    ###                     changes will be saved into the gki_defconfig                    ###
    ###                         i.e. UHCI HCD for USB-Serial Adapters                       ###
    ###     USB 3.0 is finally standard built in the ranchu-kernel since Android 12 (S)     ###
    ###     Device Drivers -> USB support -> <*>  UHCI HCD (most Intel and VIA) support     ###
    #############################################|#############################################
    
    BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 \
    FRAGMENT_CONFIG=common/arch/x86/configs/gki_defconfig \
    build/config.sh
    
    #############################################|#############################################
    ###                                    1st Build run                                    ###
    ###                          Building the modules and kernel                            ###
    #############################################|#############################################
    
    time BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 \
    build/build.sh -j$(nproc)
    
    Files copied to ~/workdir/AVD-kernel-common-android12-5.10-lts/out/android12-5.10/dist
    
    real    33m16,742s
    user    79m12,894s
    sys        11m33,474s
    
    #############################################|#############################################
    ###               Copy bzImage & initramfs.img into the rootAVD directory               ###
    ###                                   and run rootAVD                                   ###
    ###                  the AVD is running with the new Kernel and Modules                 ###
    ###                   complete the Magisk installation with EnvFixTask                  ###
    #############################################|#############################################
    
    cp out/android12-5.10/dist/initramfs.img ~/rootAVD/
    cp out/android12-5.10/dist/bzImage ~/rootAVD/
    ./rootAVD.sh ~/path-to-avd-system-images/android-S/ramdisk.img InstallKernelModules
    
    [!] Installing new Kernel Modules
    [*] Copy initramfs.img /data/data/com.android.shell/Magisk/tmp/initramfs
    [-] Extracting Modules from initramfs.img
    Detected format: [lz4_legacy]
    Decompressing to [initramfs.cpio]
    [*] Removing Stock Modules from ramdisk.img
    [!] 5.10.15-android12-0-00490-gfca78df78ef2-ab7137072
    [!] Android (7037181, based on r407598)
    [-] Installing new Modules into ramdisk.img
    [!] 5.10.31-android12-1-gb0c3c31639b2-dirty
    [!] Android (7211189, based on r416183)
    [*] Adjusting modules.load and modules.dep
    [*] Repacking ramdisk ..
    
    ./rootAVD.sh EnvFixTask
    
    #############################################|#############################################
    ###                                   (1+n)th Build run                                 ###
    ###                        Building only changes, not everything                        ###
    #############################################|#############################################
    
    time BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 \
    SKIP_MRPROPER=1 \
    build/build.sh -j$(nproc)
    
    #############################################|#############################################
    ###              Copy bzImage as kernel-ranchu into the AVDs directory                  ###
    ###            Once the modules are installed, just the kernel is needed                ###
    #############################################|#############################################
    
    cp out/android12-5.10/dist/bzImage ~/path-to-avd-system-images/android-S/kernel-ranchu
    Android_12_S_01.pngAndroid_12_S_02.png

    with this Guide I would like to show some steps on how to build your own Kernel for an Android Studio Device with CLANG.

    The Development Environment:
    ### Install the following tools to work with and to build the kernel
    Bash:
    sudo apt-get install -y build-essential libssl-dev kernel-package libncurses5-dev bzip2 lib32z1 bison flex
    sudo apt-get install -y libelf-dev libelf-devel or elfutils-libelf-dev
    sudo apt-get install -y qt5-default qttools5-dev-tools qttools5-dev
    sudo apt-get install -y geany git

    ### Gathering Informations about the Target Kernel
    By starting the AVD over the command line, we can append some kernel and verbose options:
    emulator -netdelay none -netspeed full -no-snapstorage -avd Pixel_4_API_30 -show-kernel -verbose

    The terminal will show the kernel information which are important:
    Code:
    [    0.000000] Linux version 5.4.61-android11-2-00064-g4271ad6e8ade-ab6991359 ([email protected]) (Android (6443078 based on r383902) clang version 11.0.1 (https://android.googlesource.com/toolchain/llvm-project b397f81060ce6d701042b782172ed13bee898b79), LLD 11.0.1 (/buildbot/tmp/tmp6_m7QH b397f81060ce6d701042b782172ed13bee898b79)) #1 SMP PREEMPT Mon Nov 23 17:45:44 UTC 2020

    ValueMeaning
    Linux version 5.4.61​
    Kernel VERSION.PATCHLEVEL.SUBLEVEL​
    android11-2​
    retagged Kernel branch and KMI generation number -> android11-5.4​
    -g4271ad6e8ade
    Local Version String (very important to avoid: disagrees about version of symbol module_layout)​
    -ab6991359​
    Automatically append version information to the version string​
    build-user​
    KBUILD_BUILD_USER​
    build-host​
    KBUILD_BUILD_HOST​
    Android (6443078 based on r383902) clang version 11.0.1​
    Android Clang/LLVM Prebuilt Version r383902

    ### Gathering more Informations about what is needed to build the kernel

    To figure out what we need to build the kernel, one just needs to look into the build.config file inside the kernel source.
    Inside the three build.config files we will find what we need to know.
    Makefile:
    BRANCH=android11-5.4
    KMI_GENERATION=2
    CC=clang
    LD=ld.lld
    NM=llvm-nm
    OBJCOPY=llvm-objcopy
    DEPMOD=depmod
    CLANG_PREBUILT_BIN=prebuilts-master/clang/host/linux-x86/clang-r383902/bin
    BUILDTOOLS_PREBUILT_BIN=build/build-tools/path/linux-x86
    EXTRA_CMDS=''
    STOP_SHIP_TRACEPRINTK=1
    IN_KERNEL_MODULES=1
    DO_NOT_STRIP_MODULES=1
    Makefile:
    ARCH=x86_64
    CLANG_TRIPLE=x86_64-linux-gnu-
    CROSS_COMPILE=x86_64-linux-androidkernel-
    LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/bin
    FILES="arch/x86/boot/bzImage
    vmlinux
    System.map"
    Makefile:
    DEFCONFIG=gki_defconfig
    POST_DEFCONFIG_CMDS="check_defconfig"

    Apparently we don't need the build.config.gki
    Without further ado, the gathered informations from above will eventually lead up to:

    ### Download the kernel source, CLANG toolchain, Buildtools and GCC
    Bash:
    mkdir avdkernel5.4compile && cd avdkernel5.4compile
    git clone -b android11-5.4 --single-branch https://android.googlesource.com/kernel/common
    mkdir clang-r383902 && cd clang-r383902
    wget https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/android-11.0.0_r28/clang-r383902.tar.gz
    tar -xzf clang-r383902.tar.gz && cd ..
    git clone https://android.googlesource.com/kernel/prebuilts/build-tools
    git clone \
        -b android-11.0.0_r28 \
        --single-branch https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9

    ### Pull the Kernel .config from the AVD
    Bash:
    adb pull /proc/config.gz
    gunzip -k config.gz

    ### Exports: (must be done in kernel-source directory)
    Bash:
    cd common
    export ARCH=x86_64
    export CLANG_TRIPLE=x86_64-linux-gnu-
    export CROSS_COMPILE=x86_64-linux-androidkernel-
    export LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=$(pwd)/../x86_64-linux-android-4.9/bin
    export CLANG_PREBUILT_BIN=$(pwd)/../clang-r383902/bin
    export BUILDTOOLS_PREBUILT_BIN=$(pwd)/../build-tools/linux-x86/bin
    DEVEXPS="CC=clang LD=ld.lld NM=llvm-nm OBJCOPY=llvm-objcopy DEPMOD=depmod EXTRA_CMDS='' STOP_SHIP_TRACEPRINTK=1 DO_NOT_STRIP_MODULES=1"
    export KBUILD_BUILD_USER=build-user
    export KBUILD_BUILD_HOST=build-host
    export PATH=$LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN:$CLANG_PREBUILT_BIN:$BUILDTOOLS_PREBUILT_BIN:$PATH
    make $DEVEXPS mrproper
    cp ../config .config

    ### add -g4271ad6e8ade to LOCALVERSION -> otherwise modules won't load => module xy disagrees about version of symbol module_layout
    Bash:
    make $DEVEXPS xconfig
    LocalVersion.png

    If you come back here later, this is the right place to mod your kernel.
    I like the GUI Interface because you have a better overview and a search function.

    ### build it
    Bash:
    time make $DEVEXPS -j$(nproc)

    ### Boot the AVD ...
    #### ... from the command line

    Bash:
    emulator -netdelay none -netspeed full -no-snapstorage -avd Pixel_4_API_30 \
        -no-snapshot-load \
        -show-kernel \
        -verbose \
        -ranchu \
        -kernel ~/avdkernel5.4compile/common/arch/x86/boot/bzImage

    #### ... by replacing the stock kernel-ranchu
    Code:
    mv ~/Android/Sdk/system-images/android-30/google_apis_playstore/x86_64/kernel-ranchu \
        ~/Android/Sdk/system-images/android-30/google_apis_playstore/x86_64/kernel-ranchu-backup
    cp ~/avdkernel5.4compile/common/arch/x86/boot/bzImage \
        ~/Android/Sdk/system-images/android-30/google_apis_playstore/x86_64/kernel-ranchu

    ### Compare the Kernel Verbose Messages
    Code:
    [    0.000000] Linux version 5.4.61-g4271ad6e8ade-00014-g158eae717346 ([email protected]) (Android (6443078 based on r383902) clang version 11.0.1 (https://android.googlesource.com/toolchain/llvm-project b397f81060ce6d701042b782172ed13bee898b79), LLD 11.0.1 (/buildbot/tmp/tmp6_m7QH b397f81060ce6d701042b782172ed13bee898b79)) #1 SMP PREEMPT Mon Jan 11 12:58:18 CET 2021

    Thanks for reading
    Cheers NewBit

    Thanks and Credits to @nathanchance, for his wonderful
    [REFERENCE] How to compile an Android kernel
    brought me a lot of ideas and inspirations
    1
    Pretty weird, but I was using kali to do it before and when I switched Linux Mint eveerything worked!:)