一、下载android源码 aosp
一般来说需要通过storage.googleapis.com 下载,但是由于网络限制的原因,采用通过清华源镜像来下载。
1.打开 清华源AOSP镜像 (可以参考里面步骤下载)
2.下载repo 工具
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo> ~/bin/repo
chmod a+x ~/bin/repo
3.创建下载源码的目录
mkdir ~/aosp
cd ~/aosp
4. 下载android 12 源码,增加树梅派相关代码配置 可参考树梅派android 源码
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-12.1.0_r21
git clone https://github.com/android-rpi/local_manifests .repo/local_manifests -b arpi-12
repo sync
二、编译源码
1.编译环境需安装些工具(可能列的不全)
sudo apt install libssl-dev python3-setuptools
sudo apt update
sudo apt install flex bison build-essential 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 fontconfig -y
sudo apt install make git-core gnupg zip unzip curl python3 openjdk-11-jdk -y
sudo apt clean && sudo apt autoremove -y
sudo apt-get update
sudo apt-get install -y python3-pip python3-setuptools python3-wheel ninja-build
pip3 install meson
sudo apt-get install meson
pip install mako
2.编译AOSP
source build/envsetup.sh
lunch rpi4-eng
make ramdisk systemimage vendorimage
3.下载内核源码
mkdir ~kernel
cd ~kernel
repo init -u https://github.com/android-rpi/kernel_manifest -b arpi-5.10
repo sync
4.编译内核 (android 11 后可以单独编内核)
cd ~kernel/
build/build.sh
三、刷系统
1.准备一张sd卡,16G
2.将SD分区(最好用命令行分区,试过用Gparted不行)
设置如下: p1 128MB for boot : Do fdisk, set W95 FAT32(LBA) & Bootable type, mkfs.vfat p2 1024MB for /system : Do fdisk, new primary partition p3 128MB for /vendor : Do fdisk, new primary partition p4 remainings for /data : Do fdisk, mkfs.ext4 Set volume label of /data partition as userdata : use -L option for mkfs.ext4
具体方法如下:
先删除sd卡现有的所有分区:
sudo fdisk -l
得到SD卡路径是/dev/sdc
sudo fdisk /dev/sdc
d:先删掉已有分区(在磁盘管理那里删了,这一步可以不用)
n:创建新的四个分区(/root /system /vendor /data),都是primary分区类型,设置分区大小时输入需要的MB。
a:设置boot分区为bootable
t:改变分区的类型(W95 FAT32 (LBA),Linux)
具体操作截图如下:
$ sudo mkfs.vfat -n boot /dev/sdc1
$ sudo mkfs.ext4 -L system /dev/sdc2
$ sudo mkfs.ext4 -L vendor /dev/sdc3
$ sudo mkfs.ext4 -L data /dev/sdc4
3.写入 system & vendor 分区
cd ~/aosp/out/target/product/rpi4
sudo dd if=system.img of=/dev/sdc2 bs=1M
sudo dd if=vendor.img of=/dev/sdc3 bs=1M
4.复制固件到boot分区 (直接复制不需要命令行)
~/aosp/device/arpi/rpi4/boot/* to p1:/ out/target/product/rpi4/ramdisk.img to p1:/
<kernel directory>/out/arpi-5.10/dist/Image.gz to p1:/ <kernel directory>/out/arpi-5.10/dist/bcm2711-rpi-*.dtb to p1:/ <kernel directory>/out/arpi-5.10/dist/vc4-kms-v3d-pi4.dtbo to p1:/overlays/
5.搞完,直接插卡开机
不想自己编译的可以下载我编译的:
https://download.csdn.net/download/ange_li/88570203
下载完可以按文章内容刷机
四、创建镜像方法
创建完镜像可以直接用树梅派官方工具刷卡:
以下是创建镜像的脚本:
#/bin/bash
LINEAGEVERSION=ange-1.1
DATE=`date +%Y%m%d`
IMGNAME=$LINEAGEVERSION-$DATE-rpi4.img
IMGSIZE=7
OUTDIR=${ANDROID_PRODUCT_OUT:="./aosp/out/target/product/rpi4"}
if [ `id -u` != 0 ]; then
echo "Must be root to run script!"
exit
fi
if [ -f $IMGNAME ]; then
echo "File $IMGNAME already exists!"
else
echo "Creating image file $IMGNAME..."
dd if=/dev/zero of=$IMGNAME bs=1M count=$(echo "$IMGSIZE*1024" | bc)
sync
echo "Creating partitions..."
(
echo o
echo n
echo p
echo 1
echo
echo +128M
echo n
echo p
echo 2
echo
echo +1536M
echo n
echo p
echo 3
echo
echo +256M
echo n
echo p
echo
echo
echo t
echo 1
echo c
echo a
echo 1
echo w
) | fdisk $IMGNAME
sync
LOOPDEV=`kpartx -av $IMGNAME | awk 'NR==1{ sub(/p[0-9]$/, "", $3); print $3 }'`
sync
if [ -z "$LOOPDEV" ]; then
echo "Unable to find loop device!"
kpartx -d $IMGNAME
exit
fi
echo "Image mounted as $LOOPDEV"
sleep 5
mkfs.fat -F 32 /dev/mapper/${LOOPDEV}p1 -n boot
mkfs.ext4 /dev/mapper/${LOOPDEV}p2 -L system
mkfs.ext4 /dev/mapper/${LOOPDEV}p3 -L vendor
mkfs.ext4 /dev/mapper/${LOOPDEV}p4 -L userdata
resize2fs /dev/mapper/${LOOPDEV}p4 1343228
echo "Copying system..."
dd if=$OUTDIR/system.img of=/dev/mapper/${LOOPDEV}p2 bs=1M
echo "Copying vendor..."
dd if=$OUTDIR/vendor.img of=/dev/mapper/${LOOPDEV}p3 bs=1M
echo "Copying boot..."
mkdir -p sdcard/boot
sync
mount /dev/mapper/${LOOPDEV}p1 sdcard/boot
sync
mkdir -p sdcard/boot/overlays
cp ./aosp/device/arpi/rpi4/boot/* sdcard/boot
cp $OUTDIR/ramdisk.img sdcard/boot
cp ./kernel/out/arpi-5.10/dist/Image.gz sdcard/boot
cp ./kernel/out/arpi-5.10/dist/bcm2711-rpi-4-b.dtb sdcard/boot
cp ./kernel/out/arpi-5.10/dist/bcm2711-rpi-400.dtb sdcard/boot
cp ./kernel/out/arpi-5.10/dist/vc4-kms-v3d-pi4.dtbo sdcard/boot/overlays
sync
umount /dev/mapper/${LOOPDEV}p1
rm -rf sdcard
kpartx -d $IMGNAME
sync
echo "Done, created $IMGNAME!"
fi
另外一个也可以用的源码:https://github.com/raspberry-vanilla/android_local_manifest
参考文章:树莓派Android10源码试装_树莓派 android系统编译-CSDN博客