# this is a high level guide on how to build android
# for Raspberry Pi4 (rpi4) - this is NOT a beginners guide!
# So the requirement that you have either build android before or
# have enough experiences to understand what is important here

Builds as 64bit only so make sure you have propper firmware
on you rpi4. Same is true if you want to use an USB boot device

# sync omni source

Refer to http://source.android.com/source/downloading.html
Refer to https://github.com/omnirom/android

$ repo init -u git://github.com/omnirom/android.git -b android-13.0

If you want to use the contents of omni-private.xml please check
https://github.com/omnirom/android/README and head over to send us a short
email notice. Otherwise you can simple remove the include of that xml
file and build without

Independent of that you must have a ssh key in your github account
this is a requirement for easy syuncing

If you want to build with microG add the contents of <device>/repo/local_manifest.xml
to .repo/local_manifests/local_manifest.xml

$ repo sync

# Build Android source

Refer to http://source.android.com/source/building.html

# prepare environment

$ export ROM_BUILDTYPE=<MICROG|WEEKLY>
$ export PRODUCT_EXCLUDE_EXTRA_PACKAGES=true
$ source build/envsetup.sh

# start build

$ breakfast rpi4 userdebug
$ m

after the buils is complete the generated image files are located in 
out/target/product/rpi4/

# prepare boot device (sdcard or USB device)

Devices with GPT partition table cant be used as an USB boot device
You must use MBR (msdos) parition table

The boot devices MUST contain 4 partitions with the following layout
(parition sizes here are the minimum - userdata should be made bigger
if you have the space)

Device          Start      End  Sectors  Size Id Type
boot               2    262145   262144  128M  c W95 FAT32 (LBA)
system        264192   4458495  4194304    2G 83 Linux
vendor        4458496  4982783   524288  256M 83 Linux
userdata      4982784 16777215 11794432  5.6G 83 Linux

THE PARTITION ORDER MUST BE LIKE THIS

Starting with android-11 project quotas are mandatory in userdata
parition. Prebuilt images have it enabled but if you recreate your
userdata you MUST enable project quotas with e.g.

tune2fs -O project,quota <userdata partition device>

Using resize2fs will keep quota setting intact

# boot device mode

Switching between booting from sdcard and USB is done in ALL config.txt*
files by enabling the needed overlay

...
# booting from sdcard
dtoverlay=rpi-android-sdcard

# booting from usb
dtoverlay=rpi-android-usb
....

You can still mess with the fstab if you want and dont use the overlay

# flash to boot device

I will keep it to your experience how you get the img files
on to your boot device in the according partitions (system and vendor)

Boot partition needs special treatment so I recommend to
look at one of my prebuilt zip files to see what must be in that
partition

The files in the scripts dir are what I use to create and update
feel free to adjust and update to your needs if you want