Android12源码编译及烧写Piexl6设备
我忙了一个多星期编译Android12源码到刷机Pixel6设备,因为公司的硬件环境限制和源码太大,中间浪费了很多时间,编译前一定要规划好硬件环境编译手机源码配置,否则就会事倍功半。 硬件要求 2. 软件要求
安装所需的软件包(Ubuntu18.04)
64 位版本的 Ubuntu
sudoapt-getinstallgit-coregnupgflexbisonbuild-essentialzipcurlzlib1g-devgcc-multilibg+±multiliblibc6-dev-i386libncurses5lib32ncurses5-devx11proto-core-devlibx11-devlib32z1-devlibgl1-mesa-devlibxml2-util sxsltprocunzipfontconfig
安装所需的软件包(Ubuntu14.04)
64 位版本的 Ubuntu
sudoapt-getinstallgit-coregnupgflexbisongperfbuild-essentialzipcurlzlib1g-devgcc-multilibg+±multiliblibc6-dev-i386libncurses5lib32ncurses5-devx11proto-core-devlibx11-devlib32z-devlibgl1-mesa-devlibxml2-utilsxsltprocunzip
3.源码下载 1.安装repo
repo是Android为了方便管理多个git仓库而开发的Python脚本。 repo的出现并不是为了取代git,而是为了让Android开发者能够更有效地使用git。 Android 源代码包含数百个 git 库。 仅仅下载这么多的git库是一项艰巨的任务,因此Android在下载源代码时引入了repo。
Repo命令参考:
repo工具下载并添加执行权限
> mkdir ~/bin
路径=~/bin:$路径
curl -sSL'+/master/repo?format=TEXT'|base64 -d>~/bin/repo
chmoda+x ~/bin/repo
2.下载源码并更改repo脚本,将~/bin/repo文件上REPO_URL的值修改为~上的“https://mirrors.tuna.tsinghua.edu.cn/git/git-repo” /.bashrc文件减少配置:REPO_URL=''
创建源码根目录
本地创建一个任意名称的目录,如下
mkdirandroid12
cdandroid12
初始化仓库
初始化仓库有两种方法。 第一种是直接下载,会下载所有分支代码; 第二种是下载特定标签的版本,即下载Android的某个版本,如下:
直接下载的形式,这些方法占用C盘空间很大,而且下载时间也比较长,所以不推荐。 初始化方法如下:
重新定位-u
要下载特定标签,首先确定需要下载的Android版本。 例如,您需要下载pixel6对应的Android 12最新版本。 从Android官网查看Android版本对应的标签,如下:
官方网站网址:
可以看到对应的标签为android-12.1.0_r11,初始化方法如下:
重新指向-u-bandroid-12.1.0_r11
注意:如果初始化仓库之前没有配置git相关信息,会报错。 报错后,配置git,然后按照前面的步骤重新初始化
同步代码
初始化完成后就可以同步代码了
reposync-j4
-j4表示启用4个线程同步代码。 同步时并发数不宜过高。 建议选择4
4.真机驱动下载
在谷歌手机上省砖时,需要下载谷歌硬件驱动。 不同的初始化仓库,下载的驱动是不同的。
将下载的驱动压缩包复制到Android源码根目录下,并解压,解压后得到一个.sh脚本
解压命令
tar-zxvfgoogle_devices-oriole-sp2a.220305.013.a3-04c512f4.tgz
执行extract-google_devices-oriole.sh脚本以提取驱动程序
./extract-google_devices-oriole.sh
运行提示“PressEntertoviewthelicense”时,输入Enter键编译手机源码配置,然后输入d进行翻页
当提示输入“IACCEPT”时,提示后按回车即可完成驱动程序的提取。 完成后,源码根目录下有一个vendor目录
五、编译
设置代码编译环境。 每次重启shell时,都需要配置编译环境。 在根目录下执行以下命令:
源构建/envsetup.sh
或者
.build/envsetup.sh
执行build/envsetup.sh脚本后,在环境中配置几个shell命令,然后使用该命令选择设备目标并编译。 输入hmm命令查看完整命令列表:
- lunch: lunch -
Selects as the product to build, and as the variant to
build, and stores those selections in the environment to be read by subsequent
invocations of 'm' etc.
- tapas: tapas [ ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Sets up the build environment for building unbundled apps (APKs).
- banchan: banchan [ ...] [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 命令选择设备目标
如果lunch命令输入列出的设备目标中没有之前的设备目标,则直接在其中输入设备目标即可。
开始编译,输入m或make编译整个Android源码,或者使用参数-j指定线程数
如果只编译某部分,先切换到对应目录,使用mm命令进行编译
6、刷机
Windows系统下载adb和fastboot工具
下载链接:
解压后将目录添加到环境变量中
在手机上启动USB调试
- 要在通过 USB 连接的设备上使用 adb,必须在设备的系统设置中启用 USB 调试(位于开发人员选项下)
- “开发者选项”界面默认隐藏,要显示它,请进入“设置”>“关于手机”,然后点击版本号七次,会提示“Youarenodeveloper!”
- 然后进入设置>系统>开发者选项,可以打开USB调试
- 如果此时没有安装Google的usb驱动,则在设备管理器中找不到“AndroidDevice”设备。 adbrebootbootloader进入fastboot模式后,输入fastbootdevices命令,找不到设备
安装驱动程序
下载链接:
在设备管理器中找到对应的设备安装驱动。 安装后会多出一个“AndroidDevice”设备
解锁并加载引导加载程序
- 在cmd中输入“adbrebootbootloader”进入fastboot模式并检查是否处于锁定模式
- 如果是锁定模式,输入“fastbootflashingunlock”解锁,需要在手机上确认
- 进入解锁模式后,设备每次启动都会先进入fastboot模式。 如果需要处于锁定模式,输入“fastbootflashinglock”命令
闪光装置
- 在cmd中输入“adbrebootbootloader”进入fastboot模式
-切换到Android镜像目录,输入“fastbootflashall -w”命令将镜像刷写到设备,刷写完成后等待设备重启,原生Android系统即可运行