如何从源码构建 Android ROM

来自Uotan Wiki · 刷机百科

该章节将引导你从源码构建Android ROM

注意:本教程以LineageOS 21.0OnePlus6T[1]为例

部署环境

1. 硬件方面的选择

对于LineageOS 21.0,除非你的电脑拥有较高的配置,我们一般建议你在实体机编译,以下为针对LineageOS 21.0的推荐配置。

磁盘空间 内存
300GB+ 16GB+

你可能还需要设置swap[2]以防止编译失败

2. 选择合适的操作系统

请使用64位的Linux发行版,推荐使用Ubuntu 22.04 LTS

Ubuntu安装教程: https://ubuntu.com/tutorials/install-ubuntu-desktop

Archlinux安装教程:https://wiki.archlinux.org/title/Installation_guide

(也可使用WSL:https://learn.microsoft.com/zh-cn/windows/wsl/install

3. 安装依赖项目

对于 Ubuntu 22.04 LTS等Debian系发行版,执行以下命令

 sudo apt update
 sudo apt upgrade
 sudo apt 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 libsdl1.2-dev libssl-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev git git-lfs lib32ncurses5-dev libncurses5 libncurses5-dev

对于Archlinux, 你可以从AUR安装lineageos-devel

4. 获取repo工具

在国内我们使用清华源,执行以下命令

 mkdir ~/bin
 export PATH=~/bin:$PATH
 cd ~/bin
 curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o repo
 chmod +x repo
 cd ~

5. 设置邮箱和用户名

此步骤只需要重复一次

 git config --global user.name "Your Name"
 git config --global user.email "youremail@yourdomain.com"

下载源代码

1. 镜像源的配置

建议先设置从清华源更新repo工具,并配置git从清华源拉取AOSP源码

 git config --global url.https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/.insteadof https://android.googlesource.com
 export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'

2. 获取主要源代码

这里下载的是LineageOS 21.0的源代码,请灵活操作

  • 小技巧:使用--depth=1可以有效节省流量和加快拉取速度!
2.1 初始化
 mkdir ~/los
 cd ~/los
 repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/LineageOS/android.git -b lineage-21.0 --git-lfs --depth=1
2.2 编写cust.xml防止清华源同步错误
  • 这里用nano编辑器
 nano .repo/local_manifests/cust.xml

写入以下内容

 <?xml version="1.0" encoding="UTF-8"?>
 <manifest>
   <remove-project name="LineageOS/android" />
 </manifest>
2.3 同步
 repo sync --force-sync -j4

3. 获取设备配置

  • 对于官方的设备,一般直接利用breakfast就能拉取除了vendor tree以外的所有依赖项目[3]
  • 此处我们按照非官方设备的方式进行操作
3.1 克隆 device tree
  • 注意一般的设备树(可能有例外)命名方式为device_manufacture_codename, 克隆目录为device/manufacture/codename
 git clone --depth=1 -b lineage-21 https://github.com/LineageOS/android_device_oneplus_fajita.git device/oneplus/fajita
3.2 克隆 common tree (如必要)
  • 命名方式:device_manufacture_SoC-common
 git clone --depth=1 -b lineage-21 https://github.com/LineageOS/android_device_oneplus_sdm845-common.git device/oneplus/sdm845-common
3.3 克隆 hardware (如必要)
 git clone -b lineage-21 https://github.com/LineageOS/android_hardware_oneplus.git hardware/oneplus
3.4 克隆内核源代码
 git clone -b lineage-21 --depth=1 https://github.com/LineageOS/android_kernel_oneplus_sdm845.git kernel/oneplus/sdm845
3.5 克隆vendor blobs
 git clone -b lineage-21 --depth=1 https://github.com/TheMuppets/proprietary_vendor_oneplus_fajita.git vendor/oneplus/fajita
 git clone -b lineage-21 --depth=1 https://github.com/TheMuppets/proprietary_vendor_oneplus_sdm845-common.git vendor/oneplus/sdm845-common

编译

1. 初始化编译环境

 . build/envsetup.sh

2. 启动编译

 lunch lineage_fajita-user
 mka bacon

测试

使用合适的方式将LineageOS安装到你的设备上!