AOSP 折腾日记

2,356 阅读12分钟

闲叙

自从上一次在双系统 Kali 上下载编译 AOSP 成功却倒在了 AndroidStudio 的导入的路上后,心里一直对此念念不忘,如鲠在喉,毕竟错误信息提示说只是倒在了 JDK 的版本上(提示说要 JDK 11 的版本,本地装的是 JDK 1.8),虽然我换了对应的 JDK 版本也无济于事( ╯□╰ )。刚好近日有些空闲,便借此机会折腾了一番,折腾了两天,试了 WSLUbuntu 虚拟机等方案,这里记录一下整个过程及遇到一些问题吧(最终生产使用还是双系统 Kali 通过屏蔽系统 Java 转而使用 AOSP 自带的 Java 环境即可编译成功)。

通用 Debian 系统下载编译步骤

切换到 root 用户

这一步是为了避免因系统而导致的用户权限问题,例如 Kali 很多命令的执行需要 root 权限;

su

然后输入密码,如果没有设置果 root 密码,则执行

sudo passwd root

键入密码,当然,把当前用户加入 root 用户组也不是不可以,这些 Linux 基本命令就不再复述了。

下载 repo 文件并配置 repo 环境

这里 repo 文件是 repo 命令执行的基础,我们需要将其下载下来并添加到环境变量里,以方便我们使用;由于墙的存在,谷歌官方的一些资源我们访问不到,这里我们选择使用国内清华的源;

清华大学 AOSP 镜像站:mirrors.tuna.tsinghua.edu.cn/help/AOSP/
中科大 AOSP 镜像站:mirrors.ustc.edu.cn/help/aosp.h…

// 安装 curl 工具
apt install curl
// 安装 git
apt install git
// 进入 root 用户目录,要是想换其他用户目录,自行切换即可 cd /home/XXX
cd ~
// 创建 repo 所在目录
mkdir bin
// 下载 repo
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o repo
// 赋予执行权限
chmod +x repo
// 将当前目录添加至 PATH ,要是嫌麻烦可以直接添加到 .bashrc 文件中
PATH=~/bin:$PATH
// repo的运行过程中会尝试访问官方的git源更新自己,这里使用tuna的镜像源进行更新
// 编辑 ~/.bashrc 并在文件末尾添加如下内容然后 source ~/.bashrc
// export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'

创建 AOSP 工作目录并同步源码

// 回到用户目录 
cd ~
// 创建工作目录,当然你想放哪儿都行
mkdir AOSP
// 进入工作目录
cd AOSP
// 初始化仓库,你也可以选择同步对应的版本
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest
// 开始同步
repo sync

安装必要依赖

sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g+±multilib\
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386\
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386\
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev\
sudo apt-get install git-core gnupg flex bison gperf build-essential\
sudo apt-get install zip curl zlib1g-dev gcc-multilib g+±multilib\
sudo apt-get install libc6-dev-i386\
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev\
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4\
sudo apt-get install lib32z-dev ccache\
sudo apt-get install libssl-dev libncurses5
sudo apt install libncurses5

编译代码

这里不细讲各个命令的含义,如有需要可以查看官网或者其他资料进行了解

cd ~/AOSP
// 设置编译所需环境变量
. build/envsteup.sh
// 查看哪些版本可以编译,这里根据自己的情况选择
lunch
// 开始编译,最多使用 16 核心
make -j16

参考编译耗时

由于使用的虚拟机且资源没有配满,还使用的是机械硬盘,所以编译耗时5小时8分,使用的配置如下

PC 配置
    CPU AMD 1700 
    内存 32G
    磁盘 希捷酷鱼3T

Hyper-V 虚拟机设置
    系统 Ubuntu 21.10
    CPU 14核心
    内存 动态内存最大 20G
    磁盘 1T 

image.png

AndroidStudio 导入系统源码

生成导入文件

// 编译模块
mmm development/tools/idegen

成功后如下

root@kaithmy-Virtual-Machine:/home/kaithmy/AOSP# mmm development/tools/idegen/
00:47:44 ************************************************************
00:47:44 You are building on a machine with 5.53GB of RAM
00:47:44
00:47:44 The minimum required amount of free memory is around 16GB,
00:47:44 and even with that, some configurations may not work.
00:47:44
00:47:44 If you run into segfaults or other errors, try reducing your
00:47:44 -j value.
00:47:44 ************************************************************
00:47:46 Build sandboxing disabled due to nsjail error.
build/make/core/soong_config.mk:198: warning: BOARD_PLAT_PUBLIC_SEPOLICY_DIR has been deprecated. Use SYSTEM_EXT_PUBLIC_SEPOLICY_DIRS instead.
build/make/core/soong_config.mk:199: warning: BOARD_PLAT_PRIVATE_SEPOLICY_DIR has been deprecated. Use SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS instead.
============================================
PLATFORM_VERSION_CODENAME=Tiramisu
PLATFORM_VERSION=Tiramisu
TARGET_PRODUCT=aosp_x86_64
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=x86_64
TARGET_ARCH_VARIANT=x86_64
TARGET_2ND_ARCH=x86
TARGET_2ND_ARCH_VARIANT=x86_64
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-5.13.0-22-generic-x86_64-Ubuntu-21.10
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=AOSP.MASTER
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=device/generic/goldfish device/generic/goldfish-opengl hardware/google/camera hardware/google/camera/devices/EmulatedCamera
============================================
[100% 7/7] Install out/host/linux-x86/framework/idegen.jar

#### build completed successfully (07:48 (mm:ss)) ####

成功后的目录下有以下文件

root@kaithmy-Virtual-Machine:/home/kaithmy/AOSP# ls development/tools/idegen/
Android.bp  excluded-paths  idegen.iml  idegen.ipr  idegen.sh  index-gen.sh  intellij-gen.sh  README  src  templates

回到 AOSP 目录,执行

// 运行 idegen.sh 脚本
. development/tools/idegen/idegen.sh 

此时在 AOSP 目录下会多出来 android.ipr 文件,这是导入源码的关键

# . development/tools/idegen/idegen.sh 
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
Read excludes: 21ms
Traversed tree: 25996ms

加大 AndroidStudio 的内存

修改 .vmoptions,例如:

// 按需修改
-Xmx10240m
-Xms4096m

AndroidStudio 打开 android.ipr

打开 android.ipr 即可导入源码工程,有可能会提示以下错误

The directory XXX(项目名) is registered as a Git root, but no Git repositories were found there. 直接到 File -> Setting -> Version Control 中移除不存在的项目即可,例如我遇到的是 /home,很明显是不存在的 git 项目

鸡肋 WSL

为啥说 WSL 是鸡肋呢?因为 WSL 的体验和真实的 Linux 体验在某些关键问题上差的太多了,不适合我们这些 Android 开发者,尽管它能满足日常 90% 的需求,但恰恰是另外的 10% 让它成为了不得不弃之的鸡肋。首先是代码同步问题,体现在以下几点,第一:在进行 repo sync 时,如果要 sync 指定版本的代码,必定有几个模块的代码是 checkout 失败的,具体版本以及一些错误信息一会儿讲;第二:是文件路径大小写敏感的问题,默认 WSL 是大小写不敏感的,而编译时是需要大小写敏感的,这需要前期创建工程文件夹的时候就配置好,如果是创建工程文件夹之后才配置的,那么很不幸,拉取与编译代码时你依然会碰到不少问题;然后是 PowerShellWSL 的回显问题,经常会出现 bash 内容不回显到 PowerShell 中以及回显很慢的情况,导致经常会以为 bash 命令没有执行完而在一旁焦躁不安;再然后是 bash 命令执行效率低下,一个 . build/envsteup.sh 命令跑满磁盘和大半的 CPU 花了近 5 分钟的时间才执行完,lunch 命令也是一样,这就很让人头疼,就是虚拟机再慢也在 5s 内能执行完 . build/envsteup.sh,而且 WSL 不是还号称比虚拟机性能更强嘛?以下是具体的一些步骤

启用 WSL

这个比较简单,到微软应用商店搜索 WSL 或者 Ubuntu 安装即可

设置 WSL 资源配置

一般用来控制 WSL 占用的资源大小,这里需要在 用户目录下创建 .wslconfig 文件并配置,参考如下:

[wsl2]
processors=16
memory=32GB
swap=16GB

设置文件夹大小写敏感

打开 PowerShell 输入 bash 进入 WSL,输入

// 查看路径信息
mount

查看 case 相关状态,也可以输入 mount | grep case 来查看,如果没有那么默认是 case=off,大小写不敏感的,case 选项说明如下

case=dircase=offcase=force
Flag为enable的目录Case SensitiveCase SensitiveCase Sensitive
Flag为disable的目录Case InsensitiveCase InsensitiveCase Sensitive
wsl中创建目录的FlagEnabledDisabledEnabled

可以通过修改或者创建 etc/wsl.conf 来配置它

[automount]
options = "case=dir"

然后重启 wsl 即可,参考命令(记得用管理员身份)

// 停止 wsl 服务
net stop LxssManager
// 启动 wsl 服务
net start LxssManager

如果要启用 case=force 则需要在 CMDPowerShell 中执行

 reg.exe add HKLM\SYSTEM\CurrentControlSet\Services\lxss /v DrvFsAllowForceCaseSensitivity /t REG_DWORD /d 1

image.png

image.png

基础步骤

参考以上 通用 Debian 系统下载编译步骤

错误信息一览

root@DESKTOP-EKL3BCN:/mnt/d/AOSP# repo sync
Fetching: 100% (1052/1052), done in 39m39.075s
Garbage collecting: 100% (1052/1052), done in 1m1.527s
info: A new version of repo is available
warning: repo is not tracking a remote branch, so it will not receive updates
info: Restarting repo with latest version
Fetching: 100% (1051/1051), done in 30m41.590s
Garbage collecting: 95% [4 jobs] (1008/1051) finished platform/prebuilts/gcc/darwin-x86/aarch64/aarch64-linux-android-4.Garbage collecting: 100% (1051/1051), done in 40.764s
error: Cannot link file build/bazel/bazel.BUILD to /mnt/d/AOSP/BUILD
fatal: Unable to create '/mnt/d/AOSP/cts/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: cts/: platform/cts checkout 41d9f299c73b5af190ebfb5d19c48a27c10cc371
error: Cannot checkout platform/cts
fatal: Unable to create '/mnt/d/AOSP/external/apache-http/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: external/apache-http/: platform/external/apache-http checkout e0456b661323d7c463e8d2aca7349716506dbf7d
error: Cannot checkout platform/external/apache-http
fatal: Unable to create '/mnt/d/AOSP/device/google/coral-sepolicy/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: device/google/coral-sepolicy/: device/google/coral-sepolicy checkout 62e763db9e8e79c201946660653feab3fec3ed33
error: Cannot checkout device/google/coral-sepolicy
fatal: Unable to create '/mnt/d/AOSP/external/ImageMagick/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: external/ImageMagick/: platform/external/ImageMagick checkout 60105dc3c4d636694f8c9a7efb39e28507cef107
error: Cannot checkout platform/external/ImageMagick
Checking out: 15% (160/1051) platform/external/google-fonts/arbutus-slabfatal: Unable to create '/mnt/d/AOSP/external/jcommander/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: external/jcommander/: platform/external/jcommander checkout 83574919c917acc984c225f68f4dcf2a9faff1d6
error: Cannot checkout platform/external/jcommander
Checking out: 19% (202/1051) platform/external/cros/system_apifatal: Unable to create '/mnt/d/AOSP/external/mockftpserver/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: external/mockftpserver/: platform/external/mockftpserver checkout bd7824e19c697a07d694ba6acdc1be19f138e51e
error: Cannot checkout platform/external/mockftpserver
Checking out: 21% (224/1051) platform/external/easymockfatal: Unable to create '/mnt/d/AOSP/external/libtextclassifier/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: external/libtextclassifier/: platform/external/libtextclassifier checkout 9b94a0c59dd0102f67def5e732ce535ad2ee35ca
error: Cannot checkout platform/external/libtextclassifier
Checking out: 27% (288/1051) platform/external/libnetfilter_conntrackfatal: Unable to create '/mnt/d/AOSP/external/perfetto/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: external/perfetto/: platform/external/perfetto checkout fca2f3ae3cb81e3ece194ce2a089bb5c6c4a7d7c
error: Cannot checkout platform/external/perfetto
Checking out: 100% (1051/1051), done in 27m30.281s

error: Unable to fully sync the tree.
error: Checking out local projects failed.
Failing repos:
external/ImageMagick
device/google/coral-sepolicy
cts
external/apache-http
external/jcommander
external/mockftpserver
external/perfetto
external/libtextclassifier
Try re-running with "-j1 --fail-fast" to exit at the first error.
root@DESKTOP-EKL3BCN:/mnt/d/AOSP#

仅从错误信息看好像删了 xxx.lock 文件就可以,但是当删了所有出错的 xxx.lock 文件从新 sync 时还是会有模块出现这样的错误信息,这里搜索了一下,说是文件路径大小写的问题,后面也配置了文件路径大小写还是会出现这样的问题,估计是文件夹创建时没有配置的原因吧,为了避免重新 sync 的麻烦,就直接转变方案为虚拟机了,舍弃了从 Windows 上直接导入源码的便利性还是挺可惜的,看个人取舍吧。

SMB: www.jianshu.com/p/39bbc3631…

Hyper-V 虚拟机

虚拟机是除了双系统外的一个不错的选择,一切都很接近物理机,几乎没有啥兼容性的问题

基础步骤

参考以上 通用 Debian 系统下载编译步骤

宿主与虚拟机互通

有些依赖安装命令手打很麻烦,能够粘贴最好,都说使用 linux-vm-tools 这里我没有试成功过,这里贴个参考连接,这里我使用的 ssh 方案,PowerShell 自带 ssh 客户端,而 Ubuntu 则需要给他安装,通过命令 sudo apt install openssh-server 安装,之后通过 PowerShellssh 连接即可,这样可以直接通过 ssh 安装依赖。

结语

想了想,想说的有点多,就先不说了吧,不过有一点必须得说,那就是:CSDN 真恶心!未登录都不让复制了,各种恶心用户的操作,怪不得用户都流向其他平台了,不尊重用户用户是会用脚投票的!给大家个 Tips,可以在手机浏览器中进行复制后通过其他软件如 QQ 发送至电脑后使用;

参考

  1. blog.csdn.net/learnframew…
  2. blog.csdn.net/ding1145536…
  3. blog.csdn.net/gs344937933…
  4. www.cnblogs.com/caoxinyu/p/…
  5. blog.csdn.net/qq_23327993…
  6. 权限 www.sohu.com/a/315996924…
  7. blog.csdn.net/qq_37207639…
  8. 多版本控制 zhuanlan.zhihu.com/p/20797436
  9. 多版本控制www.jianshu.com/p/7315f8b10… todo : sudo update-alternatives --set java /usr/local/jdk1.8.0_281/bin/java sudo update-alternatives --set javac /usr/local/jdk1.8.0_281/bin/javac sudo update-alternatives --set javaws /usr/local/jdk1.8.0_281/bin/javaws