How to Build a Custom ROM from Android Open Source Project

When you build Android from AOSP, sometimes the process stops with errors like “soong bootstrap failed” or “ninja: job failed” after you sync the sources and start the build. This usually means the build system crashed, often because there isn’t enough memory, or some build settings are not set up correctly.

The most common reason is not enough RAM. Computers with less than 16GB of RAM often fail during the Soong step. Other reasons include setting the -j value too high, not having swap memory, or limits in Docker.

Some manufacturers provide open-source resources or developer tools, while others keep their code private. Here’s a brief list of open-source resources from some major manufacturers:

With that introduction out of the way, let’s move forward under the assumption that we’re building a ROM for a basic, “vanilla” Android experience, specifically for a Google Pixel device. Once you learn the basics here, you’ll be ready to start creating customized versions for other manufacturers’ devices!

Requirements for this Guide:

Setting Up Your Build Environment

Let’s start by installing an Android emulator on your Linux computer. Even if you have a Google Pixel XL device, it’s safest to use an emulator to test your new ROM before trying it on your actual device. My personal recommendation is Genymotion, so I’ll guide you through that. However, you can browse other options in this “Best Android Emulators” guide, since many are also compatible with Linux.

First, go to the Genymotion website, create a free account, verify it via email, and download the appropriate installer for your Linux computer.

Open a terminal window and run these commands (replace “xxxxx” with the version in your file name):

chmod +x genymotion-xxxxx.bin
./genymotion-xxxxx.bin

When prompted, press Y to create the Genymotion directory. Then, type:

cd genymotion && ./genymotion

The installer will guide you through the remaining steps. When you reach the Add Virtual Devices window, select “Pixel XL” under Device Model, then complete the setup. You can launch this virtual device, which works much like having a Pixel XL on your desktop.

Next, let’s install Python:

sudo apt-get install python

Now you need to set up the Java Development Kit (JDK). In the terminal, enter:

sudo apt-get update
sudo apt-get install openjdk-8-jdk

At this point, you need to configure your Linux system to allow USB device access. This usually means adding a USB permissions file. Download the required 51-android.txt file, update it with your Linux username, and copy it to the appropriate folder as the root user. Now, plug in your device via USB for the new rules to take effect.

Place this updated file at:

Downloading the Android Source

The AOSP is stored on Git, so we use a tool called Repo to work with it.

First, set up a bin folder in your home directory by running:

mkdir ~/bin
PATH=~/bin:$PATH

Next, download the Repo tool:

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

Now, create a directory where you’ll do your work, and navigate to it:

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

Configure Git with your name and email – ideally use a Gmail address that you access often, since it’s needed for code review tools like Gerrit:

git config --global user.name "Your Name"
git config --global user.email you@gmail.com

Initialize Repo to pull the latest AOSP master manifest from Git:

repo init -u https://android.googlesource.com/platform/manifest

If that works, Repo will be initialized in your working directory, and you’ll see a “.repo” folder. Download the complete Android source tree by running:

repo sync

Building the Android Source

Now it’s time for those hardware binaries we mentioned before. Go to the AOSP drivers page and download the Pixel XL binaries for your Android version (for example, Android 7.1.0 NDE63P). Download both the vendor image and the hardware components. Extract the archives and run their self-extracting scripts from the root directory. Install the binaries at the root of the WORKING_DIRECTORY you created earlier.

In the Linux terminal, run:

make clobber
source build/envsetup.sh

Set your build target:

lunch aosp_marlin-userdebug
setpaths
make –j4

Congratulations, you’ve now built an Android ROM from source! To test it in the emulator, run:

emulator

Take some time to explore your new ROM in the emulator. As you’ll see, a pure “vanilla” AOSP experience is very basic—which is why manufacturers often add their own features and visual styles on top. While you can flash this ROM to a device, it probably won’t have many enhancements, and will look very plain.

Typically, manufacturers start with AOSP, add their own hardware files, change up the UI, add logos, and customize features. This will likely be your next step as well.

Stay tuned for the second part of this guide, where we’ll go through adding custom fonts, themes, and a boot animation to your new ROM!

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.

Comments

24
    NS
    Naman Pratap Singh Jun 25, 2017

    Kevin, I really liked the article. I really doubt if I can make one custom rom for my device. Can you please make a Cyanogen rom or Bliss Pop Rom for my device? I’m using Lyf Earth 1 with stock lollipop. This phone has outdated OS. Kindly make one rom for this device. I can share the stock boot image and other resources if you need.

      BJ
      Bob jones Oct 2, 2020

      Stop being lazy he has a job he cant make random people custom roms make it yourself.

    MI
    Misterxtc Jul 2, 2017

    Thanks for the write up on this. I’m trying to build for Sailfish. I get to the last command “make-j4” and get this error…

    build/core/base_rules.mk:390: warning: ignoring old commands for target `out/target/product/sailfish/system/lib/libmm-qcamera.so’
    [ 99% 895/896] glob vendor/*/*/Android.bp
    ninja: error: unknown target ‘–j4’
    10:25:38 ninja failed with: exit status 1
    build/core/main.mk:21: recipe for target ‘run_soong_ui’ failed
    make: *** [run_soong_ui] Error 1

    #### make failed to build some targets (01:59 (mm:ss)) ####

    Any ideas or suggestions? I also tried make-j8 and that errored out as well. But just running make with no arguments appears to be building. I’m running Linux Mint 18.1.

      TC
      Tony Celentano Jul 30, 2017

      Does just running ‘make’ without arguments produce a functional ROM? How many hardware threads are you running in your computer? You could for example try make -j16
      or make -j32.

      Also from the error log it appears ninja: error: unknown target ‘–j4’ is using an endash not a regular – hypen. Try it with just a regular – not the endash

      Reply
    MS
    Mordy Shahar Jul 23, 2017

    Hi,
    Thanks for that article – do you know if I can connect webcam (UVC camera) to the emulated machine? I think genymption supports it but I;m not sure.

    Thanks.

      TC
      Tony Celentano Jul 30, 2017

      It should work in Genymotion, you need to press CTRL+3 to bring up the camera configuration and then turn the camera button ON.

    EI
    Eli Irvin Aug 5, 2017

    Hey there! Great guide! When i built mine i was built in the file system of android and not into a zip file…
    Any idea what would cause that?

      TC
      Tony Celentano Aug 7, 2017

      Hi Eli,

      Can you please describe a bit more? You mean your ROM was not built as a .zip file, and all of its files ended up in a /system folder?

        EI
        Eli Irvin Aug 7, 2017

        https://uploads.disquscdn.com/images/d8b34cfca2e45fba75c84726b34acec1b8e32c8acd73485ceaef7534f0a66624.png

        It doesnt output as a .zip it comes out as a folder with all the other folders in it (/system, /vendor/ etc.)
        Not sure why lol (im building for sailfish)

          TC
          Tony Celentano Aug 8, 2017

          Ah I see, thats normal because we only used the -make command. Try this:
          # first, build the target-files .zip
          % . build/envsetup.sh && lunch tardis-eng
          % mkdir dist_output
          % make dist DIST_DIR=dist_output
          […]
          % ls -l dist_output/*target_files*

          and

          % ./build/tools/releasetools/ota_from_target_files
          dist_output/tardis-target_files.zip ota_update.zip

          You might also want to see from official AOSP documentation: https://source.android.com/devices/tech/ota/sign_builds

        EI
        Eli Irvin Aug 7, 2017

        https://uploads.disquscdn.com/images/d8b34cfca2e45fba75c84726b34acec1b8e32c8acd73485ceaef7534f0a66624.png

        It outputs as the devices partition folders and such. (/system, /vendor, etc) instead of a zip

    DM
    developer m Aug 25, 2017

    Hi great tutorial thanks!! how can i add my custom boot animation to AOSP project waiting for that tutorial.

      TC
      Tony Celentano Oct 4, 2017
    AV
    ANILESH VIJY Sep 10, 2017

    I had lenovo yoga tab 3 8″,where should i find its device-specific hardware proprieties.i had looked the link provided by you of lenovo.any other method to grab this from our device itself.
    your post is good.thanks for this.

      TC
      Tony Celentano Oct 4, 2017

      Hi Anilesh,

      Unfortunately it seems like people have been hounding Lenovo to release the source code for the Yoga Tab 3 (and other tablets), and Lenovo has not responded. So it seems devices like the Tab 3 8″ are not included in their open source projects. If you hunt around unofficially, you may be able to find the hardware binaries from another source.

      Reply
    DA
    Developer A Nov 28, 2018

    Hi thanks for the Tutorial.i have build the code and choose genymotion to run.when I give adb devices it showing the device but when I give fast boot devices its not showing.i have enabled USG debugging also what is the error here? Is there any binaries or drivers to be installed for genymotion? .how to flash system image in genymotion.please help me out.

    OL
    Olariu Feb 8, 2019

    unable to locate package libwxgtk2.8-dev ubuntu 18.10 what to do?

      SW
      Shane Warren Author Sep 20, 2019

      Check this out: “libwxgtk3.0-dev in Jessie”

      Reply
    11
    114 157 166 145 40 171 157 165 Jun 24, 2019

    Wait … so if you just want to “build” a custom OS, you just have to adjust the hardware binaries ?
    If so, can I do that and flash gapps with it and then I will have the latest stock android ?? “I guess the camera wont work”.
    @tonycelentano:disqus “sry for asking on a late forum”

    PJ
    Pieter Willem Jordaan Sep 3, 2019

    Where can I find mediatek’s hardware binaries/source? I have a MT6735V/WA based board

    SW
    Shane Warren Author Sep 5, 2019

    MediaTek doesn’t release their source code to the community

    MS
    Michael Schneider Mar 20, 2020

    I’m trying this myself. Following your instructions, I believe we also need to add in the kernel and the device files with ‘cd device/google && git clone https://android.googlesource.com/device/google/marlin-kernel && git clone https://android.googlesource.com/device/google/marlin

    Reply
    AH
    Alfin Haydar Hilmi Aug 3, 2020

    Hi,
    while building, i have this errorninja: build stopped: subcommand failed.what to do?

      SW
      Shane Warren Author Aug 4, 2020

      This can occur due to a number of reasons. It is best that you search online for your exact scenario and then diagnose your computer from there.