美文网首页
Android源码编译过程(AOSP)

Android源码编译过程(AOSP)

作者: 初见破晓 | 来源:发表于2023-05-20 19:22 被阅读0次

提前过坑

整个编译过程困难重重 ,从系统安装到整个编译的流程让人头大。先列举出几点需要特别注意的地方,避免日后频繁踩坑

  • 关闭Window Bitlocker功能
  • swap分区需要16GB
  • 源码磁盘需要格式化成ext4格式,空间大约300GB
  • 安装时屏蔽默认显卡驱动
  • 编译时线程最佳为Cpu核心数+1

双系统安装

下载镜像ISO文件

直接从官网下载最新的镜像文件
ubuntu

制作启动盘

接着我们使用rufus制作启动盘,注意U盘容量,最好大于8GB
这一步没什么好说的,配置项基本是默认的,照着下面图片修改就好了
这一步大概10分钟左右的时间

选择镜像
制作镜像

新建硬盘分区

因为要安装双系统,所以安装系统前需要对硬盘进行分区。分区大小可以根据自己硬盘的空闲情况来定,因为硬盘容量不足,我选择了128GB安装Ubuntu。
首先点击window的磁盘管理选项


磁盘管理

如下页面


磁盘管理
右键选择压缩卷
压缩卷
分配大小

保持成未分配就好(以上是从网络复制的图片,这里我设置了128GB)


未分配

BIOS设置

安装前需要进行以下几个设置
关闭Window Bitlocker功能
在window设置界面搜索 Bitlocker


Bitlocker

找到该页面上的关闭Bitlocker


关闭Bitlocker
然后耐心等待完成,大概5分钟左右
image.png
关闭bios对应设置
不同品牌的电脑bios设置也是不尽相同,不过通常需要关闭secure boot enable等配置

通过u盘安装系统

直接从U盘引导即可安装
不过加载ubuntu安装界面是有可能黑屏,这是因为ubuntu对显卡的支持有关,需要手动添加显卡选项:nomodeset,使其支持Nvidia系列显卡
进入安装时,光标移动到”install ubuntu”,按”e”进入编辑模式,进入命令行模式,找到”quite splash”然后去掉”–“后,添加“nomodeset”


修改启动项

然后就会进入系统选择界面,使用方向键选择 ubuntu 后回车就进入了 Ubuntu 的安装引导界面。在侧边栏中选择系统语言,English、Chinese都可,看自己喜好,然后点击 Install Ubuntu 进入安装


语言选择
选择键盘布局,跟前面一样,按个人喜好选择
键盘布局
选择安装方式,选择正常安装就行,会默认安装火狐浏览器等软件。或者选择最小安装的话可以在安装完成后自行安装需要的软件,两种方式影响不大。下面的安装第三方软件选项也可以选上,也可以不选,后面再根据需要手动安装。我这里就只选择了正常安装,然后点击继续。
安装方式

选择 Something else,即手动及进行磁盘分区。


手动分区

磁盘分区

因为Window用了一部分硬盘空间,这里我选择最简单的分区方案,一共四个分区:

分区 大小 类型 备注
/boot 512MB 主分区 系统的boot启动引导项安装位置
/ 64GB 主分区 根目录,所有目录的根节点,其下包含很多子目录,如/usr /tmp等
/swap 16GB 逻辑分区 交换空间,一般是物理内存的1~2倍就行了,但是编译Android系统推荐16GB
/home 剩下多少就是多少 逻辑分区 一般放置自己的数据

点击加号,创建上述的各种分区


新增分区 /boot
/swap

重启进入系统

环境配置

以下以编译Android13系统为例

安装工具链

sudo apt install repo git-core 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 libgl1-mesa-dev libxml2-utils xsltproc unzip libncurses5

安装Git

sudo apt-get install git
git config --global user.name xxx
git config --global user.email xxx

安装Java

sudo apt-get install openjdk-11-jdk

安装openjdk,直接装高版本避免其他问题

安装Python

sudo apt install python2-minimal
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 3
sudo update-alternatives --config python

编译Android13需要使用Python3版本。使用update-alternatives进行切换

安装Repo

curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo
echo "PATH=~/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
whereis repo

配置环境变量

// 将环境变量添加到 ~/.bashrc文件中
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

将环境变量添加到 ~/.bashrc文件中

下载初始包

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

解压得到的 AOSP 工程目录,正确的话应该什么都看不到,因为只有一个隐藏的 .repo 目录
压缩包大概60.8GB,可以使用迅雷下载能快很多

同步代码

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r43
repo sync -c --no-tags
  • 查看构建版本

  • -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 选项,能够让本地仓库删除对这个分支的跟踪引用。

  • 这一步取决于网络,但是不会很慢,半个小时左右。由于网络问题有可能断开,提供一个自动下载的脚本

#!/bin/bash
echo  "==========start repo sync==="
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-13.0.0_r43
repo sync -c --no-tags -j12
while [ $? = 1 ]; do  
        echo “======sync failed, re-sync again======”  
        sleep 3  
        repo sync -c --no-tags -j12
done 

编译代码

source build/envsetup.sh
make clean
lunch
lunch sdk_phone_x86_64-userdebug
make -j12
emulator

编译过程我的电脑大概2小时左右,可以使用以下设置使屏幕休眠

sudo vim /etc/systemd/logind.conf
service systemd-logind restart
sleep 2 && xset dpms force off
编译完成 模拟器运行

编译过程中关闭屏幕

sleep 2 && xset dpms force off

参考资料

https://neethu.cn/archives/ubuntu2204%E4%B8%8B%E7%BC%96%E8%AF%91aospandroid13%E5%B9%B6%E5%9C%A8%E6%A8%A1%E6%8B%9F%E5%99%A8%E4%B8%AD%E8%BF%90%E8%A1%8C
https://blog.csdn.net/h5630/article/details/127715207
https://www.cnblogs.com/fly263/p/16982647.html
https://blog.csdn.net/u010164190/article/details/125089071
https://blog.csdn.net/gh201030460222/article/details/80656513
https://blog.csdn.net/qq_37705385/article/details/129144401
https://blog.csdn.net/lucky_tom/article/details/127825300
http://www.vmaya.com/qnews_m_topic_m_tskey_m_qn7478016089954543334.html

相关文章

网友评论

      本文标题:Android源码编译过程(AOSP)

      本文链接:https://www.haomeiwen.com/subject/nggnjdtx.html