硬件配置
- RAM:最低16GB,内存越高,编译时间越短
- ROM:最低400GB,代码250GB+构建150GB,建议不低于500GB
- 64位环境
系统配置(任选一种即可)
- Ubuntu 20.04
- Docker + Ubuntu 20.04
- VM + Ubuntu 20.04
- VM + Ubuntu 20.04 + Docker + Ubuntu 20.04 (我选择的这个,为了更好的移植性)
Android源码选择
- 选择官方AOSP ,国内镜像会快点,AOSP | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
- 选择开发板厂家提供的源码,一般以网盘存储,记得开会员不然没有半个月下不下来(我这里选择的是rk3399的开发板,没办法家里有矿)首次编译用时4小时,开始编译后记得保证电脑不死机
一、配置Docker编译环境
1.1安装Docker
- 安装宿主 Ubuntu 20.04 系统,获取地址 清华大学开源软件镜像站
- 安装docker软件
sudo apt install docker.io
- 配置当前用户(非root)docker执行权限
- 把当前用户添加到docker组中,并更新当前执行命令的shell的group id。 这样,就无需使用root权限运行docker命令。 我使用的系统中,前2条命令不用执行,安装docker.io后都已经设置好了。
sudo chown root:docker /var/run/docker.sock
sudo groupadd docker
sudo gpasswd -a $USER docker
newgrp docker
- 在Host OS(Ubuntu 22.04)中查看当前用户id:
id
1.2 使用Dockerfile构建镜像
# 下载Docker配置
git clone https://gitee.com/FunXiaoYuZhou/docker-cross-compiler-novnc.git
# 进入到项目中
cd docker-ubuntu-lxde-novnc
# 构建镜像
docker build --no-cache -t docker-ubuntu-lxde-novnc .
# 使用构建的镜像创建容器
sudo docker run -d --privileged=true --net=host -v /dev:/dev -v ~/aosp_src:/aosp_src --restart=always --name build_android docker-ubuntu-lxde-novnc:latest
- 配置docker容器内的系统用户,保持和Host OS一致
- 启动docker容器,并创建相应的用户:
# 进入docker 容器
sudo docker exec -it build_android bash
#创建新的用户组和用户
groupadd -g 1000 dev
useradd -m -g 1000 -u 1000 dev -s /bin/bash
#切换为新创建的用户
su dev
二、 本地搭建交叉编译环境
2.1 安装编译所需软件包
建议使用amd64架构的Ubuntu 20.04操作系统,参考如下内容安装编译及打包所需要的软件包:
sudo apt-get -y update
sudo apt-get install -y sudo curl
sudo bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/friendlyarm/build-env-on-ubuntu-bionic/master/install.sh)"
对于中国大陆的用户亦可使用以下地址:
sudo bash -c \
"$(curl -fsSL http://112.124.9.243:3000/friendlyelec/build-env-on-ubuntu-bionic/raw/branch/cn/install.sh)"
你的电脑上会安装好如下交叉编译器:
版本 | 架构 | 编译器路径 | 用途 |
---|---|---|---|
4.9.3 | armhf | /opt/FriendlyARM/toolchain/4.9.3 | 用于编译armhf架构的应用程序 |
6.4 | aarch64 | /opt/FriendlyARM/toolchain/6.4-aarch64 | 用于交叉编译4.4内核 |
11.3 | aarch64 | /opt/FriendlyARM/toolchain/11.3-aarch64 | 用于交叉编译4.19及以上内核(含5.10,6.1)及uboot |
2.2 设置交叉编译器
参考上一节的表格,选用合适版本的编译器,然后将编译器的路径加入到PATH中,例如要使用11.3的交叉编译器,用vi编辑~/.bashrc,在末尾加入以下内容:
export PATH=/opt/FriendlyARM/toolchain/11.3-aarch64/bin:$PATH
export GCC_COLORS=auto
执行一下~/.bashrc脚本让设置立即在当前shell窗口中生效,注意"."后面有个空格:
. ~/.bashrc
验证是否安装成功:
$ aarch64-linux-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gcc
COLLECT_LTO_WRAPPER=/opt/FriendlyARM/toolchain/11.3-aarch64/libexec/gcc/aarch64-cortexa53-linux-gnu/11.3.0/lto-wrapper
Target: aarch64-cortexa53-linux-gnu
Configured with: /home/cross/arm64/src/gcc/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=aarch64-cortexa53-linux-gnu --prefix=/opt/FriendlyARM/toolchain/11.3-aarch64 --exec_prefix=/opt/FriendlyARM/toolchain/11.3-aarch64 --with-sysroot=/opt/FriendlyARM/toolchain/11.3-aarch64/aarch64-cortexa53-linux-gnu/sysroot --enable-languages=c,c++ --enable-fix-cortex-a53-843419 --with-arch=armv8-a+crypto+crc --with-cpu=cortex-a53 --with-pkgversion=ctng-1.25.0-119g-FA --with-bugurl=http://www.friendlyelec.com/ --enable-objc-gc --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=/home/cross/arm64/buildtools --with-mpfr=/home/cross/arm64/buildtools --with-mpc=/home/cross/arm64/buildtools --with-isl=/home/cross/arm64/buildtools --enable-lto --enable-threads=posix --disable-libstdcxx-pch --enable-clocale=gnu --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-indirect-function --enable-gnu-unique-object --enable-default-pie --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-gold --with-libintl-prefix=/home/cross/arm64/buildtools --disable-multilib --with-local-prefix=/opt/FriendlyARM/toolchain/11.3-aarch64/aarch64-cortexa53-linux-gnu/sysroot --enable-long-long --enable-checking=release --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.3.0 (ctng-1.25.0-119g-FA)
三、增大交换分区空间(swap),Android12及以上编译需要配置
# 将现有swap移动到主内存,可能需要几分钟
sudo swapoff -a
# 创建新的swap文件,bs×count=最后生成的swap大小,这里设置16G
sudo dd if=/dev/zero of=/swapfile bs=1G count=16
# 设置权限
sudo chmod 0600 /swapfile
# 设置swap
sudo mkswap /swapfile
# 打开swap
sudo swapon /swapfile
# 检查设置是否有效
grep Swap /proc/meminfo # 或者htop看一下
# 设置永久有效
sudo gedit /etc/fstab
# 在末尾行加上
# /swapfile swap swap sw 0 0