########################### # # # MAY 31, 2021 UPDATE # # # ########################### This guide is no longer being actively maintained or updated, as I have not built an Android ROM since 2017 or even used an Android device as my daily driver since 2019. I am leaving it up as it has been useful for many but you might want to check out some more updated guides, such as those below: https://source.android.com/setup/build/building https://wiki.archlinux.org/title/Android#Building https://wiki.lineageos.org/build_guides.html #################################### # # # BUILDING ANDROID FROM SOURCE # # # #################################### In this guide, I will go over how to build Android on your Linux machine. This particular tutorial will focus on Ubuntu flavors but this should work with any version of Linux; I include a reference for Arch users that is already done. Your version MUST be 64-bit however. I will leave the installation of that up to you, Google is a wonderful resource. If you don't have a good computer but still want to build, check out this thread on XDA: http://forum.xda-developers.com/chef-central/android/guide-how-to-build-rom-google-cloud-t3360430 Formatting!!! If something has a $ in front of it, that is a command you need to run in your terminal window. Don't include the $ or the space that follows it. ####################################### # Step One: set up your environment # ####################################### You have two options here, the lazy way or the non-lazy way. Automatic way (recommended!): -- Grab a repo with some useful scripts, and run the needed one $ sudo apt-get install git-core $ git clone https://github.com/akhilnarang/scripts $ cd scripts $ ls $ bash setup/ Run the script corresponding to your Linux Distribution: arch-manjaro.sh - Arch based distros android_build_env.sh - Ubuntu 14, Ubuntu 16, Ubuntu 18, Mint 19 Manual way: -- Grab Java 8: $ sudo apt-get update $ sudo apt-get install openjdk-8-jdk $ sudo apt-get install openjdk-8-jre -- Install build tools $ sudo apt-get install bc bison build-essential ccache curl flex \ g++-multilib gcc-multilib git-core gnupg gperf imagemagick lib32ncurses5-dev \ lib32z-dev libc6-dev-i386 libgl1-mesa-dev libssl-dev libx11-dev \ libxml2-utils unzip x11proto-core-dev xsltproc zip zlib1g-dev NOTE: Arch Linux users, use the Arch wiki page to install your packages then come back here: https://wiki.archlinux.org/index.php/Android#Building_Android ###################################### # Step Two: Configure Repo and Git # ###################################### NOTE: If you have any problems with the below commands, try running as root: $ sudo -s Git is an open source version control system which is incredibly robust for tracking changes across repositories. Repo is Google's tool for working with Git in the context of Android. More reading if you are interested: https://source.android.com/source/developing.html Run these commands to get repo all working (only needed if you did the manual method of setup above): $ curl https://storage.googleapis.com/git-repo-downloads/repo > repo $ chmod a+x repo $ sudo install repo /usr/local/bin $ rm repo Then run these commands to get git all working: $ git config --global user.name "Your Name" $ git config --global user.email "you@example.com" ##################################### # Step Three: Download the source # ##################################### First, create a folder for your source: $ mkdir ~/ (eg. mkdir ~/DU or ~/PN-Layers) $ cd ~/ When you go to build a ROM, you must download its source. All, if not most, ROMs will have their source code available on Github. To properly download the source, follow these steps: -- Go to your ROM's Github (e.g. http://github.com/DirtyUnicorns) -- Search for a manifest (usually called manifest or android_manifest). -- Go into the repo and make sure you are in the right branch (located right under the Commits tab). -- Go into the README and search for a repo init command. If one exists, copy and paste it into the terminal and hit enter. -- If one does not exist, you can make one with this formula: repo init -u .git -b . For example: $ repo init -u https://github.com/DirtyUnicorns/android_manifest.git -b o8x -- After the repo has been initialized, run this command to download the source: $ repo sync --force-sync -j$( nproc --all ) -- This process can take a while depending on your internet connection. ########################## # Step Four: Build it! # ########################## Here's the moment of truth... time to build the ROM! This process could take 15 minutes to many hours depending on the speed of your PC but assuming that you have followed the instructions so far, it should be smooth sailing. Before you compile, you may also consider setting up ccache if you can spare about 50GB. If not, skip this section. ccache is a compiler cache, which keeps previously compiled code stored so it can be easily reused. This speeds up build times DRASTICALLY. Open your bashrc or equivalent: $ nano ~/.bashrc - Append export USE_CCACHE=1 to the end of this file then hit ctrl-X, Y, and enter. $ source ~/.bashrc After that, run this command if you used the manual method of setup above $ prebuilts/misc/linux-x86/ccache/ccache -M 50G (or however much you want). Run this command if you used the automatic method of setup above $ ccache -M 50G Since Android 10, the way of using ccache has changed: $ export USE_CCACHE=1 $ export CCACHE_EXEC=$(command -v ccache) After this, load up the compilation commands: $ . build/envsetup.sh Then, tell it which device you want to make and let it roll: $ breakfast OR lunch $ mka bacon NOTE: Some ROMs may use their own bacon command, read their manifest as they will usually outline this. Others may not use mka, use make -j$( nproc --all ) If you get an error here, make sure that you have downloaded all of the proper vendor files from your ROM's repository. Once you tell it mka bacon, the computer will start building. You will know that it is done when you see a message saying make completed and telling you where your flashable zip is located. Whenever you build again, make sure you run a clean build every time by placing this command in between the other two: $ make clobber That's it! You successfully compiled a ROM from scratch :) By doing this, you control when you get the new features, which means as soon as they are available on Github. After this, you may even want to start contributing, one of the greatest things about Android and open source software in general. ################# # Jack issues # ################# For those of you who are having jack issues (like saying you ran out of memory), follow these steps. Type this into your terminal, substituting the # with how many GBs of RAM you have: $ export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx#g" Then go into the root of the source folder and type the following: $ ./prebuilts/sdk/tools/jack-admin kill-server $ ./prebuilts/sdk/tools/jack-admin start-server This will restart the jack server to reflect your new heap limit. ############# # Support # ############# If you run into any issues while building, you can use the following chat room for assistance (be sure to read the rules): https://t.me/AndroidBuildersHelp This chat is only for build errors or to get the build to compile. After you have successfully built the ROM, for any required assistance, use this chat https://t.me/AndroidBringup #################### # Special thanks # #################### @Mazda-- for spearheading the Dirty Unicorns project, his dedication to open source, and helping me out at random points during compilation. @BeansTown106 for the various advice and guidance he has provided plus his contributions to the open source community through the Pure Nexus Project. @akhilnarang for his build environment scripts. @npjohnson for the Ubuntu 16.04 Java 7 instructions.