Android12 源码编译及刷写Piexl6设备
- 一、硬件要求
- 二、软件要求
- 三、源码下载
- 1. 安装repo
- 2. 下载源码
- 四、真机驱动下载
- 五、编译
- 六、刷机
捣鼓了一个多星期Android12源码编译到刷写Pixel6设备,由于公司硬件环境限制和源码太大,中间浪费了很多时间,编译前一定要准备好硬件环境,不然事倍功半
一、硬件要求
- cpu:需要 64 位环境
- 内存:至少需要 16 GB 的可用 RAM,但 Google 建议提供 64 GB
- 硬盘空间:如果要检出代码,至少需要 250 GB 可用磁盘空间;如果要进行构建,则还需要 150 GB。如果要进行多次构建,则需要更多空间。
内存空间如果不够,可以通过增加swap空间的方法来解决,但是在windows中Ubuntu子系统试过不行
二、软件要求
- 操作系统:Ubuntu (LTS) 版本,不支持 MacOS 或 Windows
- 使用python3
- 所需的软件包
- 安装所需的软件包 (Ubuntu 18.04)
64 位版本的 Ubuntu
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
- 安装所需的软件包 (Ubuntu 14.04)
64 位版本的 Ubuntu
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g+±multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip
三、源码下载
1. 安装repo
repo是Android为了方便管理多个git库而开发的Python脚本。repo的出现,并非为了取代git,而是为了让Android开发者更为有效的利用git。Android源码包含数百个git库,仅仅是下载这么多git库就是一项繁重的任务,所以在下载源码时Android就引入了repo。
repo工具下载并添加执行权限
> mkdir ~/bin
PATH=~/bin:$PATH
curl -sSL ‘https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT’ |base64 -d > ~/bin/repo
chmod a+x ~/bin/repo
2. 下载源码
- 由于国内不能访问Android仓库,建议使用清华源下载Android源代码,两种修改方式使用清华源:
- 修改repo脚本,将~/bin/repo文件里面的REPO_URL的值修改为"https://mirrors.tuna.tsinghua.edu.cn/git/git-repo"
- 在~/.bashrc文件里面增加一条配置:REPO_URL = ‘https://mirrors.tuna.tsinghua.edu.cn/git/git-repo’
- 开始下载源码
- 创建源码根目录
在本地建一个目录,名字任意,如下
mkdir android12
cd android12
- 初始化仓库
初始化仓库有两种方式,第一种是直接下载,这种方式会下载所以分支代码;第二种下载特定tag的版本,这种方式下载的是Android的某一个版本,具体如下:
- 直接下载方式,这种方式占用磁盘空间比较大,下载时间比较长,不太建议,初始化方法如下:
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest
- 下载特定tag的方式,首先确定需要下载的Android版本,比如需要下载pixel6对应的Android12最新版本,从Android官网查看Android版本对应的tag,如下:
官网网址:https://source.android.google.cn/setup/start/build-numbers#source-code-tags-and-builds - 从中可以看出对应的tag为android-12.1.0_r11,初始化方法如下:
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-12.1.0_r11
注:初始化仓库前如果没有配置git相关信息则会报错,报错后配置一下git,在重新按上面的步骤初始化即可
- 同步代码
初始化完成后即可同步代码
repo sync -j4
- -j4表示启用4个线程来同步代码,sync的时候并发数不宜太高,建议选择4
四、真机驱动下载
在Google手机上刷机时需要下载Google硬件驱动,初始化仓库方式不同,下载的驱动也不同
- 直接下载方式
下载网址:https://developers.google.cn/android/blobs-preview - 找到对应的机型,点击Link即可下载
- 下载特定tag方式
下载网址:https://developers.google.cn/android/drivers - 根据特定tag对应的build ID找到对应机型的驱动,点击Link即可下载
- 提取驱动
- 将下载的驱动压缩包拷贝的Android源码根目录,并进行解压,解压后得到一个.sh的脚本
解压命令
tar -zxvf google_devices-oriole-sp2a.220305.013.a3-04c512f4.tgz
- 执行extract-google_devices-oriole.sh脚本提取驱动
./extract-google_devices-oriole.sh
运行提示"Press Enter to view the license"时输入回车后即可,后面输入d来进行翻页
直到提示输入"I ACCEPT"时,按提示输入后回车即可完成驱动的提取,完成后源码根目录多了一个vendor的目录
五、编译
- 设置代码编译环境,每次重启shell时都需要配置编译环境,在根目录执行如下命令:
source build/envsetup.sh
- 或
. build/envsetup.sh
- 执行build/envsetup.sh脚本后则在环境中配置几个shell命令,后续即可使用这些命令选择设备目标和编译,输入hmm命令即可查看完整的命令列表:
- lunch: lunch <product_name>-<build_variant>
Selects <product_name> as the product to build, and <build_variant> as the variant to
build, and stores those selections in the environment to be read by subsequent
invocations of 'm' etc.
- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Sets up the build environment for building unbundled apps (APKs).
- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Sets up the build environment for building unbundled modules (APEXes).
- croot: Changes directory to the top of the tree, or a subdirectory thereof.
- m: Makes from the top of the tree.
- mm: Builds and installs all of the modules in the current directory, and their
dependencies.
- mmm: Builds and installs all of the modules in the supplied directories, and their
dependencies.
To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma: Same as 'mm'
- mmma: Same as 'mmm'
- provision: Flash device with all required partitions. Options will be passed on to fastboot.
- cgrep: Greps on all local C/C++ files.
- ggrep: Greps on all local Gradle files.
- gogrep: Greps on all local Go files.
- jgrep: Greps on all local Java files.
- ktgrep: Greps on all local Kotlin files.
- resgrep: Greps on all local res/*.xml files.
- mangrep: Greps on all local AndroidManifest.xml files.
- mgrep: Greps on all local Makefiles and *.bp files.
- owngrep: Greps on all local OWNERS files.
- rsgrep: Greps on all local Rust files.
- sepgrep: Greps on all local sepolicy files.
- sgrep: Greps on all local source files.
- godir: Go to the directory containing a file.
- allmod: List all modules.
- gomod: Go to the directory containing a module.
- pathmod: Get the directory containing a module.
- outmod: Gets the location of a module's installed outputs with a certain extension.
- dirmods: Gets the modules defined in a given directory.
- installmod: Adb installs a module's built APK.
- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
- 使用lunch命令选择设备目标
- Android模拟设备
lunch sdk_phone_x86_64
- Google设备
不同的设备选择的目标不同,根据Android官网给出的设备目录选项来选择
网址:https://source.android.google.cn/setup/build/running#selecting-device-build 如图Pixel6对应的目标为aosp_oriole-userdebug,输入如下命令即可
lunch aosp_oriole-userdebug
如果输入lunch命令列出的设备目标中没有上面的设备目标,则直接输入上面的设备目标即可
- 开始编译,输入m或make即可编译整个Android源码,也可使用参数-j指定线程数
m
- 如果只编译某一部分,则先切换的对应的目录下,使用mm命令进行编译
六、刷机
- Android模拟设备,输入emulator命令即可启动Android模拟器
- Pixel真机刷机
- window系统下载adb和fastboot工具
下载链接:https://developer.android.google.cn/studio/releases/platform-tools#downloads 解压后将目录添加到环境变量中 - 手机上启动USB调试
- 如要在通过 USB 连接的设备上使用 adb,则必须在设备的系统设置中启用 USB 调试(位于开发者选项下)
- “开发者选项”屏幕默认情况下处于隐藏状态,如需将其显示出来,请依次转到设置 > 关于手机,然后点按版本号七次就会提示"You are now a developer!"
- 再依次转到设置 > 系统 > 开发者选项里面,可以开启USB调试
- 此时如果没有安装Google的usb驱动,在设备管理器里面找不到"Android Device"设备,adb reboot bootloader进入fastboot模式之后,输入fastboot devices命令找不到设备 - 安装驱动
下载链接:https://developer.android.google.cn/studio/run/win-usb 在设备管理器中找到对应的设备安装驱动,安装好后则会多出一个"Android Device"设备 - 解锁加载引导程序
- 在cmd中输入"adb reboot bootloader"进入fastboot模式,查看是否处于锁定模式
- 如果处于锁定模式则输入"fastboot flashing unlock"进行解锁,同时需要在手机上进行确认
- 处于解锁模式后,设备每次启动都会先进入fastboot模式,如需处于锁定模式,则输入"fastboot flashing lock"命令 - 刷写设备
- 在cmd中输入"adb reboot bootloader"进入fastboot模式
- 切换到Android镜像目录,输入"fastboot flashall -w"命令即可将镜像刷写到设备中,刷写完成等待设备重启就是运行的原生Android系统了