Guide to Compiling Android on Ubuntu 16.04 (Xenial Xerus) -- OpenJDK7 Installation on 16.04

Another Ubuntu release, another set of instructions to get Android source to compile.



This only applies to the x64 (64 Bit version). It will not compile on the i386 (32 Bit version).

Ubuntu has opted to dump OpenJDK7 in the Xenial Repositories. This just means we will need to add a custom PPA to install JDK7.

Why go with OpenJDK7 and not OpenJDK8?

Well, OpenJDK7 is required to build most popular AOSP 6.0.x ROM Sources as of right now, some examples include: DU, PN, Any Specific AOSP Tag, OmniROM, and Android-x86.

In order to build anything >=4.4.x you will need Oracle Java 6.

The only use case for OpenJDK8 would be those who only build CM (13+), PacROM (or any other CM derivative for that matter), or the 'master' branch of AOSP.


Follow these instructions exactly (don't just throw 'sudo' around either, it is only to be used in specific cases):


1) Remove all traces of Java:

  • sudo apt-get remove openjdk-* icedtea-* icedtea6-*


2) Add Xenial OpenJDK7 PPA & Fetch the new packages index:
  • sudo add-apt-repository ppa:openjdk-r/ppa && sudo apt-get update


3) Install all currently available updates to ensure no packages are broken:
  • sudo apt-get upgrade && sudo apt-get dist-upgrade


6) Install OpenJDK7 and all Android Build dependencies:
  • sudo apt-get install adb fastboot openjdk-7-jdk git ccache automake lzop bison gperf build-essential zip curl zlib1g-dev zlib1g-dev:i386 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 python-mako python3-mako python python3 syslinux-utils google-android-build-tools-installer

# To install OpenJDK8, replace 'openjdk-7-jdk' ==> 'openjdk-8-jdk'
# To install Oracle Java 6, replace 'openjdk-7-jdk' ==> 'oracle-java6-installer'
# You can install all three Java versions, you will just need to select the default using the 'update-alternatives' command below



7) Remove all unnecessary packages:
  • sudo apt-get autoremove


8) Make a user accessible folder, and add it to path:
  • mkdir ~/bin && echo "export PATH=~/bin:$PATH" >> ~/.bashrc


9) Enable CCACHE to speed up builds:
  • echo "export USE_CCACHE=1" >> ~/.bashrc


10) Restart Bash
  • source ~/.bashrc


11) Configure Git (Replace with your Name & Email Address):

  • git config --global user.name "John Doe"
  • git config --global user.email johndoe@example.com


12) Download Repo, make it executable, and put it in our local $PATH folder:

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


13) Make a folder for your ROM source of choice (in this case 'android'), and initialize the repo:
  • mkdir ~/android
  • cd ~/android
  • repo init -u UrlOfManifestofROM.git -b BranchName


14) Sync the Repo:
  • repo sync -j4 --force-sync


15) Setup your build enviroment:
  • . build/envsetup.sh


16) Choose here:

# If you want to fetch device dependencies, and your ROM officially supports the target device -- goto step 16) a)

# If you want to build using local device tree, repo, and kernel directories -- goto step 16) b)


16) a) Breakfast
  • breakfast RomTag_DeviceCodeName-BuildType

# I'll use DirtyUnicorns on as an example

#'breakfast du_angler-userdebug' (Builds DU for Nexus 6P with userdebug, unless you know what you are doing, use userdebug)

# If you don't know your device codename, Google it, or just run 'breakfast', and look for it in the menu


16) b) Lunch
  • lunch RomTag_DeviceCodeName-BuildType

# I'll use DirtyUnicorns on as an example

#'lunch du_angler-userdebug' (Builds DU for Nexus 6P with userdebug, unless you know what you are doing, use userdebug)

# If you don't know your device codename, Google it, or just run 'lunch', and look for it in the menu



17) Build the ROM:
  • mka -j8 bacon

# mka is a refined make.

# -j8 is a variable number, if you have an Intel processor, take the number of cores and multiply by two, so, on my Quad-Core i5, 4x2=8. As noted by +Drew Walton, the 'mka' command auto analyses cpu availabillity, so the '-jx' arguement is not necessary unless you are building on a device where you want limited resources to be used (I have a server, where I am only allowed to use 4 cores to build, so 2 x 4 due to hyperthreading).

# bacon is most custom ROM's target for a flash-able .zip archive format, if you get an error like "No Target to Make: bacon", try 'make -j8 otapackage'.


18) Collect the ROM:
  • adb push ~/android/out/target/product/ROMNAME-DATE.zip /sdcard/

19) Flash ROM & Enjoy!



NOTES:
- Some ROMs don't sync in vendor repos by default. You'll need to do this by fetching your OEM's vendor from https://github.com/TheMuppets and cloning it into ~/PathToROMSource/vendor/OEMName. You can also do this via local manifest. Look at this one as an example. but I won't be explaining it here: https://github.com/adt1-dev/local_manifests/tree/cm-13.0.

- Getting random Java or Host Binary (build output shows it building in the 'out/host' directory) related errors? Try a lower -j#, or, just exclude that part (it will default to -j1, which will take considerably longer).

- To change your default Java version, use these two commands (Make sure to use them both! Mix and matching Java versions will not go well!):

sudo update-alternatives --config java

# If building LP (Lollipop) - MM (Marshmallow): Select the option for 'java-7-openjdk-amd64'
# If building DN (Donut) - KK (KitKat): Select the option for 'java-6-oracle-amd64'
# If building CM (13+), Pac (Or any other CM derivative), or the 'master' branch of AOSP: Select the option for 'java-8-openjdk-amd64'

sudo update-alternatives --config javac

# If building LP (Lollipop) - MM (Marshmallow): Select the option for 'java-7-openjdk-amd64'
# If building DN (Donut) - KK (KitKat): Select the option for 'java-6-oracle-amd64'
# If building CM (13+), Pac (Or any other CM derivative), or the 'master' branch of AOSP: Select the option for 'java-8-openjdk-amd64'



NOTE: Some AOSP ROM Source trees have been updated to build with OpenJDK8, so, feel free to use it when you can, though, it isn't mandatory.

Getting Random HOST ART Library Errors?

A common error going around right now is the generic 'libart.so' failure to build due to a linker error. This is due to a recent update in the BinUtils package in the Ubuntu and Debian repositories that AOSP hasn't accounted for yet (CM already merged the fix). To fix this, you need to cherry-pick two commits:
  • cd ~/PathToROMSource
  • cd art/build/
  •  git remote add cm https://github.com/CyanogenMod/android_art.git
  • git fetch cm
  • git cherry-pick e5c6b049f3c716be60bc82d79c44c451f49b4ad5
  • git cherry-pick 0299788b7a974841aa3324a573fbf04f49a4f23c
  • # Thats it! Have fun building your ROM of choice from source!

Comments

  1. I think

    git remote add https://github.com/CyanogenMod/android_art.git

    should read

    git remote add cm https://github.com/CyanogenMod/android_art.git

    ReplyDelete
    Replies
    1. Replied to the wrong comment earlier on. Yes, it should have, I edited it. Thank you!

      Delete
  2. Replies
    1. took a mental break from GS4 SCHi545 VRUGOF1 troubleshooting & Verizon's really getting paid for nothing actually ^_^ which is how I ended up here. I just kept clicking links and reading from your side bar. Your incredibly insightful ( & yes, very encouraging to know someone else will devote thought to solving a mystery--because it's there and being obviously ignored, which makes it all the more intriguing to discover WHY) post/tutorial/blog on unblocking Samsung's S4 security/bootloader issue. Plus, I like your writing style. This is all new to me but it's fascinating and I am getting tons of valuable info because of people like you-- knowledge sharing and relevancy of topics covered. Thank you.

      Delete
    2. Thank you! I appreciate the nice comments you made. I use these blogs to keep my thoughts straight as well, so they benefit me too. Haha.

      Delete
  3. Could you do a kernel turorial detailed as great this? TIA :D

    ReplyDelete
  4. Thank you so much! I think @Rachael_Burns summed it up on the thanks so I'll just mash f6, lol. Thanks again my dude :D

    ReplyDelete
  5. yes very good tutorial and all new developers stepping in to create rom from source for their device or roms they like tutorials like this are much appreciated. thank you and keep up good work. we all appreciate it.

    ReplyDelete

Post a Comment

Popular posts from this blog

Breaking Samsung's Security

A rant about OEM lazieness as it pertains to Proprietary Files