[GUIDE][COMPLETE] All in one Android Development [NOOB FRIENDLY]

Search This thread

Anik_khan

Inactive Recognized Developer
Oct 13, 2014
259
2,700
Mymensingh, Dhaka
Hi All,

I and @akhilnarang decided to post this guide to help anyone wanting to learn building of Roms and kernels. We will be using Ubuntu 14.04 Trusty Tahr here.
You can find instructions to install it elsewhere, just remember rom building needs 64bit OS, minimum 4GB RAM, and 100GB space on HDD. Kernel can be built on less hardware, but will take long
Make sure you have fast net, as you have to download at least 16GB of ROM sources, or more kernel, only 1GB of sources max.
Be prepared to spend a lot of time downloading, learning, and fixing bugs :D

More RAM, or faster CPU, or SSD will speed up the process.
From our personal experiences we have found that this is the best, stable, up-to-date version of Ubuntu yet. This has come from my trial and errors, searching all over the web, and bits I picked up from here and there.

We will move Rom building part when we completed this step properly. Step by step we will learn compiling kernel, using Github, Gerrit, Cherry-picking.
At the end of the post I have linked a script by @akhilnarang which if run will automatically install all these programs for lazy people like us haha [emoji14]
If you not interested to do all procedure from below just use the script. Simply it will automatically configure the build environment.

NOTE-------- some of these packages may already be on your machine. Obviously, if you complete a step and you have one of these installed, the machine simply will not do anything. So...it will not hurt anything. Some of the packages are different from Ubuntu 12.04 and 13.04 (use these packages for Ubuntu 14.04 as many of the old ones have obsoleted - these are new replacements).

So if you set up your pc with Ubuntu we are good to go.
All code will be in code boxes like this:
Code:
$
means you have to run that command in the terminal (don't type the $).
All commands with sudo, gksudo, or gksu means it requires root permission.
It will ask for password.
Once you type, then in that terminal session it will not ask you for a few minutes again.
Any time you make changes to .bashrc or .profile file, make sure to run
Code:
$ source ~/.bashrc
$ source ~/.profile
Or exit and open new terminal else the changes will not take place

Setting up build environment:

Installing Java JDK

Note: You need JDK 7 when you willing to compile Lollipop. If you wanna compile KitKat or lower you need JDK 6.
Open terminal (CTRL + ALT + T)
Then execute the following commands in terminal one by one:

Download & install java

Code:
 $ sudo apt-get update && sudo apt-get install openjdk-7-jdk openjdk-7-jre
CHECK if java properly installed or not
Code:
$ java -version
If you get output like
Code:
java version "1.7.0_79"
on the first line then all is good :D

Now JDK is configured!

Note: Only for Ubuntu 14.04 or higher

Installing Required Packages

Open Terminal
Now execute this command:
Note: This will take some time, has to download about 500MB or so of packages
Code:
$ sudo apt-get update && sudo apt-get install git-core python gnupg flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.8-dev squashfs-tools build-essential zip curl libncurses5-dev zlib1g-dev  openjdk-7-jdk pngcrush schedtool \
 libxml2 libxml2-utils xsltproc lzop libc6-dev schedtool g++-multilib lib32z1-dev lib32ncurses5-dev lib32readline-gplv2-dev gcc-multilib liblz4-* android-tools-adb android-tools-fastboot ccache

Configuring USB Access

Now we need to set permissions so that our USB ports can detect our android devices [emoji14]
Type in the following commands, it will download a file with the permissions, give it permission to execute, and restart the service.
Code:
$ sudo curl --create-dirs -L -o /etc/udev/rules.d/51-android.rules -O -L https://raw.githubusercontent.com/snowdream/51-android/master/51-android.rules
$ sudo chmod 644   /etc/udev/rules.d/51-android.rules
$ sudo chown root. /etc/udev/rules.d/51-android.rules
$ sudo service udev restart
$ sudo killall adb
ADB & Fastboot are configured

Installing Repo Package

Open terminal and type:
Code:
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

Configure Git

Open terminal and type:
Code:
$ git config --global user.email ""
$ git config --global user.name ""
Git is configured.

Configure ccache

Open terminal and type:
Code:
$ gksu gedit ~/.bashrc
Enter these lines
Code:
export USE_CCACHE=1
export CCACHE_DIR=~/.ccache
Save & close

Open terminal & execute:
Code:
$ source .bashrc
$ ccache -M 50G

I usually use 50 gigs. You can use more if you have more space, or build for many devices

Make a GitHub account using the same email address you used for configuring git
Then follow these

So if you not interested in doing above all steps then simply use this script by my friend @akhilnarang
Open terminal and type:
Code:
$ sudo apt-get install git-core
then do
Code:
$ git clone https://github.com/akhilnarang/scripts.git
$ cd scripts
$ . build-environment-setup.sh

This script will install all required packages :D

Congrats :D

YOUR BUILD ENVIROMENT IS NOW SETUP :D

Now move onto #2nd post for Git and repo tutorial

To be continued :)
 
Last edited:

akhilnarang

Recognized Developer / Inactive RC
May 8, 2014
1,880
12,798
24
Pune
akhilnarang.dev
How To Use Git!
Now, to explain about git
Its a version control system, for code, like mercurial or subversion
Most android source code is either at AOSP
Most custom ROMs like CyanogenMod,Radium, host their code over at GitHub
So to start off, make a GitHub Account! :D
Here, you will find all the various repositories, full of code :p
All files are stored in repositories
When browsing a repository, you will see a button called fork
This will make a copy of it in your account, so you can edit it, and even submit changes for approval to the parent account
Now, to download the source of a repository onto your computer, we use git clone command
Code:
$ git clone https://github.com/akhilnarang/scripts
So we download the scripts from my github onto your PC, so we can run them
Now, if you added your ssh key as shown in bottom of post #1, then when cloning a repo from your account, while copying the link from bottom right corner, choose ssh link.
If you, when pushing(uploading)local changes, you will not be prompted for username and password, your ssh key will identify you :p.
After cloning, cd to the directory
Run
Code:
git remote -v
This will show a list of remotes
Default is origin, i.e. the link you have cloned from
Now once we make our changes locally
Code:
$ git add -A
This stages all modified/added/deleted files to be committed
Code:
$ git commit
This opens a text editor for you to write a commit message for your commit
Just give me brief description of your changes or something :v.
Then
Code:
$ git push <remote-name> HEAD:<branch>
If you have cloned, then remote will be origin
Branch can be checked by
Code:
git branch
, and also online from where you cloned
If you used https:// URL then you will be prompted for username and password to authenticate yourself
If git:// URL you can't push, you will need to use a https:// or ssh link
Another way to push can be
Code:
$ git push <link> HEAD:<branch>

Now say you want to make your own git repository of your files
Click on the new repository button on GitHub, give a name
You can do initial commit online, or locally
Follow the instructions it gives you
i.e. if you have all files ready (in a folder)
Code:
$ git init
$ git add -A
$ git commit
$ git push <link> HEAD:<branch>


A VERY important thing to be noted while using version control systems like git, is to preserve authorship of a commit
If someone else has made some changes to a file, and we want them, we should not copy the changes and commit, but should give them authorship either by cherry-picking(will be explained)
Or using this syntax
Code:
$ git commit --author="Author Name <authoremail@blah.com>"
Through this git will know the correct authors
Not giving proper authorship is known as kanging, not appreciated here on XDA,(or anywhere else for that matter), and can cause you to be banned :3
Anyway, on to cherry-picking
Go to your repository
Code:
$ git remote add blahblah link-to-url-you-want-commit-from
$ git fetch blahblah branch-commit-is-on
$ git cherry-pick Commit-sha1-hash
If you see a commit on GitHub, or GitLab, or BitBucket, there is a unique(to that repo) SHA for every commit to identify it
Fetch the correct repo as shown above, and paste the commit id
That will fetch the changes
However, if your local source is quite different from the source you cherry-picked from, it will conflict
If so your terminal will display it
then run
Code:
$ git status
Files shown in red color have conflicts
Open them with a text editor like Atom(my favorite), nano, gedit, vim, etc
Look for <<<<<<<
From there, the content till ======
is the original content
=======
till >>>>>>> *insert commit message here* is the new stuff
So check the commit, figure out what's wrong, fix it, save.
Then
Code:
$ git add -A
$ git commit
If no conflicts it will commit on its own and you just need to
Code:
$ git push
as explained earlier :v


Running
Code:
$ git log
will show you a list of commits :D, authors, and SHA
If for some reason you wanna remove some commit, use
Code:
$ git revert commit-SHA
Just like cherry-picks, it may have conflicts :p

Anyway, this is just a small guide to get started off
You can easily found out more by searching, and experimenting
I will add more stuff here soon
Enjoy with this much till then :p
 
Last edited:

Anik_khan

Inactive Recognized Developer
Oct 13, 2014
259
2,700
Mymensingh, Dhaka
Building CyanogenMod for those device which officially supported
If our build environment setted up successfully and after we learnt some Git and repo thing now we will concentrete on rom building :)
Now we have to Initialize Repo. We will try to build CyanogenMod.

Initialize the repo:

Code:
Code:
$ mkdir cm
$ cd cm
$ repo init -u git://github.com/CyanogenMod/android.git -b cm-13.0
$ repo sync -f --no-clone-bundle
For people who have already done a repo init:
Code:
$ cd cm
$ repo sync -f --no-clone-bundle
When prompted, enter your real name and email address.

Continue onto adding a device

~How To Add A Device To The List~

Find the github for your device you wish to add. (We will give Android One Devices(sprout4&sprout8) as an example

Navigate back to your home directory for building:
Code:
$ cd ~/cm
$ source build/envsetup.sh
Or
$ . build/envsetup.sh
That sets up your environment for building

Get your list of devices:

Code:
$ brunch

Choose your device from the list, till will fetch tree and dependencies if needed, sometimes vendor tree will need to be added manually (In case of CyanogenMod, rest all ROMs keep in dependencies)
Build will start,enjoy!

For users who like understanding>

Code:
. build/envsetup.sh
runs a shell script, which adds various environment variables,etc, and some commands.
Calling "brunch" actually runs the command
Code:
 breakfast $@ && mka bacon
(for CM)
$@ in shell script means all the parameters that have been passed, so if you passed brunch sprout, it would pass sprout
breakfast calls lunch, which selects the device, prepares the environment for building for it, fetches dependencies with the help of roomservice python script
"mka bacon"
This calls GNU make, and passes bacon as a parameters, to be "made"
mka is used instead of make, as it automatically uses all available threads of your CPU
If you have a quad core Intel, or octa core AMD, you could run make -j8 bacon
This is just the gist of it, reading through the envsetup.sh would make it clearer


If successful, you will get a zip in out/target/product/device
If not, check post #6
Most likely, you will find a solution there.
If not, please ask us in the thread, we would be happy to assist you :)

To be continued :)
 
Last edited:

akhilnarang

Recognized Developer / Inactive RC
May 8, 2014
1,880
12,798
24
Pune
akhilnarang.dev
Kernel Compilation
As many of you'll already know, kernel of an operating system
Android devices use a modified version of the linux kernel
Firstly, most important thing to note, in that the Linux Kernel is licensed under the GNU GPL (General Public License), which can be found here
Basically, you must always keep your work on the kernel open-source, and display the link to it when publishing your work on XDA Threads, etc
In order to do this, make sure you fork, or push the kernel source you're working on to your GitHub account so that you can push the changes you're making
Make sure you give proper authorship, as explained in post #2
If unable to, give credits to author in commit message, but don't kang please [emoji14]

Please try to have at least a basic knowledge of C, so that you can fix errors
Else, use Google before asking in thread, there's a huge chance someone has already gotten and solved it :v
So now, getting to work
I will give an example using Android One Device (sprout4&&sprout8)
The kernel source be found here(branch android-mediatek-lollipop-mr1 for 5.1,or something, not sure, check), and also here, branch cm-12.1 for 5.1 ROMs
So fork it to your GitHub account, or clone from AOSP and push to your git
I'd recommend the one from CM git, as it usually has small bugs fixed compared to stock
In my device's case, a whole lotta broken stuff is fixed, so I would take from cm
So start off, by installing all dependencies as shown in 1st post
Code:
$ git clone https://github.com/akhilnarang/android_kernel_mediatek_sprout -b cm-12.1 sprout-kernel
Replace the link with your own, ssh if you wish, as explained in #2
You must specify -b cm-12.1,as the default is cm-11.0, and you will end up will KitKat source!
The last parameter is optional, it saves it in that directory, in this case sprout-kernel
Now, all kernel source's have a file called a defconfig, or default configuration, that is device-specific
You will find it in arch/*your devices architecture*/configs
Your device may be arm, arm64, x86
Make sure you remember the name of the defconfig, you can rename it too [emoji14]
If you would like to give your kernel a name, simplest way would be to look for the CONFIG_LOCALVERSION in the defconfig
Make it something like
Code:
CONFIG_LOCALVERSION="-Your-Kernel-Name"
Anyway, so now you need a toolchain
For starting off, we will use GCC 4.8 from Google
For ARM devices
Code:
$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/
For aarch64/arm64
Code:
$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.8/
For x86 I'm not sure, go have a look here
Now remember where you saved it
Now open the file called "Makefile" in the root of kernel source
Look for a line that has ARCH?=
that dosen't start with a # (lines starting with # are comments)
And make it
Code:
ARCH ?= arm
or
Code:
ARCH ?= arm64
And look for CROSS_COMPILE ?=
make it
Code:
CROSS_COMPILE ?= path/to/downloaded/toolchain/bin/arm-eabi-
for arm devices
Code:
CROSS_COMPILE ?= path/to/downloaded/toolchain/bin/aarch64-android-linux-
for arm64 devices

This may differ on Custom toolchains, etc, you can find out about that by viewing the commits of different kernels, and going through various threads
I'm only gonna explain how to compile the kernel, not how to add stuff (at least for now)

If you wish now, you can refer to #2, and commit and push these changes [emoji14]

Now run
Code:
$ make cyanogenmod_sprout_defconfig
That's what my defconfig is called, yours maybe called something else [emoji14]
And now finally to start the build
First, check how many cores/threads your CPU has
A quad core Intel, an octa core AMD, will have 8
So if you have one, run
Code:
$ make -j8
This will compile the code on all 8 threads parallely
However this makes debugging errors harder
If you have an error, just run
Code:
 $ make
to reduce output and identify error
Anyway, the process generate a zImage in arch/arm/boot/zImage
or arch/arm64/boot/zImage
This is a binary file
If you check the zip of your favorite custom kernel, you will find one too
In most cases, you can simply replace it with this, update the updater-script, and flash :D
Most of these zips use anykernel format, meaning it utilizes ramdisk from existing kernel, so if you find such a zip for your device, you're good to go
Else unpack your boot.img and add this add the file boot.img-zImage, repack, flash
If you don't understand any of these terms [emoji14], please google about them first, and then if you are unable to find out ask here please :D
Enjoy :D
More stuff will be added soon
 
Last edited:

Anik_khan

Inactive Recognized Developer
Oct 13, 2014
259
2,700
Mymensingh, Dhaka
Uses of Local_manifests

Now we will learn what is Manifest. Its used for syncing source from Github,Bitbucket. Its driven via XML. Basically XML is a markup language. We can modify Rom's default.xml but it can cause of conflicts next time. So never think of editing default.xml
Now you can say how we sync our desired repo without modifying default manifest? There is a option called local manifests. By local manifests we can sync our desired repository. You can add,remove any project from default manifest without modifying default.xml
So now we will learn using local manifests-----

Open terminal and type
Code:
cd your source directory name/.repo
e.g: cd cm/.repo

Now create a new directory called local_manifests

Then type
Code:
cd local_manifests

Now type
Code:
nano localmanifest.xml

Now add whatever you want and save it via "ctrl+X"


Adding or Removing repo

Open localmanifest.xml then
Code:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>

  <remove-project name="CyanogenMod/android_frameworks_base" />

  <project path="frameworks/base" name="Anik1199/android_frameworks_base" remote="github" revision="cm-13.0"/>

This will remove project from CyanogenMod and sync from Anik1199's git.
When adding a new project that replaces an existing project, you should always remove that project before defining the replacement.
You can simply add a new project to the source code, such as when you want to add your own app to the build.

Note that when adding new projects, there are at least three parts defined:
• remote -- the name of the remote. this can be one that was defined in either the default manifest or local_manifest.xml.
• name -- the name of the git project-- for github it has the format account_name/project_name.
• path -- where the git repository should go in your local copy of the source code.
• revision -- (optional) which branch or tag to use in the repository. If this attribute is omitted, repo sync will use the revision specified by the <default ... /> tag in the default manifest.

After creating .repo/local_manifests.xml, you should be able to repo sync and the source code will be updated accordingly

In many Manifest.xml you ll find many stuff not needed by our device. Also there are files needed and that is not present in the default.xml.

Adding Projects to local_manifest.xml

So, to add projects to your local_manifest.xml for your device refer the following format that explains the same.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>

<project path=" directory in work tree" name="name of the repository in remote" remote="name of remote defined earlier" revision="branch of the repository" />

</manifest>

If you have any doubt? See my localmanifest.xml

Device Example Used Here : Android One (Sprout)
Code:
<manifest>
  <?xml version="1.0" encoding="UTF-8"?>
<manifest>
<!--Devices-->
  <project path="device/google/sprout4" name="CyanogenMod/android_device_google_sprout4" remote="github" revision="cm-13.0" />
<!--Devices commons-->
  <project path="device/google/sprout-common" name="CyanogenMod/android_device_google_sprout-common" remote="github" revision="cm-13.0" />
<!--Kernels-->
  <project path="kernel/mediatek/sprout" name="CyanogenMod/android_kernel_mediatek_sprout" remote="github" revision="cm-13.0" />
<!--Vendors-->
  <project path="vendor/google" name="TheMuppets/proprietary_vendor_google" remote="github" revision="cm-13.0" />
</manifest>

This will download your device Identity needed for Rom Building :)


Making CM Device Tree Compatible With Your Rom For Compiling​


We learnt how to initialize build environment,learnt some git,how to use local_manifest and learnt how to build CyanogenMod. Now we will learn how to compatiable CyanogenMod device tree for other Rom. We used Android One aka sprout as a reference. Now we will try to build another CyanogenMod based Rom for sprout.
I am using Team-Radium Rom as reference. So we can start......


There are 3 files in CM Tree Which Should Be Modified.
They are,

1) cm.mk

2) cm.dependencies

3) vendorsetup.sh

We have to edit this 3 files for building Team-Radium

1) Rename cm.mk to radium.mk


As You can see That WE HAVE JUST REPLACED CM WITH Radium

Now you all must be thinking why did we rename cm.mk to radium.mk

A Simple Answer is we go to the vendor folder and check the name and replace it.

So now you must be wondering about the product name ...as in the above image only the "cm" changes to "Radium" with the same logic of the vendor name

In this case we see that Radium is the vendor ...so we rename the cm.mk to radium.mk

this same rule applies to the content inside the radium.mk too ... we first make sure that the files are available in that location .

2) Now we have to move second file "cm.dependencies"

This file will be simply renamed by Vendor Name ...

In This Case "Radium" ....so we rename it to "radium.dependencies"

3) The Last And The Final Change Required Is In The "vendorsetup.sh"

Open vendorsetup.sh and rename cm as radium and save.

Thats it!! So simple i think :p

Happy building :)

Good luck!!! :good:
 
Last edited:

akhilnarang

Recognized Developer / Inactive RC
May 8, 2014
1,880
12,798
24
Pune
akhilnarang.dev
Last one in case ;)
Will soon add some common bugs and their fixes :p
If repo sync gives error like cannot overwrite work tree
then
Code:
$ repo sync --force-sync
For those with slow net
You can download highly compressed sources by @regalstreak and some others from here!
Unless its a one time build, i'd recommend getting the one with .repo folder, else you won't be able to easily update the source to the latest version!


Thanks guys like @OwnDroid @westcripp @##W4TCH0UT## @varun.chitre15 @CaptivateKing and many others who have inspired me about Android :D :D
 
Last edited:

nitin.chobhe

Recognized Contributor
Jan 3, 2013
8,299
19,956
India
Google Pixel 2 XL
Google Pixel 7 Pro
This is a nice guide... I think most of the people in India do not have high speed internet or continuous electricity for repo sync ;)

This puts limitations on using own PCs for ROM building. I have seen lots of people who are using rented servers for this purpose. These servers are located somewhere in Europe or USA and one can access them (or VMs on them) remotely. These servers have internet speeds in GBs so the repo sync does not take more than 30 minutes... :)

I've recently started learning about ROM building and I've started with Google Cloud. Google have 2 months free service (so interested guys can start the ROM building right away without thinking about the time required for downloading 15-20 GBs ;))

@Anik_khan may be you can think of adding this as a tip in the OP...

@akhilnarang thanks for helping me and be ready... I'll be coming with lots of questions ;)
 

corphish

Recognized Dev / Inactive Recognized Contributor
Apr 16, 2014
1,019
2,462
Durgapur
Add use of build scripts maybe.

P.S : Sometime I forget actual commands because of too much use of build scripts :D
 
  • Like
Reactions: Anik_khan

slim_guy

Senior Member
Nov 9, 2012
289
324
26
new mumbai
a small correction in installing java procedure, you need jdk 7 to build lollipop, you mentioned as jdk7 and higher but you can't build with jdk8. awesome guide so far, regards from me.

Sent from my XT1033 using Tapatalk
 
  • Like
Reactions: Anik_khan

neonkat

Senior Member
Aug 15, 2014
4,405
2,936
lost
Thanks guys..finally got a 'noob friendly guide'..:D

Sent from my HM 1SW using XDA Free mobile app
 

Frostyagent

Senior Member
Sep 3, 2012
98
21
Its really a nice guide, but i need your help as am stuck on how to add a device to aosp/CM source, whenever i tried to add a device, I follows all instructions but it always give me an error

Which would you like? [aosp_arm-eng] 7
build/core/product_config.mk:222: *** Can not locate config makefile for product "ha_i9500". Stop.

** Don't have a product spec for: 'ha_i9500'
** Do you have the right repo manifest?
 

ujwal.p

Senior Member
May 26, 2015
565
2,182
Bangalore
ujwalp15.github.io
Its really a nice guide, but i need your help as am stuck on how to add a device to aosp/CM source, whenever i tried to add a device, I follows all instructions but it always give me an error

Which would you like? [aosp_arm-eng] 7
build/core/product_config.mk:222: *** Can not locate config makefile for product "ha_i9500". Stop.

** Don't have a product spec for: 'ha_i9500'
** Do you have the right repo manifest?

Rename cm.mk to ha_i9500.mk

Sent from my A0001 using Tapatalk
 

Top Liked Posts

  • There are no posts matching your filters.
  • 48
    Hi All,

    I and @akhilnarang decided to post this guide to help anyone wanting to learn building of Roms and kernels. We will be using Ubuntu 14.04 Trusty Tahr here.
    You can find instructions to install it elsewhere, just remember rom building needs 64bit OS, minimum 4GB RAM, and 100GB space on HDD. Kernel can be built on less hardware, but will take long
    Make sure you have fast net, as you have to download at least 16GB of ROM sources, or more kernel, only 1GB of sources max.
    Be prepared to spend a lot of time downloading, learning, and fixing bugs :D

    More RAM, or faster CPU, or SSD will speed up the process.
    From our personal experiences we have found that this is the best, stable, up-to-date version of Ubuntu yet. This has come from my trial and errors, searching all over the web, and bits I picked up from here and there.

    We will move Rom building part when we completed this step properly. Step by step we will learn compiling kernel, using Github, Gerrit, Cherry-picking.
    At the end of the post I have linked a script by @akhilnarang which if run will automatically install all these programs for lazy people like us haha [emoji14]
    If you not interested to do all procedure from below just use the script. Simply it will automatically configure the build environment.

    NOTE-------- some of these packages may already be on your machine. Obviously, if you complete a step and you have one of these installed, the machine simply will not do anything. So...it will not hurt anything. Some of the packages are different from Ubuntu 12.04 and 13.04 (use these packages for Ubuntu 14.04 as many of the old ones have obsoleted - these are new replacements).

    So if you set up your pc with Ubuntu we are good to go.
    All code will be in code boxes like this:
    Code:
    $
    means you have to run that command in the terminal (don't type the $).
    All commands with sudo, gksudo, or gksu means it requires root permission.
    It will ask for password.
    Once you type, then in that terminal session it will not ask you for a few minutes again.
    Any time you make changes to .bashrc or .profile file, make sure to run
    Code:
    $ source ~/.bashrc
    $ source ~/.profile
    Or exit and open new terminal else the changes will not take place

    Setting up build environment:

    Installing Java JDK

    Note: You need JDK 7 when you willing to compile Lollipop. If you wanna compile KitKat or lower you need JDK 6.
    Open terminal (CTRL + ALT + T)
    Then execute the following commands in terminal one by one:

    Download & install java

    Code:
     $ sudo apt-get update && sudo apt-get install openjdk-7-jdk openjdk-7-jre
    CHECK if java properly installed or not
    Code:
    $ java -version
    If you get output like
    Code:
    java version "1.7.0_79"
    on the first line then all is good :D

    Now JDK is configured!

    Note: Only for Ubuntu 14.04 or higher

    Installing Required Packages

    Open Terminal
    Now execute this command:
    Note: This will take some time, has to download about 500MB or so of packages
    Code:
    $ sudo apt-get update && sudo apt-get install git-core python gnupg flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.8-dev squashfs-tools build-essential zip curl libncurses5-dev zlib1g-dev  openjdk-7-jdk pngcrush schedtool \
     libxml2 libxml2-utils xsltproc lzop libc6-dev schedtool g++-multilib lib32z1-dev lib32ncurses5-dev lib32readline-gplv2-dev gcc-multilib liblz4-* android-tools-adb android-tools-fastboot ccache

    Configuring USB Access

    Now we need to set permissions so that our USB ports can detect our android devices [emoji14]
    Type in the following commands, it will download a file with the permissions, give it permission to execute, and restart the service.
    Code:
    $ sudo curl --create-dirs -L -o /etc/udev/rules.d/51-android.rules -O -L https://raw.githubusercontent.com/snowdream/51-android/master/51-android.rules
    $ sudo chmod 644   /etc/udev/rules.d/51-android.rules
    $ sudo chown root. /etc/udev/rules.d/51-android.rules
    $ sudo service udev restart
    $ sudo killall adb
    ADB & Fastboot are configured

    Installing Repo Package

    Open terminal and type:
    Code:
    $ mkdir ~/bin
    $ PATH=~/bin:$PATH
    $ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    $ chmod a+x ~/bin/repo

    Configure Git

    Open terminal and type:
    Code:
    $ git config --global user.email ""
    $ git config --global user.name ""
    Git is configured.

    Configure ccache

    Open terminal and type:
    Code:
    $ gksu gedit ~/.bashrc
    Enter these lines
    Code:
    export USE_CCACHE=1
    export CCACHE_DIR=~/.ccache
    Save & close

    Open terminal & execute:
    Code:
    $ source .bashrc
    $ ccache -M 50G

    I usually use 50 gigs. You can use more if you have more space, or build for many devices

    Make a GitHub account using the same email address you used for configuring git
    Then follow these

    So if you not interested in doing above all steps then simply use this script by my friend @akhilnarang
    Open terminal and type:
    Code:
    $ sudo apt-get install git-core
    then do
    Code:
    $ git clone https://github.com/akhilnarang/scripts.git
    $ cd scripts
    $ . build-environment-setup.sh

    This script will install all required packages :D

    Congrats :D

    YOUR BUILD ENVIROMENT IS NOW SETUP :D

    Now move onto #2nd post for Git and repo tutorial

    To be continued :)
    25
    How To Use Git!
    Now, to explain about git
    Its a version control system, for code, like mercurial or subversion
    Most android source code is either at AOSP
    Most custom ROMs like CyanogenMod,Radium, host their code over at GitHub
    So to start off, make a GitHub Account! :D
    Here, you will find all the various repositories, full of code :p
    All files are stored in repositories
    When browsing a repository, you will see a button called fork
    This will make a copy of it in your account, so you can edit it, and even submit changes for approval to the parent account
    Now, to download the source of a repository onto your computer, we use git clone command
    Code:
    $ git clone https://github.com/akhilnarang/scripts
    So we download the scripts from my github onto your PC, so we can run them
    Now, if you added your ssh key as shown in bottom of post #1, then when cloning a repo from your account, while copying the link from bottom right corner, choose ssh link.
    If you, when pushing(uploading)local changes, you will not be prompted for username and password, your ssh key will identify you :p.
    After cloning, cd to the directory
    Run
    Code:
    git remote -v
    This will show a list of remotes
    Default is origin, i.e. the link you have cloned from
    Now once we make our changes locally
    Code:
    $ git add -A
    This stages all modified/added/deleted files to be committed
    Code:
    $ git commit
    This opens a text editor for you to write a commit message for your commit
    Just give me brief description of your changes or something :v.
    Then
    Code:
    $ git push <remote-name> HEAD:<branch>
    If you have cloned, then remote will be origin
    Branch can be checked by
    Code:
    git branch
    , and also online from where you cloned
    If you used https:// URL then you will be prompted for username and password to authenticate yourself
    If git:// URL you can't push, you will need to use a https:// or ssh link
    Another way to push can be
    Code:
    $ git push <link> HEAD:<branch>

    Now say you want to make your own git repository of your files
    Click on the new repository button on GitHub, give a name
    You can do initial commit online, or locally
    Follow the instructions it gives you
    i.e. if you have all files ready (in a folder)
    Code:
    $ git init
    $ git add -A
    $ git commit
    $ git push <link> HEAD:<branch>


    A VERY important thing to be noted while using version control systems like git, is to preserve authorship of a commit
    If someone else has made some changes to a file, and we want them, we should not copy the changes and commit, but should give them authorship either by cherry-picking(will be explained)
    Or using this syntax
    Code:
    $ git commit --author="Author Name <authoremail@blah.com>"
    Through this git will know the correct authors
    Not giving proper authorship is known as kanging, not appreciated here on XDA,(or anywhere else for that matter), and can cause you to be banned :3
    Anyway, on to cherry-picking
    Go to your repository
    Code:
    $ git remote add blahblah link-to-url-you-want-commit-from
    $ git fetch blahblah branch-commit-is-on
    $ git cherry-pick Commit-sha1-hash
    If you see a commit on GitHub, or GitLab, or BitBucket, there is a unique(to that repo) SHA for every commit to identify it
    Fetch the correct repo as shown above, and paste the commit id
    That will fetch the changes
    However, if your local source is quite different from the source you cherry-picked from, it will conflict
    If so your terminal will display it
    then run
    Code:
    $ git status
    Files shown in red color have conflicts
    Open them with a text editor like Atom(my favorite), nano, gedit, vim, etc
    Look for <<<<<<<
    From there, the content till ======
    is the original content
    =======
    till >>>>>>> *insert commit message here* is the new stuff
    So check the commit, figure out what's wrong, fix it, save.
    Then
    Code:
    $ git add -A
    $ git commit
    If no conflicts it will commit on its own and you just need to
    Code:
    $ git push
    as explained earlier :v


    Running
    Code:
    $ git log
    will show you a list of commits :D, authors, and SHA
    If for some reason you wanna remove some commit, use
    Code:
    $ git revert commit-SHA
    Just like cherry-picks, it may have conflicts :p

    Anyway, this is just a small guide to get started off
    You can easily found out more by searching, and experimenting
    I will add more stuff here soon
    Enjoy with this much till then :p
    24
    Kernel Compilation
    As many of you'll already know, kernel of an operating system
    Android devices use a modified version of the linux kernel
    Firstly, most important thing to note, in that the Linux Kernel is licensed under the GNU GPL (General Public License), which can be found here
    Basically, you must always keep your work on the kernel open-source, and display the link to it when publishing your work on XDA Threads, etc
    In order to do this, make sure you fork, or push the kernel source you're working on to your GitHub account so that you can push the changes you're making
    Make sure you give proper authorship, as explained in post #2
    If unable to, give credits to author in commit message, but don't kang please [emoji14]

    Please try to have at least a basic knowledge of C, so that you can fix errors
    Else, use Google before asking in thread, there's a huge chance someone has already gotten and solved it :v
    So now, getting to work
    I will give an example using Android One Device (sprout4&&sprout8)
    The kernel source be found here(branch android-mediatek-lollipop-mr1 for 5.1,or something, not sure, check), and also here, branch cm-12.1 for 5.1 ROMs
    So fork it to your GitHub account, or clone from AOSP and push to your git
    I'd recommend the one from CM git, as it usually has small bugs fixed compared to stock
    In my device's case, a whole lotta broken stuff is fixed, so I would take from cm
    So start off, by installing all dependencies as shown in 1st post
    Code:
    $ git clone https://github.com/akhilnarang/android_kernel_mediatek_sprout -b cm-12.1 sprout-kernel
    Replace the link with your own, ssh if you wish, as explained in #2
    You must specify -b cm-12.1,as the default is cm-11.0, and you will end up will KitKat source!
    The last parameter is optional, it saves it in that directory, in this case sprout-kernel
    Now, all kernel source's have a file called a defconfig, or default configuration, that is device-specific
    You will find it in arch/*your devices architecture*/configs
    Your device may be arm, arm64, x86
    Make sure you remember the name of the defconfig, you can rename it too [emoji14]
    If you would like to give your kernel a name, simplest way would be to look for the CONFIG_LOCALVERSION in the defconfig
    Make it something like
    Code:
    CONFIG_LOCALVERSION="-Your-Kernel-Name"
    Anyway, so now you need a toolchain
    For starting off, we will use GCC 4.8 from Google
    For ARM devices
    Code:
    $ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/
    For aarch64/arm64
    Code:
    $ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.8/
    For x86 I'm not sure, go have a look here
    Now remember where you saved it
    Now open the file called "Makefile" in the root of kernel source
    Look for a line that has ARCH?=
    that dosen't start with a # (lines starting with # are comments)
    And make it
    Code:
    ARCH ?= arm
    or
    Code:
    ARCH ?= arm64
    And look for CROSS_COMPILE ?=
    make it
    Code:
    CROSS_COMPILE ?= path/to/downloaded/toolchain/bin/arm-eabi-
    for arm devices
    Code:
    CROSS_COMPILE ?= path/to/downloaded/toolchain/bin/aarch64-android-linux-
    for arm64 devices

    This may differ on Custom toolchains, etc, you can find out about that by viewing the commits of different kernels, and going through various threads
    I'm only gonna explain how to compile the kernel, not how to add stuff (at least for now)

    If you wish now, you can refer to #2, and commit and push these changes [emoji14]

    Now run
    Code:
    $ make cyanogenmod_sprout_defconfig
    That's what my defconfig is called, yours maybe called something else [emoji14]
    And now finally to start the build
    First, check how many cores/threads your CPU has
    A quad core Intel, an octa core AMD, will have 8
    So if you have one, run
    Code:
    $ make -j8
    This will compile the code on all 8 threads parallely
    However this makes debugging errors harder
    If you have an error, just run
    Code:
     $ make
    to reduce output and identify error
    Anyway, the process generate a zImage in arch/arm/boot/zImage
    or arch/arm64/boot/zImage
    This is a binary file
    If you check the zip of your favorite custom kernel, you will find one too
    In most cases, you can simply replace it with this, update the updater-script, and flash :D
    Most of these zips use anykernel format, meaning it utilizes ramdisk from existing kernel, so if you find such a zip for your device, you're good to go
    Else unpack your boot.img and add this add the file boot.img-zImage, repack, flash
    If you don't understand any of these terms [emoji14], please google about them first, and then if you are unable to find out ask here please :D
    Enjoy :D
    More stuff will be added soon
    23
    Building CyanogenMod for those device which officially supported
    If our build environment setted up successfully and after we learnt some Git and repo thing now we will concentrete on rom building :)
    Now we have to Initialize Repo. We will try to build CyanogenMod.

    Initialize the repo:

    Code:
    Code:
    $ mkdir cm
    $ cd cm
    $ repo init -u git://github.com/CyanogenMod/android.git -b cm-13.0
    $ repo sync -f --no-clone-bundle
    For people who have already done a repo init:
    Code:
    $ cd cm
    $ repo sync -f --no-clone-bundle
    When prompted, enter your real name and email address.

    Continue onto adding a device

    ~How To Add A Device To The List~

    Find the github for your device you wish to add. (We will give Android One Devices(sprout4&sprout8) as an example

    Navigate back to your home directory for building:
    Code:
    $ cd ~/cm
    $ source build/envsetup.sh
    Or
    $ . build/envsetup.sh
    That sets up your environment for building

    Get your list of devices:

    Code:
    $ brunch

    Choose your device from the list, till will fetch tree and dependencies if needed, sometimes vendor tree will need to be added manually (In case of CyanogenMod, rest all ROMs keep in dependencies)
    Build will start,enjoy!

    For users who like understanding>

    Code:
    . build/envsetup.sh
    runs a shell script, which adds various environment variables,etc, and some commands.
    Calling "brunch" actually runs the command
    Code:
     breakfast $@ && mka bacon
    (for CM)
    $@ in shell script means all the parameters that have been passed, so if you passed brunch sprout, it would pass sprout
    breakfast calls lunch, which selects the device, prepares the environment for building for it, fetches dependencies with the help of roomservice python script
    "mka bacon"
    This calls GNU make, and passes bacon as a parameters, to be "made"
    mka is used instead of make, as it automatically uses all available threads of your CPU
    If you have a quad core Intel, or octa core AMD, you could run make -j8 bacon
    This is just the gist of it, reading through the envsetup.sh would make it clearer


    If successful, you will get a zip in out/target/product/device
    If not, check post #6
    Most likely, you will find a solution there.
    If not, please ask us in the thread, we would be happy to assist you :)

    To be continued :)
    16
    Last one in case ;)
    Will soon add some common bugs and their fixes :p
    If repo sync gives error like cannot overwrite work tree
    then
    Code:
    $ repo sync --force-sync
    For those with slow net
    You can download highly compressed sources by @regalstreak and some others from here!
    Unless its a one time build, i'd recommend getting the one with .repo folder, else you won't be able to easily update the source to the latest version!


    Thanks guys like @OwnDroid @westcripp @##W4TCH0UT## @varun.chitre15 @CaptivateKing and many others who have inspired me about Android :D :D