AOSP的编译

前言

推荐使用海外服务器进行编译,节约时间,节约生命!

并且,官方推荐使用Ubuntu LTS进行编译,算上输出物需要500+G的空间。

官方文档见下载源代码 | Android 开源项目 | Android Open Source Project Building Android | Android Open Source Project

本文示例编译版本为android-8.1.0_r39

编译过程

环境配置

首先参考搭建编译环境  |  Android Open Source Project说明来搭建配置环境

注意,上述说明中是基于Ubuntu14来进行的,在目前最新的LTS 24.04版本中,git-core即是git,lib32z-dev即是lib32z1-dev。

并且lib32ncurses5-dev已经被删除了,则是需要如下操作

sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libncurses5-dev:i386

剩下的都可以正常安装。

然后就是安装git和repo,如下

sudo apt install git repo

repo安装后运行可能会line 78,这时需要把系统的python2和/bin/python删掉,然后建立建立python3的软连接到/bin/python,repo就可以正常运行了

ln -s /usr/bin/python3 /usr/bin/python

初始化仓库

主分支

repo init -u https://android.googlesource.com/platform/manifest

repo init -u https://android.googlesource.com/platform/manifest -b master

也可以使用清华的镜像,根据说明,由于硬盘 I/O 资源有限,Git 服务器每 IP 限制 5 个并发连接。repo sync 命令默认使用 4 个并发连接,请勿使用 -j 参数增加并发连接数。

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b master

其他分支,在b后面添加分支 分支列表见代号、标记和 build 号  |  Android Open Source Project,例如repo init -b android-14.0.0_r25

同步源码

repo sync -c -j8

其中-c为当前分支,-j8意为使用8个线程。如果使用镜像,可能需要减少并发。

安装驱动

根据想要刷写的设备安装相应驱动,驱动列表见Nexus 和 Pixel 设备的驱动程序二进制文件  |  Google Play services  |  Google for Developers

下载后执行脚本即可安装,注意要在源码目录下执行

对于LneageOS可以使用./extract-files.sh(或者./extract-files.py)。这需要手动克隆设备,即git clone https://github.com/LineageOS/android_device_google_coral device/google/coral

然后进入到device/google/coral,执行./extract-files.py。这有可能需要zsh环境,如果没有,可以使用以下方案

cd device/google/coral/coral

# 创建包装脚本
cat > run_extract.sh << 'EOF'
#!/bin/bash
cd ~/Source/LineageOS/device/google/coral/coral
PYTHONPATH=../../../../tools/extract-utils python3 extract-files.py "$@"
EOF

# 设置执行权限
chmod +x run_extract.sh

# 执行包装脚本
./run_extract.sh

上述脚本执行需要python 3.9以上的环境,在老版本Ubuntu中可能需要手动编译

或者如果是在wsl或者vps中,不方便连接设备或者设备没有root,可以先手动挂在并提取ventor.img中的文件至驱动目录,即

# 释放payload.bin

# 下载payload-dumper-go
wget https://github.com/ssut/payload-dumper-go/releases/download/1.3.0/payload-dumper-go_1.3.0_linux_amd64.tar.gz
tar -xvf payload-dumper-go_1.2.2_linux_amd64.tar.gz

rm README.md LICENSE
chmod +x payload-dumper-go

# 释放
./payload-dumper-go /path/to/payload.bin

# 挂载
mkdir -p ~/vendor_mount
sudo mount -o loop,ro vendor.img ~/vendor_mount

# 复制
cd [lineageos源码根目录]
mkdir -p vendor/google/corcal
sudo cp -r ~/vendor_mount/* vendor/google/corcal

然后再执行setup-makefiles.py即可

当然为了方便和准确,还是建议使用./extract-files.py一键完成。因为这里使用了env -S,所以需要更新coreutils到8.30以上

更多的可以参考https://wiki.lineageos.org/extracting_blobs_from_zips

亦或者是直接通过仓库TheMuppets / proprietary_vendor_google_coral获得

设置环境

source build/envsetup.sh # 或 . build/envsetup.sh

设置编译目标

格式为lunch product_name-build_variant,例如lunch aosp_coral-userdebug

其中构建模式如下

构建类型 使用情况
user 权限受限;适用于生产环境
userdebug 与“user”类似,但具有 root 权限和调试功能;是进行调试时的首选编译类型
eng 具有额外调试工具的开发配置

不同设备的构建目标见刷写设备 | Android 开源项目 | Android Open Source Project

对于LineageOS来说,是breakfast coral

通过repo管理依赖

对于需要某些其他依赖的情况,例如上面需要的设备驱动以及内核,需要手动添加,可以在.repo\local_manifests目录下编译xml来实现。

例如如果添加一个谷歌的内核仓库,则就是

<project path="kernel/google/msm-4.14" remote="github" name="LineageOS/android_kernel_google_msm-4.14" />

这里需要主义的是,如果使用了镜像源,例如清华,这里的github会被替换成清华的镜像仓库,但是某些内容其是没有的,这是就需要手动指定远程仓库,如下添加了一个GitHub上分享的设备驱动

<remote name="the-muppets" fetch="https://github.com/" />
  <project name="TheMuppets/proprietary_vendor_google_coral" path="vendor/google/coral" remote="the-muppets" groups="muppets,muppets_coral" clone-depth="1" revision="lineage-22.1" />

这时再repo sync即可。

或者bu’ti不添加revision="lineage-22.1"标签,使其版本使用主仓库的版本

构建

执行m即可。可以使用m -jN来指定线程,如果不指定则会默认选择最优的线程来进行编译。

报错解决

prebuilts/clang/host/linux-x86/clang-4053586/bin/clang++.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

apt install libncurses5

File build/tools/merge-event-log-tags.py, line 51
except getopt.GetoptError, err:
^
SyntaxError: invalid syntax

python版本问题,应当用python2,而不是python3(同步源码却要用python3.。。)

删掉原先的软连接,安装python2,创建python2的软连接


ln -s /bin/pyton2 /bin/python

SSL error when connecting to the Jack server. Try \'jack-diagnose\'

将 \$HOME/.jack-settings和 \$HOME/.jack-server/config.properties下面的两个端口改为其他两个就行,注意两个文件要相同


flex-2.5.39: loadlocale.c:130:_nl_intern_locale_data: ?? ‘cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))’ ???
Aborted (core dumped)

在~/.bashrc最后添加export LC_ALL=C
然后 source ~/.bashrc

添加export LC_ALL=C是去除本地C化,使得Android的编译工具与本地工具不冲突


Out of memory error (version 1.3-rc7 \'Douarn\' (445000 d7be3910514558d6715ce455ce0861ae2f56925a ……

内存不够了,增加内存,增加的大小根据编译环境来

export JACK_SERVER_VM_ARGUMENTS= -Xmx6g

添加KernelSU

这里其实主要是针对非GKI内核来说的,GKI内核完全没有必要通过从源代码编译的方式来构建,直接刷官方编译好的gki就完事了。

根据KernelSU的说明,只需要在内核原名代码目录,执行curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash - 即可。(对于非GKI设备,目前的支持停留在了0.95版本,也就是curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -s v0.9.5

对于KernelSU Next来说也是一样,其对非GKI内核一直都有支持,也是类似的操作,在内核源代码目录执行curl -LSs "https://raw.githubusercontent.com/rifsxd/KernelSU-Next/next/kernel/setup.sh" | bash -

然后再执行常规的编译操作即可。

如果只编译内核,具体来说

export ARCH=arm64
export SUBARCH=arm
export O=out # 设定架构和输出目录

cd kernel/google/msm-4.14/ # 其他设备可能不一样

make defconfig floral_defconfig

make menuconfig

make savedefconfig

cp defconfig arch/arm64/configs/floral_defconfig

make mrproper # 清理

需要注意的是,shell脚本执行完毕,操作执行完毕,需要通过git提交以下,即

git add .
git commit -m "Add KernelSU support"

最后使用mka bootimage 即可编译出直接可以使用的内核。

如果想要编译kernelsu后编译kernelsu next或者其他的,只需要执行curl -LSs "https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh" | bash -s -- --cleanup进行清理即可

编译strace

在源代码根目录下mmma -j6 external/strace即可

一些调整

修改连接验证服务器

有两种方法,可以通过烧写之后,使用adb修改,如下,修改成小米的

adb shell settings put global captive_portal_https_url https://connect.rom.miui.com/generate_204
adb shell settings put global captive_portal_http_url http://connect.rom.miui.com/generate_204

也可以通过源代码修改,packages/modules/NetworkStack/res/values/config.xml同样修改为小米的

<!-- HTTP URL for network validation, to use for detecting captive portals. -->
<string name="default_captive_portal_http_url" translatable="false">https://connect.rom.miui.com/generate_204</string>

<!-- HTTPS URL for network validation, to use for confirming internet connectivity. -->
<string name="default_captive_portal_https_url" translatable="false">https://connect.rom.miui.com/generate_204</string>

<!-- List of fallback URLs to use for detecting captive portals. -->
<string-array name="default_captive_portal_fallback_urls" translatable="false">
    <item>https://connect.rom.miui.com/generate_204</item>
    <item>https://connect.rom.miui.com/generate_204</item>
</string-array>

修改授时服务

也是可以刷写后通过adb修改,这里改为中科院国家授时中心的NTP

adb shell settings put global ntp_server ntp.ntsc.ac.cn

或者在源代码中修改,在device/vendor/device/gps/etc/gps.conf(vendor和device是对应的厂家和设备)

#NTP server
NTP_SERVER=ntp.ntsc.ac.cn

后记

  1. 最后吐槽一下网上的某些文档,执行的操作东西乱七八糟不明不白,同步源码必须先更新系统?编译aosp需要htop?离谱,多看官方文档,深爱生命,远离垃圾文档

  2. 不要原封不动的复制网上的命令,研究一下是干嘛的,然后根据需要或者实际情况修改

  3. 修好了问题可能需要clean后再make

记录一下这个困扰了我多年的问题,重打包boot.img,修改其中的default.prop,开启之后似乎没有任何变化。

除此之外还应该修改system.img中的build.prop中的对应字段。system.img后挂载会覆盖上述设定。修改后刷入system.img就能实现正常的效果了。刷入system.img不会影响任何现有数据。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇