首页
社区
课程
招聘
Android 源码编译
2023-5-3 22:18 6615

Android 源码编译

2023-5-3 22:18
6615

环境配置

环境 配置
VMware 16.1.1 build-17801498
Ubuntu Ubuntu 20.04.2 LTS
物理机内存 32G
虚拟机内存 16G
物理机储存 2T
虚拟机内存 1T
目标机型 Pixel 4
目标版本 Android 10
 

备注:内存建议最少16G,否则大概率编译失败,物理储存建议预留600G,笔者这边编译后就 300+,随便几个快照后就 500+

配置基本同步包

下载源码

1
2
3
mkdir ~/bin
cd ~/bin
wget https://mirrors.ustc.edu.cn/aosp-monthly/aosp-latest.tar

计算源码md5

1
md5sum aosp-latest.tar

访问网址检查源码md5

1
https://mirrors.ustc.edu.cn/aosp-monthly/aosp-latest.tar.md5

解压源码

1
tar -xvf aosp-latest.tar

配置 git

安装git

1
sudo apt-get install git

配置 git 邮箱

1
git config --global user.email 211527456@qq.com

配置 git 用户名

1
git config --global user.name "lxz"

配置 repo

把环境变量添加到 bashrc 中,在 linux 系统中,如果希望命令永久生效就需要写到 bashrc 中,因为每次重启系统会自动执行 bashrc 中的命令

1
echo "PATH=~/bin:\$PATH" >> ~/.bashrc

如果我们不想重启系统,可以手动执行一下 bashrc

1
source ~/.bashrc

安装 curl

1
sudo apt-get install curl

将 repo 下载到 ~/bin/repo

1
curl -sSL 'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo

设置可执行权限

1
chmod a+x ~/bin/repo

导出一个临时环境变量,该环境变量关闭 shell 即失效

1
export REPO_URL='https://gerrit-googlesource.proxy.ustclug.org/git-repo'

也可以直接设置个永久的

1
2
echo "REPO_URL='https://gerrit-googlesource.proxy.ustclug.org/git-repo'" >> ~/.bashrc
source ~/.bashrc

配置 python

1
sudo ln -s /usr/bin/python3.8 /usr/bin/python

执行 repo init,这遇到个大坑,建议拉取 android-10.0.0_r9 版本,之前拉取 r33 遇到一大堆问题

1
2
cd ~/bin/aosp/
repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-10.0.0_r9

开始同步

1
repo sync

遇到报错

1
2
3
error: Unable to fully sync the tree.
error: Downloading network changes failed.
Try re-running with "-j1 --fail-fast" to exit at the first error.
1
repo sync -j1 --fail-fast

安装 JDK

命令如下

1
2
3
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk

安装其他依赖

命令如下

1
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig libncurses5

执行驱动脚本

下载对应的驱动程序,相关链接在下方

1
2
https://source.android.com/docs/setup/about/build-numbers?hl=zh_cn
https://developers.google.com/android/drivers?hl=zh-cn#flameqq1c.191205.016.a1

拷贝下载的驱动文件到 ~/bin/aosp 目录下并解压

1
2
tar -zxvf qcom-flame-qd1a.190821.011.c4-6f7c02a5.tgz
tar -zxvf google_devices-flame-qd1a.190821.011.c4-bd22abf2.tgz

执行解压出来的两个脚本, 需要注意到是回车到 8.e 左右,需要按照提示输入 I ACCEPT

1
./extract-qcom-flame.sh
1
./extract-google_devices-flame.sh

查看内核是否存在,源码下载完后最好看下里面有没有你手机对应的设备内核如果没有,需要另外下载或者编译,内核所在路径可以在下方链接的文档中找到对应表

1
https://source.android.google.cn/setup/build/building-kernels#id-version

配置 jvm 和 wasp

建议修改jvm和wasp的默认存储空间,否则编译时容易遇到报错

1
2
3
4
5
FAILED: [W][2023-04-15T09:03:20-0700][75058] void cmdline::logParams(nsjconf_t *)():250 Process will be UID/EUID=0 in the global user namespace, and will have user root-level access to files
[W][2023-04-15T09:03:20-0700][75058] void cmdline::logParams(nsjconf_t *)():260 Process will be GID/EGID=0 in the global user namespace, and will have group root-level access to files
11:34:07 ninja failed with: exit status 137
 
#### failed to build some targets (02:31:58 (hh:mm:ss)) ####

配置 jvm

解决方案为打开 build/soong/java/config/config.go 文件,将 JavacHeapSize 改为虚拟机内存一半的大小

1
sudo gedit ~/bin/abuild/soong/java/config/config.go
1
2
3
//2048修改为4096关闭当前编译Terminal窗口,
//另新建一个Terminal窗口再编译即可,不需要删除out再编译
pctx.StaticVariable("JavacHeapSize", "2048M")

配置 swap

查看当前 swap 空间

1
free -h
1
2
3
              total        used        free      shared  buff/cache   available
Mem:           15Gi       1.0Gi        13Gi       2.0Mi       1.2Gi        14Gi
Swap:         2.0Gi          0B       2.0Gi

关闭 swap 空间

1
sudo swapoff /swapfile

调整 swapfile 到 8GB

1
sudo fallocate -l 8G /swapfile

设置 swapfile 为 swap 空间

1
2
sudo chmod 600 /swapfile
sudo mkswap /swapfile

启动 swap 空间

1
sudo swapon /swapfile

查看当前 swap 空间是否为 8G

1
free -h
1
2
3
              total        used        free      shared  buff/cache   available
Mem:           15Gi       1.0Gi        13Gi       2.0Mi       1.2Gi        14Gi
Swap:         8.0Gi          0B       8.0Gi

编译源码

清除之前编译的数据,在同一份源码中可以编译不同的机型,在编译不同机型是需要执行这个命令,编译相同机型则可以不执行此命令

1
make clobber

导出环境变量

1
source build/envsetup.sh

在导出环境变量后我们可以使用 lunch 命令查看当前可编译的选项

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
root@ubuntu:~/bin/aosp# lunch
 
You're building on Linux
 
Lunch menu... pick a combo:
[W][2023-04-15T08:15:15-0700][12673] void cmdline::logParams(nsjconf_t *)():250 Process will be UID/EUID=0 in the global user namespace, and will have user root-level access to files
[W][2023-04-15T08:15:15-0700][12673] void cmdline::logParams(nsjconf_t *)():260 Process will be GID/EGID=0 in the global user namespace, and will have group root-level access to files
     1. aosp_arm-eng
     2. aosp_arm64-eng
     3. aosp_blueline-userdebug
     4. aosp_bonito-userdebug
     5. aosp_car_arm-userdebug
     6. aosp_car_arm64-userdebug
     7. aosp_car_x86-userdebug
     8. aosp_car_x86_64-userdebug
     9. aosp_cf_arm64_phone-userdebug
     10. aosp_cf_x86_64_phone-userdebug
     11. aosp_cf_x86_auto-userdebug
     12. aosp_cf_x86_phone-userdebug
     13. aosp_cf_x86_tv-userdebug
     14. aosp_coral-userdebug
     15. aosp_crosshatch-userdebug
     16. aosp_flame-userdebug
     17. aosp_marlin-userdebug
     18. aosp_sailfish-userdebug
     19. aosp_sargo-userdebug
     20. aosp_taimen-userdebug
     21. aosp_walleye-userdebug
     22. aosp_walleye_test-userdebug
     23. aosp_x86-eng
     24. aosp_x86_64-eng
     25. beagle_x15-userdebug
     26. car_x86_64-userdebug
     27. fuchsia_arm64-eng
     28. fuchsia_x86_64-eng
     29. hikey-userdebug
     30. hikey64_only-userdebug
     31. hikey960-userdebug
     32. hikey960_tv-userdebug
     33. hikey_tv-userdebug
     34. m_e_arm-userdebug
     35. mini_emulator_arm64-userdebug
     36. mini_emulator_x86-userdebug
     37. mini_emulator_x86_64-userdebug
     38. poplar-eng
     39. poplar-user
     40. poplar-userdebug
     41. qemu_trusty_arm64-userdebug
     42. uml-userdebug
 
Which would you like? [aosp_arm-eng]

此时我们发现没有 eng 的版本,所以这里还需要手动修改配置文件,路径为 /aosp/device/google/coral/AndroidProducts.mk,注意这里的 flame 是谷歌手机的甜品标识,可能不是你所使用手机的标识,因为一个映像是可以对应多个手机的,所以得多找找,这里添加两个选项

1
2
3
4
5
COMMON_LUNCH_CHOICES := \
    aosp_coral-userdebug \
    aosp_flame-userdebug \
    aosp_flame-eng \
    aosp_flame-user \

回到 aosp 目录,重新导出环境变量

1
source build/envsetup.sh

再次执行 lunch 命令,可以发现

1
make -j16
1
2
09:01:59 ninja failed with: exit status 1
make: *** [build/make/core/main.mk:21: run_soong_ui] Error 1
1
export LC_ALL=C

刷机

linux 下刷机

安装adb

1
2
3
sudo su
apt-get install android-tools-adb
apt install fastboot

ununtu 中的 platform-tools 版本比较低,此时刷机会报错,所以此时我们需要更新 platform-tools,具体方法是到下载最新的 platform-tools,解压到共享文件夹中(我这里是共享文件夹,也可以从别的地方拷贝),然后进入ubuntu 进入管理员模式执行下方命令,将 platform-tools 拷贝到 /usr/lib/android-sdk 目录下即可

1
cp -r /mnt/hgfs/share/platform-tools /usr/lib/android-sdk

设置临时环境变量

1
export ANDROID_PRODUCT_OUT=/home/lxz/bin/aosp/out/target/product/flame

也可以把这个环境变量直接设置成永久的

1
2
echo "ANDROID_PRODUCT_OUT=/home/lxz/bin/aosp/out/target/product/flame" >> ~/.bashrc
source ~/.bashrc

查看下是否设置成功

1
echo $ANDROID_PRODUCT_OUT

刷机

1
2
adb reboot bootloader
fastboot flashall -w

windows下刷机

  1. 将桌面添加到环境变量

    变量名:ANDROID_PRODUCT_OUT

    变量值:C:\Users\Administrator\Desktop

  2. 将 ununtu /home/lxz/bin/aosp/out/target/product/flame 目录中 android.info.txt 以同目录中的所有 .img 文件全部拷贝到 windows 桌面,这里说一下,我的这个目录里当时没有 vendor.img,我当时有执行驱动脚本,按理说应该有的,所以我手动将 /home/lxz/bin/aosp/vendor/google_devices/coral/proprietary 中的 vendor.img 拷贝了出来,还有同目录下的 radio.img 和 bootloader.img 也一起拷贝出来,这俩好像是用不到,但先拷贝再说

    这里在后续的测试发现 android-10.0.0_r33 才会出现上述的问题,之后在拉取 android-10.0.0_r9 时并没有上述问题,应该是版本的问题

  3. 使手机进入 fastboot 模式

    1
    adb reboot bootloader
  4. 开始刷机

    1
    fastboot flashall -w

[培训]《安卓高级研修班(网课)》月薪三万计划,掌 握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

最后于 2023-5-4 10:08 被简单的简单编辑 ,原因:
收藏
点赞4
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回