LineageOS编译之小米Max3

编译环境说明

搭建编译环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo apt update
sudo apt install bc bison build-essential ccache curl flex \
g++-multilib gcc-multilib git gnupg gperf imagemagick \
lib32ncurses5-dev lib32readline-dev lib32z1-dev liblz4-tool \
libncurses5 libncurses5-dev libsdl1.2-dev libssl-dev libxml2 \
libxml2-utils lzop pngcrush rsync schedtool squashfs-tools \
xsltproc zip zlib1g-dev libwxgtk3.0-dev python-enum34 python-mako

# 可设置代理来加速外网下载
# export http_proxy="IP:PORT" && export https_proxy="IP:PORT"

# GIT的优化设置
git config --global user.email "xxx@xx.com"
git config --global user.name "xxx"
git config --global http.sslVerify false
git config --global http.postBuffer 1048576000
git config --global core.compression -1
git config --global http.version HTTP/1.1

获取系统源码

  1. 安装源码管理工具REPO
1
2
3
4
5
6
7
# 谷歌的repo需要python2而清华的repo则是python3
curl https://storage.googleapis.com/git-repo-downloads/repo > /usr/bin/repo
chmod a+x /usr/bin/repo

# 也可使用repo的清华源:https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
# 因为repo运行时访问官方的git源更新自己,设置下面变量可以使用国内源进行更新
# export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
  1. 获取LineageOS系统源码的清单
1
2
3
4
5
6
7
8
9
10
11
12
mkdir lineage && cd lineage
repo init -b lineage-16.0 -u https://github.com/LineageOS/android.git

# REPO中init的相关参数
-u # 指定一个URL,其连接到一个maniest仓库
-m # 在manifest仓库中选择一个xml文件
-b # 选择一个maniest仓库中的一个特殊的分支
--depth=1 # 只下载最近版本的代码,只保留最近的commit版本
--mirror # 建立本地镜像,只在repo第一次初始化的时候使用
--repo-url # 设定repo的版本库地址
--repo-branch # 设定需要检出的分支
--no-repo-verify # 设定不要对repo的签名进行严格的验证
  1. 也可改用清华源来加速下载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
repo init -b lineage-16.0 -u \
https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/LineageOS/android.git

# 将 .repo/manifest.xml 或 .repo/manifests/default.xml 修改如下:
##########################################################################
<remote name="github"
- fetch=".."
+ fetch="https://github.com/" />
- review="review.lineageos.org" />

+ <remote name="lineage"
+ fetch="https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/"
+ review="review.lineageos.org" />

##########################################################################
<remote name="aosp"
- fetch="https://android.googlesource.com"
+ fetch="https://mirrors.tuna.tsinghua.edu.cn/git/AOSP"

##########################################################################
<default revision="refs/heads/lineage-16.0"
- remote="github"
+ remote="lineage"
  1. 根据repo的清单下载LineageOS系统源码
1
2
3
4
5
6
7
8
# 开始下载LineageOS系统源码,可能需要几小时
repo sync

# REPO中sync的相关参数
--no-tags # 减少不需要的tag下载,可以缩减下载的代码量
--no-clone-bundle # 关闭cdn分流下载可以更好的利用网络带宽,加快下载速度
--current-branch # 只拉取当前分支,只在init时指定分支同步时也会拉些没用的
--force-sync # 如果同步失败继续同步,如果文件目录有差异强制覆盖掉

获取设备资源

  1. 需要的设备资源包括但不限于设备源码,内核源码,厂商闭源驱动vendor

  2. 如果有LineageOS官方维护的设备那么可以根据如下获取设备资源

1
2
3
4
5
6
7
8
# 通过网络下载官方支持的设备源码及内核源码
# source build/envsetup.sh # 配置环境变量和编译工具路径等
# breakfast lavender # 例如红米Note7通过网络获取vendor相关文件

# 然后从运行LineageOS的红米Note7上提取厂商闭源驱动vendor相关文件
# 将手机连接电脑,并保证手机能被adb命令操控,还需要手机授予adb root权限
# adb connect 192.168.1.177
# device/xiaomi/lavender/extract-files.sh
  1. 若无官方维护但已有对应的非官方设备源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 小米Max3的非官方设备源码:https://github.com/kirillinda/device_xiaomi_nitrogen
# 查看device_xiaomi_nitrogen/lineage.dependencies可知需要添加哪些依赖

# 方法一:将Max3的设备树及闭源驱动和内核资源添加到repo的清单后通过repo sync下载
# mkdir .repo/local_manifests/ && vi .repo/local_manifests/nitrogen.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<project name="LineageOS/android_packages_resources_devicesettings" path="packages/resources/devicesettings" remote="github" revision="lineage-16.0" />
<project name="kirillinda/device_xiaomi_nitrogen" path="device/xiaomi/nitrogen" remote="github" revision="pie-lineage" />
<project name="kirillinda/vendor_xiaomi_nitrogen" path="vendor/xiaomi/nitrogen" remote="github" revision="pie" />
<project name="kirillinda/vendor_MiuiCamera" path="vendor/MiuiCamera" remote="github" revision="pie-nitrogen" />
<project name="kirillinda/kernel_xiaomi_nitrogen" path="kernel/xiaomi/nitrogen" remote="github" revision="pie" />
</manifest>

# 方法二:使用git直接下载Max3的设备树及闭源驱动和内核资源,在执行完repo sync后执行
git clone -b lineage-16.0 https://github.com/LineageOS/android_packages_resources_devicesettings packages/resources/devicesettings/
git clone -b pie-lineage https://github.com/kirillinda/device_xiaomi_nitrogen device/xiaomi/nitrogen/
git clone -b pie https://github.com/kirillinda/vendor_xiaomi_nitrogen vendor/xiaomi/nitrogen/
git clone -b pie https://github.com/kirillinda/kernel_xiaomi_nitrogen kernel/xiaomi/nitrogen/
git clone -b pie-nitrogen https://github.com/kirillinda/vendor_MiuiCamera vendor/MiuiCamera/
  1. 对于设备既没有官方维护,也没有非官方源码,但是有其他ROM的源码可参考这个移植

开始编译系统

  1. 设置优化编译环境
1
2
3
4
5
6
7
8
# 先把项目源文件用ccache编译器编译,然后缓存编译生成的信息
# 然后在下一次编译时利用这个缓存加快编译的速度。注意首次编译由于还未缓存所以暂时无效
export USE_CCACHE=1 # 开启CCACHE编译器缓存工具
export CCACHE_COMPRESS=1 # 压缩缓存信息
export CCACHE_DIR=~/.ccache # 指定缓存编译生成信息的存储目录
./prebuilts/misc/linux-x86/ccache/ccache -M 50G # 设置CCACHE的缓存大小

export WITH_SU=true # 开启LineageOS自带的Root权限
  1. 开始编译系统源码
1
2
3
source build/envsetup.sh	# 配置环境变量和编译工具路径等
croot # 切换到Android源码的根目录
brunch nitrogen # 开始编译:lineage_nitrogen-userdebug
  1. 刷入生成的LineageOS包前应刷入MIUI 9.9.3的底包
1
2
3
4
5
6
7
8
# 查看device_xiaomi_nitrogen/board-info.txt显示为:
# require version-trustzone=TZ.BF.4.0.7.C3-00007

# 查看lineage-xxx.zip/META-INF/com/google/android/updater-script知可用如下底包
# 国际稳定版MIUI:V10.3.5.0.PEDMIXM
# 中国开发版MIUI:TZ.BF.4.0.7.C3-00007(即MIUI 9.9.3)

# MIUI底包下载:https://xiaomifirmwareupdater.com/archive/firmware/nitrogen/
  1. 生成文件刷入设备
1
2
3
4
5
6
7
8
9
10
11
# 生成的LineageOS系统相关文件在如下位置:
# out/target/product/nitrogen/recovery.img
# out/target/product/nitrogen/lineage-16.0-20201216-UNOFFICIAL-nitrogen.zip

# 如果已刷入TWRP等可直接进入Recovery刷入生成文件
adb reboot recovery

# 如无TWRP等可按如下刷入LineageOS的Recovery
adb reboot bootloader
fastboot flash:raw recovery recovery.img
fastboot reboot

其他问题集锦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 编译源码时内存不足解决方案,注意是在执行完source和lunch之后
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server


# 错误A ######################################################
tInvalidRevisionError: revision refs/tags/android-9.0.0_r46 in platform/prebuilts/misc not found
error: in `sync`: revision refs/tags/android-9.0.0_r46 in platform/prebuilts/misc not found

# 解决A:repo sync platform/prebuilts/misc


# 错误B ######################################################
device/xiaomi/nitrogen/parts/res/values/styles.xml:34: error: resource layout/preference_category_material_settings (aka org.lineageos.settings:layout/preference_category_material_settings) not found.
device/xiaomi/nitrogen/parts/res/values/styles.xml:45: error: resource layout/preference_material_settings (aka org.lineageos.settings:layout/preference_material_settings) not found.
error: failed linking references

# 解决B:git clone -b lineage-16.0 https://github.com/LineageOS/android_packages_resources_devicesettings packages/resources/devicesettings/


# 适用于红米Note7的LineageOS 16.0的资源,完成后brunch lavender即可
git clone -b lineage-16.0 https://github.com/erfanoabdi/android_device_xiaomi_lavender device/xiaomi/lavender
git clone -b lineage-16.0 https://github.com/erfanoabdi/proprietary_vendor_xiaomi_lavender vendor/xiaomi/lavender
git clone -b mkp https://github.com/SubDragonzj/android_kernel_xiaomi_lavender kernel/xiaomi/lavender


# 错误C ######################################################
error: vendor/lineage/build/soong/Android.bp:31:8: module "generated_kernel_includes": cmd: unknown variable '$(KERNEL_MAKE_FLAGS)'

# 解决C:没有选择要构建的设备对象,可先执行breakfast lavender选择构建设备(红米Note7为例)