Build Custom ROM from Source
Ready to go beyond flashing and start building? Compiling Android from source is the ultimate customization - you control every line of code.
Fair warning: this is advanced territory. But if you've always been curious how those ROMs are made, this guide is for you.
Requirements
Hardware
Minimum specs for building Android:
- RAM: 16GB minimum (32GB+ recommended)
- CPU: 4+ cores (8+ recommended)
- Storage: 300GB+ free space (SSD strongly recommended)
- Internet: Fast connection for downloading 50GB+ sources
Software
- OS: Ubuntu 20.04+ or other compatible Linux distro
- Patience: First sync can take hours
Step 1: Set Up Build Environment
Install Ubuntu Dependencies
sudo apt update
sudo apt install bc bison build-essential ccache curl flex \
g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev \
lib32readline-dev lib32z1-dev liblz4-tool libncurses5 libncurses5-dev \
libsdl1.2-dev libssl-dev libxml2 libxml2-utils lzop pngcrush rsync \
schedtool squashfs-tools xsltproc zip zlib1g-dev openjdk-11-jdk python3
Install Repo Tool
Google's repo tool manages multiple git repositories:
mkdir -p ~/bin
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
# Add to PATH
echo 'export PATH=~/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Configure Git
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Set Up ccache
ccache dramatically speeds up rebuilds:
echo 'export USE_CCACHE=1' >> ~/.bashrc
echo 'export CCACHE_EXEC=/usr/bin/ccache' >> ~/.bashrc
source ~/.bashrc
# Set cache size (50GB recommended)
ccache -M 50G
Step 2: Initialize Repository
Create Build Directory
mkdir -p ~/android/lineage
cd ~/android/lineage
Initialize Repo
For LineageOS 21 (Android 14):
repo init -u https://github.com/LineageOS/android.git -b lineage-21.0 --git-lfs
For AOSP:
repo init -u https://android.googlesource.com/platform/manifest -b android-14.0.0_r1
Step 3: Sync Source Code
This downloads ~50GB of source code:
repo sync -c --force-sync --no-clone-bundle --no-tags -j$(nproc --all)
Flags explained:
-c: Only sync current branch-j$(nproc --all): Use all CPU cores- First sync: 1-4 hours depending on internet
If sync fails, just run again - it resumes.
Step 4: Add Device Trees
Required Repositories
To build for a device, you need:
- Device tree: Device-specific configuration
- Kernel source: Linux kernel for device
- Vendor blobs: Proprietary files
Using Local Manifests
Create .repo/local_manifests/ folder:
mkdir -p .repo/local_manifests
Create manifest file (example for device "bacon"):
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<project name="device_oneplus_bacon"
path="device/oneplus/bacon"
remote="github"
revision="lineage-21" />
<project name="kernel_oneplus_bacon"
path="kernel/oneplus/bacon"
remote="github" />
<project name="vendor_oneplus_bacon"
path="vendor/oneplus/bacon"
remote="github" />
</manifest>
Sync again to get device files:
repo sync -c --force-sync -j$(nproc --all)
Step 5: Configure Build
Set Up Environment
source build/envsetup.sh
Select Device
For LineageOS:
breakfast bacon
# or
lunch lineage_bacon-userdebug
For AOSP:
lunch aosp_walleye-userdebug
Build Types
| Type | Purpose |
|---|---|
| user | Production, limited debugging |
| userdebug | Debug + user, most common |
| eng | Full debugging, slowest |
Step 6: Build!
Start Build
mka bacon -j$(nproc --all)
For AOSP:
make -j$(nproc --all)
Build Time
Expect:
- First build: 2-8 hours (depending on hardware)
- Subsequent builds with ccache: 30min - 2 hours
Output
ROM zip appears in:
out/target/product/bacon/lineage-21.0-xxxxxxxx-UNOFFICIAL-bacon.zip
Making Modifications
Where to Make Changes
| Change | Location |
|---|---|
| System apps | packages/apps/ |
| Framework | frameworks/base/ |
| System UI | frameworks/base/packages/SystemUI/ |
| Settings | packages/apps/Settings/ |
| Kernel | kernel/[vendor]/[device]/ |
Example: Add System Feature
Edit framework file:
nano frameworks/base/core/res/res/values/config.xml
Rebuild affected module:
mma SystemUI # Rebuild SystemUI only
Or full rebuild:
mka bacon
Common Build Errors
"Jack server error"
# Kill existing jack server
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server
Out of Memory
# Limit jack memory
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
Or reduce parallel jobs:
mka bacon -j4 # Instead of -j$(nproc)
Missing Dependencies
# Sync again
repo sync -c --force-sync
# Check local manifests
Speed Tips
Use SSD
HDD builds are painfully slow. SSD or NVMe makes huge difference.
ccache is Essential
After first build, ccache speeds up rebuilds dramatically:
ccache -s # Check cache statistics
Parallel Jobs
Match to CPU cores, but consider RAM:
- 16GB RAM: -j4 to -j8
- 32GB RAM: -j8 to -j16
- 64GB+ RAM: -j$(nproc --all)
Build Server
Consider cloud build servers:
- Google Cloud
- AWS EC2
- Dedicated build machines
Cleaning Builds
Clean Output
Remove built files (keep source):
make clean
Deep Clean
Remove all output:
make clobber
Remove ccache
ccache -C # Clear cache
Sharing Your ROM
Create Release
- Build final zip
- Test thoroughly
- Generate checksums:
sha256sum yourrom.zip > yourrom.zip.sha256
Publish
- XDA Developers forum
- GitHub releases
- Telegram channels
Documentation
Include:
- Changelog
- Known issues
- Installation instructions
- Credits
Learning Path
Start Simple
- Build existing ROM for your device
- Make small changes, rebuild
- Learn where everything is
Then Advance
- Port ROM to new device
- Create device trees
- Fix device-specific bugs
- Build custom kernel
Resources
- LineageOS Wiki
- AOSP documentation
- XDA Developers forums
- r/AndroidDev
Summary
Building ROM:
1. Set up Ubuntu with dependencies
2. Install repo tool
3. repo sync sources (~50GB)
4. Add device trees
5. source build/envsetup.sh
6. lunch device-userdebug
7. mka bacon
8. Wait...
9. ROM in out/target/
Building from source is rewarding. You understand Android deeply and can make any modification imaginable. Start the journey!
Keywords: build custom rom, compile android, lineageos build, aosp source, android development