pixel XL AOSP源码编译

配置

  • 操作系统 :虚拟机 ubuntu 16.04
  • 内存 :12G
  • 硬盘 :150G
  • CPU :4核

编译过程

1. 源码分支获取

根据手机型号下载对应的版本,通过以下网站获取pixel XL android 10版本的源码标记 android-10.0.0_r17

1
https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds

2.下载android源码

repo

1
2
3
4
5
6
7
8
sudo apt-get install git-core
sudo apt-get install git-core curl
mkdir ~/bin/
cd ~/bin

curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo
gedit ~/.bashrc

在~/.bashrc里添加

1
2
#国内镜像
export REPO_URL='https://aosp.tuna.tsinghua.edu.cn/git-repo'

3. 同步代码

3.1 第一种方式

1
2
3
cd ~/work/sources/android-10.0.0_r17
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-10.0.0_r17
repo sync -c --no-tags --prune -f -j4 #线程根据cpu核数决定 一般为cpu核数x2,此处采用4线程

参数介绍:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-c, --current-branch fetch only current branch from server.
这个选项指定只获取执行 repo init 时 -b 选项所指定的分支,不会获取远端服务器的分支信息。
例如服务器上新增了其他分支,使用 -c 选项同步后,在本地 git 仓库执行 git branch -r 命令看不到服务器新增的分支名。如果不加 -c 选项,那么同步的时候,会打印 [new branch] 这样的信息,使用 git branch -r 命令可查看到服务器新增的分支。

–no-tags don’t fetch tags.
该选项指定不获取服务器上的tag信息。

–prune delete refs that no longer exist on the remote.
如果远端服务器已经删除了某个分支,在 repo sync 时加上 --prune 选项,可以让本地仓库删除对这个分支的跟踪引用。

-j JOBS, --jobs=JOBS projects to fetch simultaneously (default 2).
指定启用多少个线程来同步。
例如上面的 -j 4 指定用4个线程来同步。如果没有提供该选项,默认是用2个线程。

-f:即使某个项目同步失败,也继续同步其他项目。

总的来说,在 repo sync -c --no-tags --prune -j 4 命令中,使用 -c 和 --no-tags 选项可以减少需要同步的内容,从而减少要占用的本地代码空间,也可以减少一些同步时间。
使用 -j 选项来指定启用多线程进行同步,可以加快执行速度,也就减少了同步时间。
使用 --prune 选项去掉已删除分支的跟踪引用,一般不会用到,这个选项可加可不加。

3.1 第二种方式(未尝试此方法不知道是否可行)

下载源码的初始化包(加速源码下载) 大约60G

1
wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar

解压缩,解压缩完成之后会多出来~/aosp目录
切进去

1
tar zxvf aosp-latest.tar

cd aosp

1
2
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-10.0.0_r17
repo sync -c --no-tags --prune -f -j4 #线程根据cpu核数决定 一般为cpu核数x2,此处采用4线程

4. 驱动下载

1
https://developers.google.com/android/drivers#sailfishqp1a.190711.020

下载google_devices-sailfish-qp1a.191005.007.a3-a1615a0f.tgz

qcom-sailfish-qp1a.191005.007.a3-191228fe.tgz
两个压缩包,191005.007.a3为当时最新版本驱动

下载完成后解压会获得两个.sh脚本文件,运行两个脚本后会生成vendor文件夹,将vendor文件夹拷贝到源码根目录

5. 编译环境

1
sudo apt-get install git-core openjdk-8-jdk gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libxml2-utils xsltproc unzip

6. 编译脚本准备

进去源码根目录下

1
2
3
4
5
6
source ./build/envsetup.sh 
lunch aosp_sailfish-userdebug #也可以命令行lunch回车稍等片刻输入 然后输入15即可

#调整一个Java参数,要不然会出现Java OOM错误
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"

注: 我们可以在aosp/build/tools/buildinfo.sh vendor_buildinfo.sh 这两个文件中直接修改手机品牌的名字,厂家等信息

7. 开始编译

1
2
3
4
5
6
7
8
9
10
make -j6   #线程视配置而定

#编译的结果在 ~/aosp/out/target/product/marlin 文件夹中
#比较重要的几个是:

boot.img
recovery.img
ramdisk.img
system.img
userdata.img

8. 刷机

回到源码根目录下输入

1
fastboot flashall -w

注 lanch命令为我们设置好了ANDROID_PRODUCT_OUT变量的值为编译文件目录确保它的值和编译后镜像输出目录的路径值一致。如果不一致的话,重新设置ANDROID_PRODUCT_OUT的值与编译后镜像输出目录路径值一致。

其他

fastboot

一、Fastboot

1.1 Recovery模式(卡刷)

在系统进行定制时,编译系统会编译出一份ZIP的压缩包,里面是一些系统分区镜像,提供给客户进行手动升级、恢复系统。需要提前将压缩包内置SDcard,在Recovery模式进行。

进入Recovery方法:将手机完全关机后,按住音量键下(上)+电源键,进入BootLoader界面。用音量加减来控制光标,电源键来进行确认(有的机器只能用音量下键进行选择,上键是确认键)。说明:有的机器可能没有预装Recovery。

1.2 Recovery模式(线刷)

在安卓手机中Fastboot是一种比Recovery更底层的刷机模式。使用USB数据线连接手机的一种刷机模式。这就是所谓的线刷,与Recovery模式相比Fastboot需要掌握一些烧机命令,对于某些系统卡刷来说,线刷更可靠,安全。

二、Android系统分区介绍

  • bootloader 系统开机引导类似电脑BIOS,这块刷错手机就会变成砖

  • radio 通讯模块、基带、WIFI、Bluetooth等衔接硬件的驱动软件

  • recovery 系统故障时负责恢复

  • boot Linux嵌入式系统内核

  • system 系统文件、应用

  • cache 系统运行时产生的缓存

  • userdata 用户使用APP产生的缓存数据

模块编译介绍

1
2
3
4
5
6
7
8
9
10
11
#除了通过make命令编译可以整个android源码外,Google也为我们提供了相应的命令来支持单独模块的编译.
#编译环境初始化(即执行source build/envsetup.sh)之后,我们可以得到一些有用的指令,除了上边用到的lunch,还有以下:

- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory.
- mmm: Builds all of the modules in the supplied directories.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir: Go to the directory containing a file.

其中mmm指令就是用来编译指定目录.通常来说,每个目录只包含一个模块.比如这里我们要编译Launcher2模块,执行指令:

1
mmm packages/apps/Launcher2/

稍等一会之后,如果提示:

make completed success fully

即表示编译完成,此时在out/target/product/gereric/system/app就可以看到编译的Launcher2.apk文件了.

重新打包系统镜像

1
编译好指定模块后,如果我们想要将该模块对应的apk集成到系统镜像中,需要借助make snod指令重新打包系统镜像,这样我们新生成的system.img中就包含了刚才编译的Launcher2模块了.重启机器之后生效.

单独安装模块

1
2
我们在不断的修改某些模块,总不能每次编译完成后都要重新打包system.img,然后重启手机吧?有没有什么简单的方法呢?
在编译完后,借助adb install命令直接将生成的apk文件安装到设备上即可,相比使用make snod,会节省很多事件.

编译目录介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

- Android系统自带的apk文件都在```out/target/product/generic/system/apk```目录下
- 一些可执行文件(比如C编译的执行),放在```out/target/product/generic/system/bin```目录下
- 动态链接库放在```out/target/product/generic/system/lib```目录下
- 硬件抽象层文件都放在```out/targer/product/generic/system/lib/hw```目录下

### 源码目录介绍

![](https://img2020.cnblogs.com/blog/2275974/202101/2275974-20210109203736099-1901913006.png)


![](https://img2020.cnblogs.com/blog/2275974/202101/2275974-20210109203759902-672469351.png)


### lunch 后面的参数介绍

lunch aosp_arm-eng

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

该命令表示针对模拟器进行完整编译,并且所有调试功能均处于启用状态。

如果您没有提供任何参数就运行命令,lunch 将提示您从菜单中选择一个目标。

所有编译目标都采用 BUILD-BUILDTYPE 形式,其中 BUILD 是表示特定功能组合的代号。

BUILDTYPE 是以下类型之一:


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

![](https://img2020.cnblogs.com/blog/2275974/202101/2275974-20210109203911293-1939592244.png)


![](https://img2020.cnblogs.com/blog/2275974/202101/2275974-20210109204041185-295820055.png)



### 单独刷入某个 img:

fastboot刷入命令格式:fastboot flash 系统分区 对应的name.img

```shell
cd out/target/product/marlin
fastboot flash boot_a boot.img
fastboot flash boot_b boot.img
fastboot flash system system.img
fastboot flash system_b system_other.img
fastboot flash vendor vendor.img
fastboot flash userdata userdata.img
fastboot flash userdata data.img

参考文章

1
2
3
4
5
https://blog.csdn.net/wengliuhu/article/details/107979371 源码目录介绍
https://developers.google.cn/android/images#marlin 官网刷机包,救砖
https://blog.csdn.net/u012417380/article/details/73196722 lunch参数介绍
https://source.android.com/source/building 官网刷机教程
https://www.dazhuanlan.com/2019/12/09/5dedf04e496e7 Android8 分区表变化和相关信息