本系列所有文章

  1. lineageOS编译环境及构建指南
  2. 最新LOS_gsi构建指南
你可以通过使用右下角的小齿轮,来开启黑夜模式和阅读模式。

如果你发现文章内容有错漏、你对文章内容有任何改进意见或者你阅读文章遇到了困难,都可以通过博客首页的邮箱联系我。


操作系统Debian12
*需要root权限

【核心硬件建议:

  • CPU
    核心数:至少 8 核以上。
    性能优先级:多核性能 > 单核性能(编译过程高度并行化)。
  • 内存(RAM)
    最低要求:16GB(勉强可用,但可能因设备代码量不同而频繁卡顿),需要扩展swap,不然进程会被kill。
    推荐配置:32GB 或更高(避免因内存不足导致编译中断)。
  • 存储(硬盘)
    容量:至少 500GB 可用空间(源码 + 编译产物可能占用 300GB 以上,内存不够得开swap、后续加速编译要开ccache,也得个大几十GB)。
    类型:NVMe SSD(编译过程涉及大量文件读写,SSD 速度直接影响效率)。
  • 操作系统
    官方支持:Debian Stable(如 本文档使用的就是Debian12)或 Ubuntu LTS(如 22.04/24.04)。
    其他系统:可尝试 Arch Linux 或 Fedora,但需自行解决依赖兼容性问题。
    尽量避免使用 Windows,WSL 2虽然可行,但性能和兼容性较差。

云编译建议使用tmux,避免由于网络差丢失ssh连接,从而被杀进程。基础使用:

  • 创建会话 tmux new -s ‘这里输入你想创建的会话名称’ 示例: tmux new -s lineage_build
  • 若需要离开tmux窗口,先按 Ctrl+b再按d,即可退出会话
  • 列出tmux后台会话进程,tmux ls
  • 重新连接后进入tmux会话或者查看编译进度,tmux attach -t ‘这里输入你创建的会话名称’ 示例:tmux attach -t lineage_build

配置编译环境,安装软件包、编译依赖、编译工具以及同步lineageOS源代码

sudo apt update && sudo apt upgrade -y
sudo apt install -y bc bison build-essential ccache curl flex g++-multilib gcc-multilib git git-lfs gnupg gperf imagemagick lib32ncurses-dev lib32z1-dev liblz4-tool libncurses5-dev libsdl1.2-dev libssl-dev libwxgtk3.2-dev libxml2 libxml2-utils lzop maven pngcrush python3 python-is-python3 rsync schedtool squashfs-tools tmux unzip wget zip zlib1g-dev openjdk-17-jdk android-sdk-platform-tools-common protobuf-compiler python3-protobuf lib32readline-dev libdw-dev libelf-dev lz4 xsltproc libncurses5 libtinfo5
mkdir -p ~/bin
mkdir -p ~/android/lineage
mkdir ~/.ccache
echo 'export USE_CCACHE=1' >> ~/.bashrc
echo 'export CCACHE_DIR=~/.ccache' >> ~/.bashrc
source ~/.bashrc
ccache -M 50G # 设置缓存大小为 50GB,可以按需求自行调整,但是就算只编译一种设备的映像文件也建议25G起步。
ccache -o compression=true
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git lfs install
git config --global trailer.changeid.key "Change-Id"
cd ~/android/lineage
repo init -u https://github.com/LineageOS/android.git -b lineage-21.0 --git-lfs 【可选参数--no-clone-bundle】 #自行修改要拉取的lineage版本
repo sync

编译lineageOS通用镜像【可以自行选择编译的lineageos的对应脚本】
注意,安卓15的td编译脚本,推荐使用MisterZtr的lineageOS编译脚本。github上可以找到仓库。

git clone https://github.com/AndyCGYan/lineage_build_unified lineage_build_unified -b lineage-21-light
git clone https://github.com/AndyCGYan/lineage_patches_unified lineage_patches_unified -b lineage-21-light
  • td即TD-based,应用TrebleDroid的全量补丁集,对旧设备、mtk处理器、三星等手机有更好的兼容性。
  • light脚本应用的补丁则少得多,几乎只对一般的现代设备有用,编译出来的系统映像文件兼容性比td的要差些。
  • 请按自己的实际情况来选择,一般而言,除了老旧的设备和奇怪的冷门设备,一般可以使用light脚本构建的系统映像文件。
bash lineage_build_unified/buildbot_unified.sh treble 64VN 64VS 64GN #按需要选择参数编译

编译指定设备的lineageos,需要到github下载设备树、驱动、内核

以oneplus 8t为例:
手动下载设备树、内核、专有文件(或通过local_manifest.xml自动拉取)
设备树路径:device/oneplus/kebab
内核路径:kernel/oneplus/sm8250
专有文件路径:vendor/oneplus/kebab 和 vendor/oneplus/sm8250-common
硬件支持路径:hardware/oplus

一加8T 设备树 https://github.com/LineageOS/android_device_oneplus_kebab 文件放置路径 device/oneplus/kebab
SM8250 一加通用设备树:https://github.com/LineageOS/android_device_oneplus_sm8250 文件放置路径device/oneplus/sm8250-common
SM8250 一加内核:https://github.com/LineageOS/android_kernel_oneplus_sm8250 文件放置路径 kernel/oneplus/sm8250
SM8250 一加通用专有文件:https://github.com/TheMuppets/proprietary_vendor_oneplus_sm8250-common 文件放置路径 vendor/oneplus/kebab/sm8250-common
一加8T专有文件:https://github.com/TheMuppets/proprietary_vendor_oneplus_kebab 文件放置路径 vendor/oneplus/kebab
一加硬件支持:https://github.com/LineageOS/android_hardware_oplus 文件放置路径 hardware/oplus

推荐通过 local_manifest.xml 自动拉取

  • 在源码根目录创建 .repo/local_manifests/roomservice.xml
  • 添加设备树、内核、专有文件的仓库定义
    以下是local_manifest.xml参考内容:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<!-- 一加8T 设备树 -->
<project name="LineageOS/android_device_oneplus_kebab" path="device/oneplus/kebab" remote="github" />
<!-- SM8250 通用设备树 -->
<project name="LineageOS/android_device_oneplus_sm8250-common" path="device/oneplus/sm8250-common" remote="github" />
<!-- SM8250 内核 -->
<project name="LineageOS/android_kernel_oneplus_sm8250" path="kernel/oneplus/sm8250" remote="github" />
<!-- 一加8T 专有文件 -->
<project name="TheMuppets/proprietary_vendor_oneplus_kebab" path="vendor/oneplus/kebab" remote="github" />
<!-- SM8250 通用专有文件 -->
<project name="TheMuppets/proprietary_vendor_oneplus_sm8250-common" path="vendor/oneplus/sm8250-common" remote="github" />
<!-- 一加硬件支持 -->
<project name="LineageOS/android_hardware_oplus" path="hardware/oplus" remote="github" />
</manifest>

编译lineageOS映像文件

1】对于编译通用镜像GSI,命令行进入lineageOS源码根目录,使用脚本指令

bash lineage_build_unified/buildbot_unified.sh treble 64VN

即可
输出文件在用户主目录下,build-output里面

2】对于编译指定设备的lineageOS,命令行进入lineageOS源码根目录,依次输入下列指令

source build/envsetup.sh
breakfast kebab
croot
brunch kebab

进入输出文件夹(假设构建完成并且没有错误)

cd $OUT