Table of contents
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
利用移动设备编译内核
相较于 PC 或服务器,基于 ARM 架构的移动设备存在性能、散热、内存等多方面限制,编译体验会有所不同,但并非不可行。
本教程以基于 Android 系统的移动设备为例进行演示。
如果只是为了编译内核,移动设备的成本效益远低于一台入门级服务器。确认自己有此需求后再继续。
注意:本项目并没有原生支持ARM架构下的编译,若你有需求,只能通过修改项目源码实现支持。
准备环境
选择容器方案
在 Android 上运行 Linux 环境可选以下方式:
| 方案 | 特点 |
|---|---|
| Proot | 无需 Root,兼容性最广,性能略低 |
| Chroot | 需要 Root,性能较好 |
| LXC | 需要 Root,接近原生性能,隔离性好 |
| Docker | 需要 Root,使用方便,依赖内核支持 |
对应的终端工具可选 Termux、Droidspaces 或其他终端应用。
本教程采用 LXC + Droidspaces 的组合。
准备 Rootfs
使用 Ubuntu 24.04 ARM64 Rootfs 即可。
可参考以下项目获取 Rootfs:
构建基础编译环境
启动 Rootfs 后,执行以下命令安装必要依赖:
apt update && apt upgrade -y
apt install gcc clang make git bison flex libssl-dev ccache cpio -y
至此,基础编译环境搭建完毕。
编译内核
以 LineageOS OnePlus SM8250 内核(分支 lineage-23.2)为例进行演示。
步骤 1:获取内核源码
git clone -b lineage-23.2 \
https://github.com/LineageOS/android_kernel_oneplus_sm8250.git \
kernel_sm8250 --depth 1
cd kernel_sm8250/
步骤 2:生成 .config 文件
make ARCH=arm64 CC="ccache clang" O=out \
vendor/kona-perf_defconfig vendor/oplus.config
步骤 3:编译
make ARCH=arm64 CC="ccache clang" O=out \
-j$(nproc --all) 2>&1 | tee error.log
-j后的值建议按设备可承受的核心数调整,也可直接使用-j$(nproc --all)调用全部核心。注意移动设备长时间满载可能导致发热降频,适当降低并发数(如-j4)有时反而更稳定。
以一加 8 为例,初次编译时间约为 30 分钟至 1 小时。
完成
编译完成后,若日志中出现 Image 相关输出,则代表编译成功。类原生 ROM 设备此时即可刷入并开机验证。
🌐 Language / 语言
📖 English
Local Build
- Setting Up the Environment
- Choosing a Toolchain
- Compiling the Kernel
- Compiling on a Mobile Device
- Compiling the Stock Vendor Kernel
- Embedding KernelSU & Patching SuSFS
- Packaging and Flashing
- Kernel Build FAQ
Build with Actions
- Fork the Repository
- Configuring the YML File
- Variable Reference
- Running and Downloading Artifacts
- Automated Release
Hide Root Trace
📖 中文
基础编译
利用 Action 构建内核
隐藏Root痕迹