Windows 11 部署 Linux 环境并编译 LineageOS 指南

适配设备:一加9(代号 lemonade)

一、准备 Linux 环境(WSL)
1、安装 WSL 并配置 Ubuntu
在 Windows 终端中执行(需管理员权限) wsl --install -d Ubuntu-24.04

安装完成后,从开始菜单打开 Ubuntu-24.04
首次启动需设置用户名和密码(密码输入时不可见,正常输入后回车)

二、基础环境配置
2、更新软件源并安装工具
添加软件仓库 sudo add-apt-repository universe multiverse
更新软件包列表 sudo apt update
安装解压工具和文本编辑器 sudo apt install unzip gedit

3、安装 Android 调试工具 (ADB)
下载工具到根目录 wget https://dl.google.com/android/repository/platform-tools-latest-linux.zip
解压到用户目录 unzip platform-tools-latest-linux.zip -d ~
添加环境变量:gedit ~/.profile

在文件末尾添加:

点击查看代码
if [ -d "$HOME/platform-tools" ] ; then
    PATH="$HOME/platform-tools:$PATH"
fi

让配置生效 source ~/.profile

三、安装编译依赖
4、安装构建工具链
sudo apt-get install bc bison build-essential ccache curl flex g++-multilib gcc-multilib git git-lfs gnupg gperf imagemagick lib32readline-dev lib32z1-dev libelf-dev liblz4-tool lz4 libsdl1.2-dev libssl-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev

5、配置代码仓库工具 (Repo)
创建工具目录 mkdir -p ~/bin
创建源码目录 mkdir -p ~/android/lineage
下载 Repo 工具 curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
赋予执行权限 chmod a+x ~/bin/repo
编辑 ~/.profile 文件:gedit ~/.profile

点击查看代码
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

让配置生效 source ~/.profile

6、配置 Git 账户
you@example.com 替换为你的邮箱 git config --global user.email "you@example.com"
Your Name 替换为你的用户名 git config --global user.name "Your Name"
启用 Git LFS git lfs install

四、源码下载与配置
7、初始化源码仓库
cd ~/android/lineage

指定国内镜像
设置 repo 工具镜像 export REPO_URL='https://mirrors.ustc.edu.cn/aosp/git-repo'
替换 AOSP 仓库
git config --global url."https://mirrors.ustc.edu.cn/aosp/".insteadOf https://android.googlesource.com/
git config --global url."https://gitclone.com/github.com/LineageOS/".insteadOf https://github.com/LineageOS/
初始化仓库 repo init -u https://mirrors.cernet.edu.cn/lineageOS/LineageOS/android.git -b lineage-22.1 --git-lfs --no-clone-bundle
同步源码(耗时较长,建议保持网络稳定) repo sync

五、设备驱动与 USB 调试
8、Windows 端 USB 设备共享
打开windows终端运行
安装 USB/IP 工具 winget install --interactive --exact dorssel.usbipd-win
查看设备列表(记录目标设备的 BUSID) usbipd list
绑定设备(如 3-1) usbipd bind --busid <busid>
共享到 WSL usbipd attach --wsl --busid <busid>

在WSL中运行
安装usbutils sudo apt install usbutils
查看usb设备能否被wsl检测到 lsusb

9、WSL 内 USB 权限配置
创建 USB 权限规则 echo 'SUBSYSTEM=="usb", MODE="0666", GROUP="plugdev"' | sudo tee /etc/udev/rules.d/99-android.rules
重新加载规则 sudo udevadm control --reload-rules
触发设备更新 sudo udevadm trigger

六、编译 LineageOS
10、准备设备代码
源码根目录 cd ~/android/lineage
加载编译环境 source build/envsetup.sh
自动配置设备树和依赖项 breakfast lemonade

11、提取设备驱动文件
确保手机已开启 USB 调试 和 Root 权限,连接电脑后执行 cd ~/android/lineage/device/oneplus/lemonade
从手机提取硬件驱动 ./extract-files.py

12、开始编译
启用编译缓存 export USE_CCACHE=1
设置缓存路径 export CCACHE_EXEC=/usr/bin/ccache
设置缓存大小为 50GB ccache -M 50G
在windows 11中 C:\Users\你的用户名 目录下创建 文本文档 ,重命名为 .wslconfig ,打开并添加以下内容,设置WSL可使用的内存,cpu核心数

点击查看代码
[wsl2]
memory=32GB
swap=8GB
processors=4

返回源码根目录 cd ~/android/lineage
加载编译环境 source build/envsetup.sh
启动编译(耗时数小时 #### build completed successfully (11:43:56 (hh:mm:ss)) ####)
切换回源码根目录 croot

完整编译(user debug) brunch lemonade

七、获取编译结果
编译完成后,生成的 ROM 文件目录 cd $OUT

主要文件:
boot.img : 恢复镜像
lineage-*.zip : LineageOS 安装包

posted @ 2025-03-11 21:58  好大一块糖  阅读(1179)  评论(1)    收藏  举报