Build AOSP Kernel

This page details the process of building custom kernels for Android devices. The following instructions guide you through the process of selecting the right sources, building the kernel, and embedding the results into a system image built from the Android Open Source Project (AOSP).

More recent kernel sources can be acquired using repo and be built without further configuration by running build/build.sh from the root of your source checkout.

The root of the kernel source checkout contains build/build.sh. The Android tree contains only prebuilt kernel binaries. The kernel trees contain the kernel sources and all tools to build the kernels, including this script.

For older kernels or kernels not listed below, refer to the instructions on how to build legacy kernels.

Downloading sources and build tools

For recent kernels, the sources, toolchain and build scripts can be downloaded with repo. Some kernels (e.g. the Pixel 3 kernels) require sources from multiple git repositories, while others like the common kernels require only a single one. Using the repo approach ensures a correct source directory setup.

Download the sources for the appropriate branch:

repo init -u https://android.googlesource.com/kernel/manifest -b BRANCH

 

repo sync

 

The following table lists the BRANCH names for kernels available through this method:

DeviceBinary path in AOSP treeRepo branches
Pixel 3 (blueline)device/google/crosshatch-kernelandroid-msm-crosshatch-4.9-pie-qpr2
Pixel 3 XL (crosshatch)
Pixel 2 (walleye)device/google/wahoo-kernelandroid-msm-wahoo-4.4-pie-qpr2
Pixel 2 XL (taimen)
Pixel (sailfish)device/google/marlin-kernelandroid-msm-marlin-3.18-pie-qpr2
Pixel XL (marlin)
Hikeydevice/linaro/hikey-kernelandroid-hikey-linaro-4.4
android-hikey-linaro-4.9
android-hikey-linaro-4.14
android-hikey-linaro-4.19
Hikey960device/linaro/hikey-kernelandroid-hikey960-linaro-4.4
android-hikey960-linaro-4.9
android-hikey960-linaro-4.14
android-hikey960-linaro-4.19
Android Common KernelN/Acommon-android-4.4
common-android-4.9
common-android-4.14
common-android-4.19

 

Building the kernel

Then build the kernel with:

build/build.sh

 

 

Note: Common kernels are generic, customizable kernels and therefore do not define a default configuration. Please see the customize build section to find out how to specify the BUILD_CONFIG for common kernels.

The kernel binary, modules and corresponding image are located in the out/BRANCH/dist directory.

Running the kernel

There are multiple ways of running a custom-built kernel. The following are known ways suitable for various development scenarios:

Embedding into the Android image build

Copy the Image.lz4-dtb to the respective kernel binary location within the AOSP tree and rebuild the boot image.

Alternatively, you can define the TARGET_PREBUILT_KERNEL variable while using make bootimage (or any other makecommand line that builds a boot image). This variable is supported by all devices as it is set up viadevice/common/populate-new-device.sh. For example:

export TARGET_PREBUILT_KERNEL=DIST_DIR/Image.lz4-dtb

 

Flashing and booting kernels with fastboot

Most recent devices have a bootloader extension to streamline the process of generating and booting a bootimage. To boot the kernel without flashing:

adb reboot bootloader
fastboot boot Image.lz4-dtb

 

Using this method, the kernel is not actually flashed and hence won't persist across a reboot.

 

Note: Kernel names differ by device. To locate the correct filename for your kernel, refer to device/VENDOR/NAME-kernel in the AOSP tree.

Customize the kernel build

The build process and outcome can be influenced by environment variables. Most of them are optional and each kernel branch should come with a proper default configuration. The most frequently used ones are listed here. For a complete (and up-to-date) list, refer to build/build.sh.

Environment variableDescriptionExample
BUILD_CONFIGBuild config file to initialize the build environment from. The location is to be defined relative to the repo root directory. Defaults to 'build.config'.
Mandatory for common kernels!
BUILD_CONFIG=common/build.config.cuttlefish.x86_64
OUT_DIRBase output directory for the kernel build.OUT_DIR=/path/to/my/out
DIST_DIRBase output directory for the kernel distribution.OUT_DIR=/path/to/my/dist
CCOverride compiler to be used. Falls back to the default compiler defined by build.config.CC=clang
SKIP_MRPROPERSkip make mrproperSKIP_MRPROPER=1
SKIP_DEFCONFIGSkip make defconfigSKIP_DEFCONFIG=1

 

Custom kernel config for local builds

If you require to regularly switch a kernel configuration option, e.g. when working on a feature, or need an option to be set for development purposes, you can achieve that flexibility by maintaining a local modification or copy of the build config.

Set the variable POST_DEFCONFIG_CMDS to a statement that is evaluated right after the usual make defconfig step has been done. As the build.config files are sourced into the build environment, functions defined in build.configcan be called as part of the post-defconfig commands.

A common example is disabling LTO (Link Time Optimization) for crosshatch kernels during development. While LTO is beneficial for released kernels, the overhead at build time can be significant. The following snippet added to the local build.config will disable LTO persistently when using build/build.sh.

POST_DEFCONFIG_CMDS="check_defconfig && update_debug_config"
function update_debug_config() {
    ${KERNEL_DIR}/scripts/config --file ${OUT_DIR}/.config \
         -d LTO \
         -d LTO_CLANG \
         -d CFI \
         -d CFI_PERMISSIVE \
         -d CFI_CLANG
    (cd ${OUT_DIR} && \
     make O=${OUT_DIR} $archsubarch CROSS_COMPILE=${CROSS_COMPILE} olddefconfig)
}

 

 

Identifying kernel versions

There are multiple ways of identifying the right version to build.

 

Kernel version from AOSP tree

The AOSP tree contains prebuilt kernel versions. Most of the time the git log reveals the correct version as part of the commit message:

cd $AOSP/device/VENDOR/NAME
git log --max-count=1

 

 

Kernel version from system image

To determine the kernel version used in a system image, run the following command against the kernel file:

file kernel

 

 

For Image.lz4-dtb files, run:

grep -a 'Linux version' Image.lz4-dtb
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值