[HOW TO] Setup Ubuntu 16.04 LTS Xenial Xerus to Compile Android ROMs

— For a ready-to-go Virtual Machine based on these instructions, check out this post!

With a new version of Ubuntu comes an update to my guide for setting up a build environment to compile Android ROMs. The aim of this is to simplify the configuration process and teach a little bit about the command line. It’s ten easy steps! Of course, this could be boiled down to a two-liner with some CLI wizardry, but that isn’t the point.

Follow the directions in order. Copy and paste the code, or better yet, type it out for an improved learning experience.

This guide applies to all variations of Ubuntu 16.04 LTS Xenial Xerus 64 bit. Do not use the 32 Bit version.

Also, PAY CLOSE ATTENTION when to use “sudo” and when to not.

Much thanks goes out to Google, ProTekk, Canonical, and everyone else that I read a random paragraph here and snippet there.


1) Unless it’s a completely fresh Ubuntu installation, the wrong version of Java might be installed. Let’s fix that first.

The command below makes sure you’re starting with a clean slate. Copy and paste it into a terminal (command prompt) window:

sudo apt-get remove openjdk-* icedtea-*

If necessary, follow the on-screen instructions to remove any stray Java versions. Otherwise, move on to the next step.


2) Install the main build tools with this command:

sudo apt-get install git ccache automake lzop bison gperf build-essential zip curl zlib1g-dev g++-multilib python-networkx libxml2-utils bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev squashfs-tools pngcrush schedtool dpkg-dev liblz4-tool make optipng maven libc6-dev linux-libc-dev gcc-multilib g++-5-multilib libssl-dev


3) “repo” communicates with git servers for all that precious source code. The next command will grab it:

mkdir ~/bin && curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo && chmod a+x ~/bin/repo


4) Use a favorite text editor to open ~/.bashrc (or equivalent) – nano is easy to use:

nano ~/.bashrc


5) At the very bottom (use the Page Down key) paste this code to a new line:

export PATH=~/bin:$PATH
export USE_CCACHE=1


6) Save it. In nano that would be Ctrl-O and then Enter. Then Ctrl-X to exit back to a prompt. Restart the shell:

source ~/.bashrc


7) In the terminal, make a folder to download the Android source code. The commands below will keep it in the home directory, but if limited on space it’s ok to create it somewhere else. Faster is better, i.e. NVMe would be best, USB (even 3.0) will be comparatively slow. Here we go:

mkdir ~/android
cd ~/android


8) Now initialize the repo. Decide the flavor of Android to build, i.e. AOKP, CyanogenMod, AOSP etc. For the purposes of this tutorial, here’s the command for Cyanogenmod 13 (Marshmallow) :

repo init -u https://github.com/CyanogenMod/android.git -b cm-13.0


9) Time to get the source, many gigabytes of downloading await. Don’t worry, it’s automated.

repo sync

Check back periodically every hour or so. It all depends on how fast the connection is.


10) Now, for Java. Some ROMs (Android Lollipop / CM 12.1 and below) require OpenJDK 7. Marshmallow / CM 13 and above require OpenJDK 8. If building for reallllllly old devices, OpenJDK 6. Here’s the easiest way to install on Ubuntu 16.04.


Add the PPA for OpenJDK:

sudo add-apt-repository ppa:openjdk-r/ppa


Then, install the appropriate version of Java. In this case we’re going to use 7, because it’s popular. Simply replace openjdk-7-jdk with 6 or 8 as needed.

sudo apt-get update && sudo apt-get install openjdk-7-jdk



That’s it! Everything should be ready to go.

Donate to the new hardware fund here. Thanks for reading!


  1. Openjdk 7 has been removed from 16.04 in these days so apt-get install not work anymore

    but I need v7 to fix some error compiling rom with v8.
    How can I manual install openjdk 7?

    1. I just used these exact directions to set up a fresh build environment on Xubuntu Core 16.04 final beta and OpenJDK7 installed fine for me.

      Maven was really the only major change, as CM13 now requires it for builds. AOSP is encouraging OpenJDK8 but you’ll run into problems with some ROMs using anything but 7.

      What is the output when you try sudo apt-get install openjdk-7-jdk ? You also may want to start by removing any previous versions, although update-alternatives works fine too.

  2. Hi,

    it is a big problem that Ubuntu 16.04 does not have openjdk-7-jdk package,
    it is needed to build lollipop, marshmallow and many other projects,
    I really do not understand the reason, the package was there ready and working.

    Mauro Rossi
    Android-x86 developer (not much) and tester, bug fixer (a lot!)

    1. Hi Mauro,

      I’m trying to build Android-x86 Marshmallow (http://www.android-x86.org/releases/releasenote-6-0-r1) using a custom 4.8.4
      kernel for my Acer Switch 10 SW5-012 which works perfectly with Ubuntu-MATE (I compiled both the 32 and the 64 bits).
      I’ve cloned the kernel from:

      I still need to figure out how to merge correctly the _defconfig files in order to have a decent .config to compile the kernel.
      My latest attempt produced a merged config file where all the CONFIG_*=m have become CONFIG_*=y. A quasi-monolithic
      kernel that didn’t compile (/link) at all: many dup symbols.

      Any help would be appreciated!

    1. Yep, I have the instructions for OpenJDK 8 there at the bottom if anyone is working on AOSP.

      I left the guide using OpenJDK7 by default because many ROMs are still relying on it. For example, as of writing this CM13 builds with JDK 7.


  3. Pure Newbie here.
    I am also getting ” package openjdk 7 jdk has no installation candidate ”
    Just installed the jdk 8 since I cannot find jdk 7

    1. Thanks for the heads up.

      OpenJDK 7 apparently existed in the 16.04 beta repos, but now that it’s officially released, they appear to have removed it!

      I’ll make the necessary changes.

    1. try this
      sudo add-apt-repository ppa:openjdk-r/ppa
      sudo apt-get update
      sudo apt-get install openjdk-7-jdk

  4. I’m on make 4.1-6. how can I downgrade to make 2.81 so I can get faster compile times again?

  5. I am getting below error while building PureNexus ROM on Ubuntu 16.04. Can any one help on this

    Makefile:130: recipe for target ‘sub-make’ failed make[1]: *** [sub-make] Error 2make[1]: Leaving directory ‘/home/narasimha/pure nexus/kernel/lge/hammerhead build/core/tasks/kernel.mk:220: recipe for target ‘TARGET_KERNEL_BINARIES’ failed make: *** [TARGET_KERNEL_BINARIES] Error 2make: *** Waiting for unfinished jobs….

  6. Import includes file: out/host/linux-x86/obj/STATIC_LIBRARIES/libslang_intermediates/import_includes
    Import includes file: out/host/linux-x86/obj/STATIC_LIBRARIES/libLLVMBitWriter_2_9_intermediates/import_includes
    Import includes file: out/host/linux-x86/obj/STATIC_LIBRARIES/libLLVMBitWriter_2_9_func_intermediates/import_includes
    Import includes file: out/host/linux-x86/obj/STATIC_LIBRARIES/libLLVMBitWriter_3_2_intermediates/import_includes
    Export includes file: frameworks/compile/slang/RSSpec.mk — out/host/linux-x86/obj/EXECUTABLES/llvm-rs-cc_intermediates/export_includes
    Notice file: frameworks/compile/slang/NOTICE — out/host/linux-x86/obj/NOTICE_FILES/src//lib/libslang.a.txt
    Notice file: frameworks/compile/slang/NOTICE — out/host/linux-x86/obj/NOTICE_FILES/src//bin/llvm-rs-cc.txt
    Notice file: external/clang/NOTICE — out/host/linux-x86/obj/NOTICE_FILES/src//lib/libclang.so.txt
    make: *** No rule to make target `out/target/common/obj/JAVA_LIBRARIES/com.android.gallery3d.common2_intermediates/javalib.jar’, needed by `out/target/common/obj/APPS/Gallery2_intermediates/classes-full-debug.jar’. Arrêt.
    make: *** Attente des tâches non terminées….
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: Some input files use unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

  7. I recommend for number 9 running: repo sync -c -j2, if you are not downloading the master branch.

    -c makes it download only what you specified in ‘repo init’ after -b, such as cm-13.0.

    I use -j2 because I have a 2 core processor.
    The number after -j denotes the amount of streams you will be using.
    You can’t do anything else with the computer while it is syncing but it goes a lot faster.

  8. I’m trying to compile a KitKat ROM and it’s telling me to use Oracle JDK and not OpenJDK.

  9. I haven’t found a solution for this problem yet and I get it no matter what version of Ubunyu or Mint I have installed.
    Now I’m on Mint 18. I formatted the HDD too many times in order to do a completely clean repo sync of Pure Nexus but I end up in the same spot. I’m trying to compile formy Nexus 4. I set up everything according to the instructions but no success.
    I hope someone can help me. Any suggestion is appreciated.
    Here’s what’s stopping my compiling process:

    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsState.java:198: OP_WIFI_CHANGE cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsState.java:199: OP_BLUETOOTH_CHANGE cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsState.java:200: OP_NFC_CHANGE cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsState.java:201: OP_DATA_CONNECT_CHANGE cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsState.java:219: OP_BOOT_COMPLETED cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/notification/OtherSoundSettings.java:96: VOLUME_ADJUST_SOUNDS_ENABLED cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsDetails.java:75: MODE_ASK cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsDetails.java:89: MODE_ASK cannot be resolved or is not a field
    ERROR: /home/funny-bunny/PN/packages/apps/Settings/src/com/android/settings/applications/AppOpsDetails.java:206: The method isStrictOp(int) is undefined for the type AppOpsManager
    build/core/java.mk:643: recipe for target ‘/home/funny-bunny/PN/out/target/common/obj/APPS/Settings_intermediates/with-local/classes.dex’ failed
    make: *** [/home/funny-bunny/PN/out/target/common/obj/APPS/Settings_intermediates/with-local/classes.dex] Error 41
    make: *** Waiting for unfinished jobs….

    #### make failed to build some targets (02:56:31 (hh:mm:ss)) ####

  10. Hello, Newbie here. I need to be able to install jdk6, but im not sure about how to go about it. Since its been removed like 7 has, i just cant use the commands to have it installed. Also, this will be my first rom to compile.

  11. Hi,

    trying everything out with the windows bash. It’s not an issue in the guide that’s occuring. It’s an incompatibility of the Windows bash with the ghc compiler. Some packages needed to be installed manually but the ghc compiler outputs ghc-pkg: timer_create: Function not implemented when I install the package. Anyone else facing this issue? Is it possible to compile ghc timer_create independent? It’d awesome if this bash is fully working soon as I don’t want to use VMs at all and dual boot doesn’t really statisfy me as well. Thanks for your help 😉

  12. I’m having issues building after updating my build machine to Ubuntu 16.04:

    Checking API: checkapi-current
    out/target/common/obj/PACKAGING/public_api.txt:20: error 5: Added public field android.Manifest.permission.BACKUP
    out/target/common/obj/PACKAGING/public_api.txt:81: error 5: Added public field android.Manifest.permission.INVOKE_CARRIER_SETUP
    out/target/common/obj/PACKAGING/public_api.txt:105: error 5: Added public field android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE
    out/target/common/obj/PACKAGING/public_api.txt:115: error 5: Added public field android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST

    You have tried to change the API from what has been previously approved.

    To make these errors go away, you have two choices:
    1) You can add “@hide” javadoc comments to the methods, etc. listed in the
    errors above.

    2) You can update current.txt by executing the following command:
    make update-api

    To submit the revised current.txt to the main Android repository,
    you will need approval.

  13. Any solution Please for the following error while building CM14.1 for Lenovo K3 Note (aio_otfp) from Ubuntu 16.04 64bit sysytem.

    make: execvp: /bin/bash: Argument list too long
    make: *** [/media/lavu/Backup/out/cm/target/product/aio_otfp/module-info.json] Error 127

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.