从零开始快速构建Android GKI内核

前置知识

repo

一个易于git的python脚本,可以同时管理多个git项目,批量执行git操作

开始构建

同步源代码

创建一个空文件夹并进入再用repo初始化仓库

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

初始化后在通用内核镜像发布构建中找到需要构建的内核版本,在所需版本的内核工件中下载manifest_xxxxx.xml并移动到.repo/manifests/manifests.xml

注意:对于已废弃的构建版本需要将uptream和dest-branch改为正确的分支
例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version='1.0' encoding='UTF-8'?>

<manifest>
<remote name="aosp" fetch="https://android.googlesource.com/" review="https://android.googlesource.com/" />

<default upstream="master-kernel-build-2021" dest-branch="master-kernel-build-2021" remote="aosp" sync-j="4" />

<superproject name="kernel/superproject" remote="aosp" revision="common-android12-5.10-2023-05" />

<!-- other -->

<project path="common" name="kernel/common" revision="f23f00da6b8ec6e2f99cc9659912c34fdee55237" upstream="android12-5.10-2023-05" dest-branch="android12-5.10-2023-05" />

<!-- other -->
</manifest>

common/android12-5.10-2023-05已废弃,所以该分支不存在,需要将upstream和dest-branch改为deprecated/android12-5.10-2023-05

修改完成后指定清单文件再次初始化

1
2
repo init -m manifest.xml 
# manifest.xml为.repo/manifests下需要指定的清单文件的文件名

初始化完成后开始同步源代码

1
repo sync

构建

对于Android12以及更低的版本,使用build.go构建

1
BUILD_CONFIG=xxx build/build.sh

BUILD_CONFIG为构建配置文件的文件名,在common下,如需要构建arm64的GKI内核则指定

1
BUILD_CONFIG=common/build.config.gki.aarch64 build/build.sh

对于运行内存小于24G的设备建议加上LTO=thin

1
LTO=thin BUILD_CONFIG=common/build.config.gki.aarch64 build/build.sh

对于Android13及以上版本可使用bazel构建

1
tools/bazel run //common:kernel_aarch64_dist