In this post, we will go through a full cycle of building a custom AOSP image, flashing it on a Pixel phone, and then restoring it to an original stock image. We will use Pixel 6a (codenamed “bluejay”) as the device example. We will walk through it in a succinct manner without detailed explanations.

  • Note the Android build number
  • Click “Settings”/”About Phone”.
  • In my case, it is “bluejay-tp1a.221105.002”
  • Enable developer options
  • Click on the “Build Number” row 7 times to enable developer options
  • Enable “OEM unlocking”
  • Toggle “Settings”/”System”/”Advanced”/”Developer Options”/”OEM unlocking”

My host is Ubuntu 20.04. Generally, we are following this guide from Google. Below is a quick gist.

sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
sudo apt install python2 #repo needs python
sudo usermod -aG plugdev $LOGNAME
apt-get install android-sdk-platform-tools-common # install udev rules and adb

Generally, we are following this guide from Google. Below are some specific steps.

  • Find the exact branch/tag that matches the device’s current build.
  • Go to the Android build tag page
  • Search for the build number noted in the previous step
  • Find the corresponding AOSP tag. In my case, it is “android-14.0.0_r2”
  • Check out this branch
mkdir android-14.0.0_r2
cd android-14.0.0_r2
repo init --depth=1 -u -b android-14.0.0_r2
repo sync --force-sync --no-clone-bundle --no-tags -j$(nproc)
  • Go to Google’s proprietary binary page
  • Find the section corresponding to your device (Pixel 6a in my case) and the build number (“UP1A.231005.007.A1”)
  • Download driver binaries
  • Untar them under the root directory of the AOSP source tree
cd ~/android-14.0.0_r2
tar xzf ~/Downloads/google_devices-bluejay-up1a.231005.007.a1-2ceb7344.tgz
./extract-google_devices-bluejay.sh
source build/envsetup.sh
lunch
m
  • On the phone, enable OEM unlocking and USB debugging
  • Toggle “Settings”/”System”/”Advanced”/”Developer options”/”USB debugging”
  • Toggle “Settings”/”System”/”Advanced”/”Developer options”/”OEM unlocking”
  • On the PC in the same terminal where you just build the AOSP image
adb devices             # it should show the phone device attached over ADB
adb reboot bootloader # reboot to fastboot
fastboot devices # it should show the phone device attached over ADB
export ANDROID_PRODUCT_OUT= # set the path of the folder where ROM is present
fastboot flashing unlock
fastboot flashall -w

During “fastboot flashing unlock” step, you need to follow the instructions on the screen, press the up/down volume button to select “unlock bootloader”, and press the power button to commit.

If things go smoothly, the phone will reboot a couple of times and eventually boot into the freshly built AOSP image with root access.

adb root && adb remount && adb reboot # This will ONLY be needed for the first time,
adb root
adb remount # if this is first time, refer the first command
adb push my-app.apk /system/priv-app
adb reboot # to complete the app installation

Often times you need to assign PivilegedOrSystem level permissions to system apps (which is why you want to install system app at the first place). Starting from Android 8.1, you will need to explicitly whitelist these permissions in system XML files.

For example, to give READ_PRIVILEGED_PHONE_STATE permission to a testing app, com.goyal.demoapp, you need to create the following file on phone, /etc/permissions/priv-app/privapp-permissions-demoapp.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
This XML file declares which signature|privileged permissions should be granted to privileged
applications that come with the platform
-->
<permissions>
<privapp-permissions package="com.goyal.demoapp">
<permission name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>
</privapp-permissions>
</permissions>

After you played enough with the AOSP image, you may want to return to the original retail state. Google has made it easy recently.

  • Open Chrome browser and go to “https://flash.android.com”
  • Allow the browser to connect to the device
  • Select the Pixel 6A factory image and find the desired build number to flash
  • Select “Wipe device” and “Lock bootloader” — optional
  • Click “Confirm” to start the process.

Source link