1. 下载 repo 工具
mkdir ~/bin
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o ~/bin/repo
chmod +x ~/bin/repo
repo 的运行过程中会尝试访问官方的 git 源更新自己,如果想使用 tuna 的镜像源进行更新,可以将如下内容复制到你的 ~/.bashrc 或者 ~/.zshrc 里。
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
PATH=~/bin:$PATH
然后 source 一下:
source ~/.bashrc
#如果使用的是 zsh
#source ~/.zshrc
2. 初始化仓库并同步远程代码
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
mkdir aosp
cd aosp
#初始化仓库,-b 指示分支,这里使用 android10
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-10.0.0_r41
#同步远程代码
repo sync
这里选用了 android-10.0.0_r41 版本用于学习。Android 每年都会更新一个大版本,学习的角度来说,选择一个不太老的版本即可,不必追新。
3. 编译源码
source build/envsetup.sh
lunch aosp_x86_64-eng
make -j16
4. 安装 emulator-installer
sudo apt install android-sdk
sdkmanager --install "emulator"
ps,这里可能会遇到连接的问题,最简单的解决方法就是去官网或者是镜像地址,下载对应的 emulator,然后再执行下一步 供参考的镜像地址:
腾讯:
https://mirrors.cloud.tencent.com/AndroidSDK/
阿里:
https://mirrors.aliyun.com/android.googlesource.com/
5. 配置环境变量
echo 'export PATH=$PATH:$ANDROID_SDK_ROOT/emulator:$ANDROID_SDK_ROOT/platform-tools' >> ~/.bashrc
source ~/.bashrc
6. 运行模拟器
emulator -verbose -cores 4 -show-kernel
ps:运行模拟器的时候遇到了两个关于python版本的问题,一个是
[100% 992/992] Create system-qemu.img now FAILED:
out/target/product/generic_x86_64/system-qemu.img /bin/bash -c "(export
SGDISK=out/host/linux-x86/bin/sgdisk SIMG2IMG=out/host/linux-x86/bin/simg2img;
device/generic/goldfish/tools/mk_combined_img.py -i
out/target/product/generic_x86_64/system-qemu-config.txt -o
out/target/product/generic_x86_64/system-qemu.img)" /bin/bash:
device/generic/goldfish/tools/mk_combined_img.py: /usr/bin/python: 错误的解释器: 没有那个文
件或目录 20:07:34 ninja failed with: exit status 1
这个错误很常见,意思是:
/usr/bin/python这个路径下没有 Python 解释器。
原因
mk_combined_img.py 的第一行(Shebang)是:
#!/usr/bin/python
而我的系统(Ubuntu 22.04)默认没有 /usr/bin/python,只有:
/usr/bin/python3
解决方法
创建软链接
在终端执行:
sudo ln -s /usr/bin/python3 /usr/bin/python
然后重新执行:
make -j16
👉 这样系统中 /usr/bin/python 就指向了 Python 3,后续 AOSP 构建也会正常。
随后,我的第一个问题解决了,第二个问题出现了:
[ 50% 1/2] Create system-qemu.img now FAILED: out/target/product/generic_x86_64/system-
qemu.img /bin/bash -c "(export SGDISK=out/host/linux-x86/bin/sgdisk
SIMG2IMG=out/host/linux-x86/bin/simg2img;
device/generic/goldfish/tools/mk_combined_img.py -i
out/target/product/generic_x86_64/system-qemu-config.txt -o
out/target/product/generic_x86_64/system-qemu.img)" File
"/home/lixiang/aosp/device/generic/goldfish/tools/mk_combined_img.py", line 48 print
"'%s' cannot be converted to int" % (line[2])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in
call to 'print'. Did you mean print(...)? [100% 2/2] build check-all-partition-sizes
The sum of sizes of [system vendor] is within BOARD_SUPER_PARTITION_SIZE:
1468014592+119820288 == 1587834880 <= 3229614080 == 3229614080 The sum of sizes of
[system vendor] is within BOARD_EMULATOR_DYNAMIC_PARTITIONS_SIZE: 1468014592+119820288
== 1587834880 <= 3221225472 == 3221225472 The sum of sizes of
[emulator_dynamic_partitions] is within BOARD_SUPER_PARTITION_SIZE:
问题原因
这个错误说明:
mk_combined_img.py是用 Python 2 语法写的,而你现在用的是 Python 3 在执行它。
例如这行:
print "'%s' cannot be converted to int" % (line[2])
是 Python 2 语法,在 Python 3 下就报错。
解决:用 Python 2 解释器执行
Ubuntu 默认不装 Python 2,可以通过下面命令安装:
sudo apt install python2
然后给系统加上 /usr/bin/python 链接到 Python 2:
sudo ln -sf /usr/bin/python2 /usr/bin/python
然后重新构建:
make -j16
✅ 适用于老版本 AOSP(Android 10 ~ 12)。
不会破坏系统其他功能