安装虚拟机

选用:OrbStack · Fast, light, simple Docker & Linux虚拟机

安装后,启动OrbStack,macOS系统选用的amd64的Linux系统,安装完成后设置内存为32G以上否则可能会报错。

配置好后,启动虚拟机,然后开始准备下载源码。

源码下载

使用清华镜像,一定要在虚拟机下面下载,在mac下直接下载,会报错。

清华镜像:Aosp | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

根据里面的教程下载AOSP源码,我下载的是

1
2
repo init -u http://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b  android16-qpr2-release
repo sync -c -j10

编译步骤

代码下载后进入代码根目录,

执行:

1
source build/envsetup.sh

然后执行:

1
lunch --help

显示如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
The lunch command selects the configuration to use for subsequent
Android builds.

Usage: lunch TARGET_PRODUCT [TARGET_RELEASE [TARGET_BUILD_VARIANT]]

Choose the product, release and variant to use. If not
supplied, TARGET_RELEASE will be 'trunk_staging' and
TARGET_BUILD_VARIANT will be 'eng'


Usage: lunch TARGET_PRODUCT-TARGET_RELEASE-TARGET_BUILD_VARIANT

Chose the product, release and variant to use. This
legacy format is maintained for compatibility.


Note that the previous interactive menu and list of hard-coded
list of curated targets has been removed. If you would like the
list of products, release configs for a particular product, or
variants, run the following as individual commands:
list_products, list_releases, or list_variants
respectively.

这里有三个命令:

1
list_products, list_releases, or list_variants

最终需要执行的格式是:

1
lunch TARGET_PRODUCT-TARGET_RELEASE-TARGET_BUILD_VARIANT

但是需要先执行上面三个命令获取对应信息,

执行:

1
list_products

得到:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
aosp_64bitonly_x86_64
aosp_arm
aosp_arm64
aosp_arm64_fullmte
aosp_arm64_plus_armv7
aosp_cf_arm64_auto
aosp_cf_arm64_minidroid
aosp_cf_arm64_only_phone
aosp_cf_arm64_only_phone_hwasan
aosp_cf_arm64_only_phone_vendor
aosp_cf_arm64_phone
aosp_cf_arm64_phone_fullmte
aosp_cf_arm64_phone_hwasan
aosp_cf_arm64_phone_pgagnostic
aosp_cf_arm64_phone_vendor
aosp_cf_arm64_slim
aosp_cf_arm_minidroid
aosp_cf_riscv64_minidroid
aosp_cf_riscv64_phone
aosp_cf_riscv64_slim
aosp_cf_riscv64_wear
aosp_cf_x86_64_auto
aosp_cf_x86_64_auto_dd
aosp_cf_x86_64_auto_dewd
aosp_cf_x86_64_auto_md
aosp_cf_x86_64_auto_mdnd
aosp_cf_x86_64_auto_portrait
aosp_cf_x86_64_foldable
aosp_cf_x86_64_host
aosp_cf_x86_64_minidroid
aosp_cf_x86_64_only_phone
aosp_cf_x86_64_only_phone_hsum
aosp_cf_x86_64_pc
aosp_cf_x86_64_phone
aosp_cf_x86_64_phone_pgagnostic
aosp_cf_x86_64_phone_soong_system
aosp_cf_x86_64_phone_vendor
aosp_cf_x86_64_slim
aosp_cf_x86_64_ssi
aosp_cf_x86_64_tv
aosp_cf_x86_64_wear
aosp_cf_x86_go_phone
aosp_cf_x86_tv
aosp_cf_x86_wear
aosp_cheetah_car
aosp_husky_car
aosp_panther_car
aosp_riscv64
aosp_tangorpro_car
aosp_trout_arm64
aosp_trout_x86_64
aosp_tv_arm64
aosp_tv_x86
aosp_x86
aosp_x86_64
aosp_x86_arm
arm_krait
arm_v7_v8
armv8
armv8_cortex_a55
armv8_kryo385
csi_arm
csi_arm64
csi_x86
csi_x86_64
db845c
db845c_mini
full
full_x86
generic
generic_system_arm64
generic_system_x86
generic_system_x86_64
generic_system_x86_arm
generic_x86
gsi_arm
gsi_arm64
gsi_car_arm64
gsi_car_x86_64
gsi_tv_arm
gsi_tv_arm64
gsi_x86
gsi_x86_64
hikey
hikey32
hikey64_only
hikey960
hikey960_tv
hikey_tv
linaro_swr
linux_bionic
mainline_sdk
mainline_system_arm64
mainline_system_x86
mainline_system_x86_64
mainline_system_x86_arm
mgsi_arm
mgsi_arm64
mgsi_x86
mgsi_x86_64
mini_arm64
mini_armv7a_neon
mini_x86
mini_x86_64
module_arm
module_arm64
module_arm64only
module_riscv64
module_x86
module_x86_64
module_x86_64only
ndk
qemu_trusty_arm64
riscv64
sdk
sdk_atv64_arm64
sdk_atv_x86
sdk_car_arm64
sdk_car_cw_x86_64
sdk_car_dewd_x86_64
sdk_car_md_arm64
sdk_car_md_x86_64
sdk_car_x86_64
sdk_phone16k_arm64
sdk_phone16k_x86_64
sdk_phone64_arm64
sdk_phone64_arm64_minigbm
sdk_phone64_arm64_riscv64
sdk_phone64_x86_64
sdk_phone64_x86_64_minigbm
sdk_phone64_x86_64_riscv64
sdk_slim_arm64
sdk_slim_x86_64
sdk_tablet_arm64
sdk_tablet_x86_64
sdk_with_runtime_apis
silvermont
sm8x50
trout_arm64_ds
yukawa

选择:sdk_phone64_x86_64

然后执行:

1
2
// 手机:
export TARGET_PRODUCT=sdk_phone16k_x86_64

然后才能执行:

1
list_releases

得到:

1
2
3
4
5
6
7
8
9
aosp_current
ap2a
ap3a
ap4a
bp1a
bp2a
bp3a
bp4a
trunk_staging

选择:aosp_current

然后执行:

1
list_variants

得到:

1
2
3
user
userdebug
eng

选择userdebug

最终合成我们要的命令

1
2
3
lunch sdk_phone64_x86_64-aosp_current-userdebug
或者:
lunch sdk_tablet_x86_64-aosp_current-userdebug

这里二选一

最后执行(我的电脑是18核):

1
m -j16

开始编译。(先看下面信息)

报错

在编译过程中会有一些报错,使用AI查询就可以了,首先是内存不足,要设置模拟器内存为32G及以上,硬盘也要留出足够空间

1
2
3
4
5
6
7
[100% 280/280] analyzing Android.bp files and generating ninja file at out/soong/build.aosp_arm64.ninja
FAILED: out/soong/build.aosp_arm64.ninja
cd "$(dirname "out/host/linux-x86/bin/soong_build")" && BUILDER="$PWD/$(basename "out/host/linux-x86/bin/soong_build")" && cd / && env -i "$BUILDER" --top "$TOP" --soong_out "out/soong" --out "out" --soong_variables out/soong/soong.aosp_arm64.variables -o out/soong/build.aosp_arm64.ninja --kati_suffix -aosp_arm64 --kati_enabled -l out/.module_paths/Android.bp.list --available_env out/soong/soong.environment.available --used_env out/soong/soong.environment.used.aosp_arm64.build Android.bp
Killed
11:24:23 soong bootstrap failed with: exit status 1

#### failed to build some targets (02:08 (mm:ss)) ####

使用命令查看是否是内存问题:

1
dmesg -T | tail -200 | grep -i -E 'killed process|out of memory|oom'

内存不足

使用命令查看内存:

1
free -h

内存只有16G,不够,需要调大虚拟机内存

安装工具

在编译过程中还会出现缺少工具的报错,我安装了如下工具(虚拟机下安装),

  • unzip
  • zip
  • rsync

编译成功

没有截图,不过统计总耗时2个多小时完成编译。

参考