目录
设备信息
设备型号:一加9 (OnePlus 9),已刷入官方LineageOS 22.2并获取root权限(如果还未刷入或未获取root权限,可参考 一加9刷入LineageOS 22.2 + Root教程)
设备代号:lemonade
系统版本:LineageOS 22.2 (Android 15)
编译环境:Ubuntu 22.04.5 LTS 桌面版
硬件配置:650元的28核64G洋垃圾 + 480G固态硬盘(建议至少准备48G内存和300G硬盘空间)
一、准备编译工具
1. 安装apt依赖包
执行命令安装apt依赖包:
sudo apt install net-tools openssh-server vim bc bison build-essential ccache flex g++-multilib gcc-multilib git git-lfs gnupg gperf imagemagick protobuf-compiler python3-protobuf lib32readline-dev lib32z1-dev libdw-dev libelf-dev lz4 libsdl1.2-dev libxml2 libxml2-utils lzop libssl-dev pngcrush schedtool curl squashfs-tools xsltproc zip zlib1g-dev lib32ncurses5-dev libncurses5 libncurses5-dev rsync
2. 安装platform-tools工具
下载压缩包
platform-tools - 夸克网盘https://pan.quark.cn/s/22e7d63826b6
解压到用户家目录,并把adb复制到系统bin目录
cd ~
unzip platform-tools-latest-linux.zip
sudo cp ~/platform-tools/adb /usr/bin/
执行 vim ~/.profile 编辑环境变量配置文件,在底部添加下面的内容
if [ -d "$HOME/platform-tools" ] ; then
PATH="$HOME/platform-tools:$PATH"
fi
执行 source ~/.profile 更新环境变量
3. 安装repo工具
下载文件
repo - 夸克网盘https://pan.quark.cn/s/a9588f510089
执行 sudo cp repo /usr/bin/ 复制到系统bin目录
执行 sudo chmod 755 /usr/bin/repo 设置可执行权限
4. 安装Python2
执行下面的命令安装Python2
sudo apt install python2
sudo ln -s /usr/bin/python2 /usr/bin/python
二、拉取安卓源码
1. 配置Git账户信息
执行下面的命令配置Git账户信息
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
2. 配置Git国内源
执行 vim ~/.gitconfig 编辑Git配置文件,在底部添加下面的内容
[url "https://mirrors.tuna.tsinghua.edu.cn/git/git-repo"]
insteadof = https://gerrit.googlesource.com/git-repo
[url "https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/"]
insteadof = https://review.lineageos.org/
[url "https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/"]
insteadof = https://android.googlesource.com/
[url "https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/LineageOS/"]
insteadof = https://github.com/LineageOS/
执行 git config --list --show-origin 检查并更新配置
3. 初始化源码目录
执行下面的命令完成初始化
mkdir -p ~/android/lineage
cd ~/android/lineage
git lfs install
repo init -u https://github.com/LineageOS/android.git -b lineage-22.2 --git-lfs --no-clone-bundle
执行过程中如果询问是否启用彩色显示及确认Git账户信息,输入 y 确认
执行完成后看到如下提示,即说明初始化完成
repo has been initialized in /home/ubuntu/android/lineage
4. 从国内镜像拉取安卓源码
执行 repo sync 拉取安卓源码(约100GB+),所需的时间取决于网速,拉取期间会出现如下报错内容,无视即可
error: Unable to fully sync the tree
error: Checking out local projects failed.
Failing repos (checkout):
external/chromium-webview/prebuilt/arm
external/chromium-webview/prebuilt/arm64
external/chromium-webview/prebuilt/x86
external/chromium-webview/prebuilt/x86_64
Repo command failed due to the following `SyncError` errors:
Cannot initialize work tree for LineageOS/android_external_chromium-webview_prebuilt_arm
Cannot initialize work tree for LineageOS/android_external_chromium-webview_prebuilt_arm64
Cannot initialize work tree for LineageOS/android_external_chromium-webview_prebuilt_x86
Cannot initialize work tree for LineageOS/android_external_chromium-webview_prebuilt_x86_64
5. 编辑lineage.xml配置文件
执行 vim ~/android/lineage/.repo/manifests/snippets/lineage.xml 编辑配置文件,删除下面的内容
6. 重新拉取并同步源码
重新执行 repo sync 拉取并同步源码,看到如下提示即表示拉取成功
7. 放入缺失的webview.apk
下载刚才拉取源码时缺失的 webview.apk 文件压缩包,解压到用户家目录
chromium-webview.zip - 夸克网盘https://pan.quark.cn/s/6d5d457221d8执行 cp -r ./chromium-webview/ ~/android/lineage/external/ 放入源码对应目录
三、构建完整源码树
1. 拉取设备树文件
执行下面的命令拉取设备树文件
cd ~/android/lineage
source build/envsetup.sh
breakfast lemonade
执行后出现如下报错内容:
In file included from build/make/core/config.mk:406:
In file included from build/make/core/envsetup.mk:351:
build/make/core/product_config.mk:226: error: Cannot locate config makefile for product "lineage_lemonade".
09:43:28 dumpvars failed with: exit status 1
Device lemonade not found. Attempting to retrieve device repository from LineageOS Github (http://github.com/LineageOS).
Failed to fetch data from GitHub
...
** Don't have a product spec for: 'lineage_lemonade'
** Do you have the right repo manifest?
在报错内容找到以下关键信息:
- Cannot locate config makefile for product "lineage_lemonade".
- Device lemonade not found.
- Don't have a product spec for: 'lineage_lemonade'
我们通过错误信息看到,拉取时找不到 lemonade(oneplus9) 设备对应的编译配置文件,查询网上的资料了解到,由于LineageOS官方未支持一些型号的设备,因此我们需要手动配置设备树
2. 手工拉取设备树源码
执行命令 git clone https://github.com/LineageOS/android_device_oneplus_lemonade.git -b lineage-22.2 拉取源码
先执行 mkdir ./device/oneplus/ 创建设备文件夹,再执行 mv ./android_device_oneplus_lemonade/ ./device/oneplus/lemonade 移动并重命名源码文件夹
3. 处理设备树源码依赖
执行 cat ./device/lemonade/lineage.dependencies 查看 android_device_oneplus_lemonade 项目源码的依赖说明,在说明中看到如下依赖内容:
[
{
"repository": "android_device_oneplus_sm8350-common",
"target_path": "device/oneplus/sm8350-common"
}
]
说明我们需要继续拉取 android_device_oneplus_sm8350-common 项目源码,移动到 ./device/oneplus/ 目录并命名为 sm8350-common
拉取后,我们在 sm8350-common 目录下的 lineage.dependencies 文件中又看到如下依赖内容:
[
{
"repository": "android_hardware_oplus",
"target_path": "hardware/oplus"
},
{
"repository": "android_kernel_oneplus_sm8350",
"target_path": "kernel/oneplus/sm8350"
}
]
即说明我们需要继续执行补充依赖项目源码:
- 拉取 android_hardware_oplus 项目,移动到 ./hardware/ 目录并命名为 oplus
- 拉取 android_kernel_oneplus_sm8350 项目(约1GB+),移动到 ./kernel/oneplus/ 目录(需先执行 mkdir ~/android/lineage/kernel/oneplus 创建该目录)并命名为 sm8350
补充完上面2个依赖后,检查发现 ./hardware/oplus/ 及 ./kernel/oneplus/sm8350/ 目录下均不再存在依赖说明文件,即说明依赖已处理完毕
4. 提取设备闭源文件
手机开启USB调试,在开发者选项中勾选允许ADB使用root权限进行调试,执行命令把电脑切换到root会话,提取设备闭源文件(如果手机或电脑未正确获取root权限,会出现报错导致提取不完整或提取失败)
cd ~/android/lineage/device/oneplus/lemonade
sudo su
adb kill-server
./extract-files.py
exit
5. 再次执行breakfast
再次执行 breakfast lemonade,不再出现报错信息,即说明完整源码树已构建完毕
四、编译LineageOS刷机包
1. 编译刷机包
执行 brunch lemonade 编译刷机包(电脑剩余内存需大于48G,否则在构建Android.bp阶段会因为内存不足意外中断)
2. 刷入测试
从 ~/android/lineage/out/target/product/lemonade/lineage-22.2-2025xxxx-UNOFFICIAL-lemonade.zip 拷贝刷机包到手机,刷入手机测试(刷机步骤参考:一加9刷入LineageOS 22.2 + Root教程),刷入后在手机设备信息中看到构建日期变为 2025xxxx,即说明编译成功
特别说明:使用LOS官方Recovery通过ADB sideload刷入过程中,手机上会提示签名验证失败,是否继续刷入,这时候要选第二项Yes即可正常刷入
总结
作者因为很害怕,所以这里并没有对文章进行总结,但贴了一张Hanser的壁纸XD