为 Pixel 3 构建自定义内核的 Android 11


最近的项目需要在 Android 平台上实验,于是在新的测试机 (📱Pixel 3) 上折腾了一番

My Environment

OS: Debian 11

Mobile Device: Google Pixel 3

AOSP Tag: android-11.0.0_r1

AOSP Build ID: RP1A.200720.009

Prepare Building Environment

Make sure you have installed adb and fastboot, if not

Download from Google (recommended!!)

or

1
2
sudo apt install android-tools-adb
sudo apt install android-tools-fastboot

Install some tools

1
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5-dev lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig

Install repo

1
2
3
4
mkdir ~/.bin
export PATH=~/.bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
ln -s /usr/bin/python3 /usr/bin/python

Build Kernel

Get the source code, it will take about 30GB of disk space after building.

1
2
3
mkdir kernel && cd kernel
repo init -u https://android.googlesource.com/kernel/manifest -b android-msm-crosshatch-4.9-android11-qpr2
repo sync
Get tools
1
2
3
4
cd prebuilts
git clone https://android.googlesource.com/kernel/prebuilts/build-tools
mv build-tools kernel-build-tools
export PATH=${WORK_DIR}/kernel/prebuilts/kernel-build-tools/linux-x86/bin:$PATH
If you want kernel can work on android-11.0.0_r1, checkout msm to commit 5bded8e40b62. You can get this commit id in ${WORK_DIR}/android11/device/google/crosshatch-kernel/Image.lz4 after downloading the Android source.
1
2
cd ${WORK_DIR}/kernel/private/msm-google
git checkout 5bded8e40b62
Build
1
2
cd ${WORK_DIR}/kernel
. build/build.sh

Build Android

Get the source, it will take about 200GB of disk space after building.

1
2
3
mkdir android11 && cd android11
repo init --depth 1 -u https://android.googlesource.com/platform/manifest -b android-11.0.0_r1
repo sync -c -j8
Get the vendor images from Driver Binaries for Nexus and Pixel Devices
1
2
3
4
wget https://dl.google.com/dl/android/aosp/google_devices-blueline-rp1a.200720.009-6cd41940.tgz
wget https://dl.google.com/dl/android/aosp/qcom-blueline-rp1a.200720.009-f772c38c.tgz
tar -xf google_devices-blueline-rp1a.200720.009-6cd41940.tgz && tar -xf qcom-blueline-rp1a.200720.009-f772c38c.tgz
./extract-google_devices-blueline.sh && ./extract-qcom-blueline.sh
Compile with our new kernel, it costs 2 hours on my i5-10600KF.
1
2
3
4
cp ${WORK_DIR}/kernel/out/android-msm-pixel-4.9/dist/Image.lz4 ${WORK_DIR}/android11/device/google/crosshatch-kernel/Image.lz4
. build/envsetup.sh
lunch aosp_blueline-userdebug
make -j12

Flash Device

After getting the building output, let's flash them to the device. Since $ANDROID_PRODUCT_OUT will be set automatically, we can just use command flashall. If not, set it to ${WORK_DIR}/android11/out/target/product/blueline.

1
2
adb reboot bootloader
fastboot flashall -w

Update Drivers

Actually now your device can work well via adb. But due to driver mismatching, you may not be able to use the touchscreen, wlan etc. To fix this problem

1
2
3
adb root
adb remount -R
adb push ${WORK_DIR}/kernel/out/android-msm-pixel-4.9/dist/*.ko /vendor/lib/modules/
Then reboot. Everything should works now.

Result

References

Building Kernels

Building Android

mkbootfs command not found while trying to build aosp kernel

Built kernel from scratch, touchscreen doesn't work?

Touchscreen not working after building and flashing clean msm kernel for the Google Pixel blueline

AOSP Kernel build for Pixel 5

Pixel 3 上 Linux 内核源码的下载,编译与烧录

Pixel 3 Aosp 自编译内核如何正确的驱动设备正常运行

Pixel 3 AOSP 自编译内核 adb 无法识别设备

Practical Android Debugging Via KGDB

Factory Images for Nexus and Pixel Devices