HOW TO RUN ANDROID EMULATOR WITH CUSTOMIZED KERNEL

 HOW TO RUN ANDROID EMULATOR WITH CUSTOMIZED KERNEL


I. AOSP source code ( from AOSP 11 onward, google introduced GKI which separate android kernel and vendor kernel. kernel source is separated from AOSP )

   1. clone aosp source from google

   2. build emulator

      cd <aosp>

      source ./build/envsetup.sh

      lunch sdk_car_x86_64-userdebug

      make -j$(nproc)

   3. test emulator

      emulator -no-window -no-snapshot -writable-system -show-kernel


II. Build custom android kernel

   1. Find android kernel version compatible with current working emulator 

      at step I.3, you will file in the log current kernel of emulator, sth like 

      "[    0.000000] Linux version 5.10.66-android12-9-00021-g2c152aa32942-ab8087165 (build-user@build-host) (Android (7284624, based on r416183b) clang version 12.0.5"

      It means that the current kernel version is "android12-5.10"


   2. Clone android kernel source and build

      Mainly follow google guide - https://source.android.com/docs/setup/build/building-kernels#id-version-from-aosp

      I am working with AOSP12 so I will clone android12-5.10

      repo init -u https://android.googlesource.com/kernel/manifest -b common-android12-5.10


   3. Build common-module

      cd <android-kernel> ### <android-kernel> is dir you clone android-kernel

      BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 build/build.sh


   4. Build customize bootloader image ( custom kernel )

      Please read below message which extracted from AI answers, below steps will follow this strategy .

      "The recommended "Permanent Method" for kernel customization involves creating a separate custom.config file. This file is dedicated to storing only the specific configuration options that the developer wishes to enable, disable, or modify. The custom.config is then integrated into the build process, typically by being merged into the .config file after the base defconfig has been applied. This ensures that custom settings override or add to the default ones, making the changes persistent across builds and clean operations. For debugging or inspection, the configuration of a currently running kernel can often be retrieved by pulling the compressed config.gz file from /proc on a connected device using adb pull /proc/config.gz, which can then be uncompressed for analysis."

      4.1 cd <android-kernel> ### <android-kernel> is dir you clone android-kernel

      4.2 BUILD_CONFIG=common/build.config.gki.x86_64 build/build.sh    

      4.3 cd out/*/dist

      4.4 make menuconfig

      4.5 in menuconfig, select "Load" to load file config which is created at step 4.2 <android-kernel>/out/<android-kernel-version>/common/.config

      4.6 enable/disable/modify kernel config in menuconfig

      4.7 select "Save" in menuconfig

      4.8 the configuration will be saved as ".config"

      4.9 make savedefconfig

      4.10 cp defconfig <android-kernel>/common/arch/x86/configs/<your_defconfig>

      4.11 # Clean previous build (optional but recommended for major changes)

           BUILD_CONFIG=common/build.config.gki.x86_64 build/build.sh clean

      4.12 Edit <android-kernel>/common/build.config.gki ( This way seems that it is not the good way to apply new config. Pls fix me if you know the good way )

           replace "DEFCONFIG=gki_defconfig" by "DEFCONFIG=<your_defconfig>"

      4.13 # Rebuild with your new configuration

           BUILD_CONFIG=common/build.config.gki.x86_64 build/build.sh

      4.14 Verify new configure is applied to built kernel

           cat <android-kernel>/out/<android-kernel-version>/common/.config | grep <CONFIG>

           file <android-kernel>/out/<android-kernel-version>/dist/bzImage

           output of "file" command should be similar - "bzImage: Linux kernel x86 boot executable bzImage, version 5.10.237-android12-9-00008-g5e2e421acf53-dirty"


III. Integrate customize kernel to anroid emulator

   1. Edit kernel make file of emulator <aosp>/device/generic/goldfish/x86_64-kernel.mk likely

      """ 

          TARGET_KERNEL_USE ?= 5.10

          #KERNEL_MODULES_PATH := kernel/prebuilts/common-modules/virtual-device/$(TARGET_KERNEL_USE)/x86-64

          KERNEL_MODULES_PATH := <android-kernel>/out/android12-5.10/dist

          KERNEL_MODULES_EXCLUDE := \

              $(KERNEL_MODULES_PATH)/virt_wifi.ko \

              $(KERNEL_MODULES_PATH)/virt_wifi_sim.ko

          BOARD_VENDOR_RAMDISK_KERNEL_MODULES += \

              $(filter-out $(KERNEL_MODULES_EXCLUDE), $(wildcard $(KERNEL_MODULES_PATH)/*.ko))

          #EMULATOR_KERNEL_FILE := kernel/prebuilts/$(TARGET_KERNEL_USE)/x86_64/kernel-$(TARGET_KERNEL_USE)

          EMULATOR_KERNEL_FILE := <android-kernel>/out/android12-5.10/dist/bzImage

       """

   2. Rebuild android emulator

   3. run emulator

   4. verify new config applied or not

      4.1 adb shell

      4.2 zcat /proc/config.gz | grep <CONFIG>

   


Comments

Popular posts from this blog

Static vs. Shared Libraries: Understanding C++ Dependencies

The best books C++