AOSP stands for Android Open Source Project. This is the complete android codebase which is available as an open source project. Recently, for one of my projects I required to build complete AOSP code and flash it into pixel 3. Google has provided good documentation around this listing complete steps to follow. But following these steps was not smooth and I faced some issues and hurdles. So thought to write a guide for anyone willing to do this on their own.
If you just want to use AOSP on your device without any modifications, then google has provided very easy way Android Flash Tool. You can flash stock android within few clicks.
Lets come to another case where as a developer you want to do some modifications in AOSP code. Then you need to build the code yourself and then flash it.
Linux machine preferably ubuntu. Building on windows or MacOS is not supported till now.
- A 64 bit environment
- At least 250GB of free disk space to check out the code and an extra 150 GB to build it
- At least 16 GB of available RAM is required, but Google recommends 64 GB.
- Multi-core processor. More the number of cores faster your build will be.
Downloading source code:
Tools required are git and repo. Install repo by below steps:
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repochmod a+x ~/bin/repo
Firstly, create a working directory.
Start code download by below commands. Here build tag will be as per device https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds .
repo init -u https://android.googlesource.com/platform/manifest -b <build tag>repo sync -j<n>
Here n is the number of parallel processes that will run. Best value of n is usually number of cores in your machine. Code download first time takes long depending on network and number of parallel processes.
If because of any reason repo sync fails then you can start it again. In my case repo sync failed twice while syncing /prebuilt package. So I deleted /prebuilt package and started repo sync again.
Very Important Step:
After code is downloaded completely and before proceeding to build it, this is a very important step. I missed it and lost 2 days trying to figure out why my device is not booting up. You have to install propriety binary files. These are vendor specific files and required for hardware support. Download driver images from here https://developers.google.com/android/drivers as per your device. You will get 2 zip files each having extract script. Run these scripts from root of your project and it will place files in /vendor folder.
Building source code:
EnvSetup script is used to set up environment variables and aliases.
To make clean build use clobber if you are building now for a different device. Clobber cleans up the out folder completely. For target choose it as per your device https://source.android.com/setup/build/building.html#choose-a-target.
make clobberlunch <target>
Use below command to increase heap size. This will speed up the build process. Then build the code. n is again equal to number of cores.
export _JAVA_OPTIONS=”-Xmx4g”m -j<n>
If everything goes well, in the end you will get a green success message.
[COLOR="SeaGreen"]#### build completed successfully (2:03:04 (hh:mm:ss)) ####[/COLOR]
Flashing on the device:
Now half the work is done as code is built successfully. Now the other half is flashing that build on the device and booting it successfully. So lets get started.
Lets setup the device first. Ensure you have latest platform-tools of Android SDK and platform-tools is added to your path. In order to flash a custom image we need to unlock the bootloader. That needs to be enabled first. To enable OEM unlocking on the device:
- In Settings, tap About phone, then tap Build number seven times.
- When you see the message You are a developer, tap the back button.
- Tap Developer options and enable OEM unlocking and USB debugging.
adb reboot fastboot
This will start the device in fastboot mode. After this we will check if fastboot drivers are installed
If device gets detected, its fine otherwise install fastboot drivers. Next step is to unlock the bootloader.
fastboot flashing unlock
Set ANDROID_PRODUCT_OUT. This will point to your flavor directory in out. Then flash the image. Flashing with -w will wipe all the existing user data.
fastboot flashall -w
Flashing will install various images into your device like system, boot, userdata etc.
After flashing is done successfully, your device will reboot.
Hopefully you should be able to see freshly baked custom ROM in your device !!!
Go try it out and let it me know how it goes in the comment section.
Reference : https://source.android.com/setup/start