FORUMS

[ULTIMATE GUIDE] [A-Z] Using Android Source Code/ How to Compile

373 posts
Thanks Meter: 1,088
 
By Ubuntuz, Senior Member on 15th July 2013, 06:27 PM
Post Reply Email Thread
Using Android Source Code/How to Compile
So you want to compile Android? It doesn't matter if it's AOSP, CyanogenMod, Paranoid Android or whatever - this will help.

Getting Started

Intro
Android is great. It's fast, stable, easy to use.. oh, and it's open source. However, if you want to build your own versions - you'll need to learn a little bit.

Linux
Throughout this guide you will be using Linux, and if you don't even know what that is then please just stop now. If you do, well done! You're 1 step closer!

Git
Git is the project management software by the same person that made Linux, Linus Torvalds. It's really popular with open source devs so of course it is used with android! It's easy to use and is managed into repos. Repos also have branches which are mainly used for managing different versions of the code.

CyanogenMod
Ah, the great Cyanogen. Cyanogen was the man who kicked all this off! He created the ever so popular CyanogenMod ROM which helped speed up old devices. CyanogenMod has evolved into a worldwide project to help improve Android with after-market firmware and also provide materials for create these ROMs.

The Android Source Code
Google uploads all of it's Android source code to it's own git server into repos with specific names - here are some examples:
  • Device Tree - android_device_*vendor*_*codename* (android_device_htc_shooteru for HTC Evo 3D)
  • Kernel - android_kernel_*vendor*_*board* (android_kernel_htc_msm8660)
  • Frameworks - android_frameworks_*part* (android_frameworks_av)
The list goes on and on. You should get used to these because you are going to be using them a lot (assuming you will actually give back to the community ).

Requirements
Compiling Android is only possible on Linux 64-bit (typically) or maybe even OS X if you want a challenge. All the commands listed in this guide will be suited for Debian based distros (mainly Ubuntu), but if you have some experience with Linux, you can try changing them for use on other distros.
  • Ubuntu 12.04(.2) or BBQLinux (not debian) running on your computer OR in VirtualBox
  • At least 4GB of RAM
  • Dual-Core is nice
  • ~30GB of disk space for 1 project and a few builds
  • A brain
  • You will also need these packages installed:
Quote:

sudo apt-get install openjdk-6-jdk python git gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev x11proto-core-dev \
libx11-dev libreadline6-dev libgl1-mesa-glx \
libgl1-mesa-dev g++-multilib mingw32 tofrodos \
python-markdown libxml2-utils xsltproc zlib1g-dev

This command also needs to be run on Ubuntu 12.04(.2):
Quote:

sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

Secondly, the Android SDK is needed:
Code:
# Android tools
export PATH=${PATH}:~/android-sdk/tools
export PATH=${PATH}:~/android-sdk/platform-tools
export PATH=${PATH}:~/bin
  • Also add this to your .profile:
Code:
PATH="$HOME/android-sdk/tools:$HOME/android-sdk/platform-tools:$PATH"
Run the command 'android' to check for updates. The Android SDK is now installed!

Last thing to do is run:
Quote:

gksudo gedit /etc/udev/rules.d/51-android.rules

and add this:
Code:
#Acer
SUBSYSTEM=="usb", ATTR{idVendor}=="0502", MODE="0666"

#ASUS
SUBSYSTEM=="usb", ATTR{idVendor}=="0b05", MODE="0666"

#Dell
SUBSYSTEM=="usb", ATTR{idVendor}=="413c", MODE="0666"

#Foxconn
SUBSYSTEM=="usb", ATTR{idVendor}=="0489", MODE="0666"

#Garmin-Asus
SUBSYSTEM=="usb", ATTR{idVendor}=="091E", MODE="0666"

#Google
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666"

#HTC
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666"

#Huawei
SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", MODE="0666"

#K-Touch
SUBSYSTEM=="usb", ATTR{idVendor}=="24e3", MODE="0666"

#KT Tech
SUBSYSTEM=="usb", ATTR{idVendor}=="2116", MODE="0666"

#Kyocera
SUBSYSTEM=="usb", ATTR{idVendor}=="0482", MODE="0666"

#Lenevo
SUBSYSTEM=="usb", ATTR{idVendor}=="17EF", MODE="0666"

#LG
SUBSYSTEM=="usb", ATTR{idVendor}=="1004", MODE="0666"

#Motorola
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0666"

#NEC
SUBSYSTEM=="usb", ATTR{idVendor}=="0409", MODE="0666"

#Nook
SUBSYSTEM=="usb", ATTR{idVendor}=="2080", MODE="0666"

#Nvidia
SUBSYSTEM=="usb", ATTR{idVendor}=="0955", MODE="0666"

#OTGV
SUBSYSTEM=="usb", ATTR{idVendor}=="2257", MODE="0666"

#Pantech
SUBSYSTEM=="usb", ATTR{idVendor}=="10A9", MODE="0666"

#Philips
SUBSYSTEM=="usb", ATTR{idVendor}=="0471", MODE="0666"

#PMC-Sierra
SUBSYSTEM=="usb", ATTR{idVendor}=="04da", MODE="0666"

#Qualcomm
SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", MODE="0666"

#SK Telesys
SUBSYSTEM=="usb", ATTR{idVendor}=="1f53", MODE="0666"

#Samsung
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666"

#Sharp
SUBSYSTEM=="usb", ATTR{idVendor}=="04dd", MODE="0666"

#Sony Ericsson
SUBSYSTEM=="usb", ATTR{idVendor}=="0fce", MODE="0666"

#Toshiba
SUBSYSTEM=="usb", ATTR{idVendor}=="0930", MODE="0666"

#ZTE
SUBSYSTEM=="usb", ATTR{idVendor}=="19D2", MODE="0666"
Save it and run:
Quote:

sudo chmod a+r /etc/udev/rules.d/51-android.rules

Wow. We done alot there... what did we do?
  1. First, we downloaded the packages that we need to compile android.
  2. Secondly, we installed the Android SDK. This gives us adb, fastboot etc. We will go into detail about this later.
  3. Last of all, we added in vendor IDs for USB to make sure connecting to our devices works.
The Following 39 Users Say Thank You to Ubuntuz For This Useful Post: [ View ] Gift Ubuntuz Ad-Free
15th July 2013, 06:28 PM |#2  
Ubuntuz's Avatar
OP Senior Member
Thanks Meter: 1,088
 
More
Grabbing what we Need
Downloading Repo

You're all setup, time to download the sources for what you want to build.

To do this we will be using a tool made by Google called 'repo'. This was made by Google to easily manage loads of git repos easily. You can download it with this:
Quote:

mkdir ~/bin - create a directory for the repo binary
PATH=~/bin:$PATH - add it to the executable path
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo - download it
chmod a+x ~/bin/repo - give it permission to run

Organize yourself!

Now, we need to create a folder for our sources. This can be called what you want, but in this example I'll use ~/android_source:
Quote:

cd ~/ - move into the home folder
mkdir android_source - create the source code directory
cd android_source - move into it

Grabbing 'dem sources

That's looking good, now you have to make a decision. What do you want to compile? AOSP? CM? AOKP? PA?
Once you know, you need to initialize it in your folder:

For AOSP:
Quote:

repo init -u https://android.googlesource.com/platform/manifest -b android-4.2.2_r1.2

For CM:
Quote:

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

For PA:
Quote:

repo init -u git://github.com/ParanoidAndroid/manifest.git -b jellybean

For AOKP:
Quote:

repo init -u git://github.com/AOKP/platform_manifest.git -b jb-mr1

Great! Get ready the second longest part... grab a sandwich or make a coffee, this one is gonna take a little while (depending on your internet connection):
Quote:

repo sync

This downloads the hundreds of git repos filled with the android source code.

Set it up for your device
This is the trickiest part as far as getting the sources go, and it's still not that hard, we need to grab the device specific code.

Using the CyanogenMod wiki, find your device. On the device's page you will find links to the device tree and kernel source on the CyanogenMod github. Using these commands, download them:

Device Tree:
Quote:

git clone *URL TO DEVICE TREE* -b jellybean device/*VENDOR*/*CODENAME*

Kernel:
Quote:

git clone *URL TO KERNEL* -b jellybean kernel/*VENDOR*/*BOARD*

These commands use git to clone the repositorys into their respective folders. The -b argument tells it what branch to use

Some devices also have '*-common' repo. You'll have to use google for these.

Most devices have some proprietary files that are not open-source (grr). To obtain these we need to plugin with USB and run the following commands:
Quote:

cd device/*VENDOR*/*CODENAME* - move into the device folder
./extract-files.sh - get the proprietary files

The Following 24 Users Say Thank You to Ubuntuz For This Useful Post: [ View ] Gift Ubuntuz Ad-Free
15th July 2013, 06:28 PM |#3  
Ubuntuz's Avatar
OP Senior Member
Thanks Meter: 1,088
 
More
The looonngggeeessstt part

I hope you have some hours to spare and a decent cooling system, this is the resource hungry and long part...

Setting up for the ROM

To get started, setup your environment:
Quote:

. build/envsetup.sh

BUILD THE ROM!

Now get ready, run brunch to configure your build and being building:
Quote:

brunch *CODENAME*

Go outside for a while, this ones gonna take a few hours (depending on your system). Don't worry though, second builds and so on will take less time

brunch is the equivalent of running 'breakfast *CODENAME* & mka'. Breakfast configures the build and mka (a quicker version of make) automatically builds executable programs.

Speed it up

You can speed up your builds by using CCACHE. To do this, edit your .bashrc and add this:
Code:
export USE_CCACHE=1
Then punch in some commands:
Quote:

export USE_CCACHE=1
export CCACHE_DIR=~/.ccache
prebuilts/misc/linux-x86/ccache/ccache -M 50G

The suggested cache size is 50GB to 100GB


If you get errors, check the help section and/or google it. If it succeeded - well done You can find a zip in out/target/product/*CODENAME* that you can flash.

Setting up for the kernel (WIP)

Similar to the ROM, we need to setup our environment:
Quote:

export ARCH=arm - set our architecture
export CROSS_COMPILE=~/android/kernel/toolchains/arm-eabi-linaro-4.6.2/bin/arm-eabi- - set our toolchain

BUILD THE KERNEL! (WIP)

Set the defconfig:
Quote:

make <your_config_name>_defconfig

And start compiling!
Quote:

make

The Following 16 Users Say Thank You to Ubuntuz For This Useful Post: [ View ] Gift Ubuntuz Ad-Free
15th July 2013, 06:50 PM |#6  
Ubuntuz's Avatar
OP Senior Member
Thanks Meter: 1,088
 
More
nearly forgot, one more
The Following User Says Thank You to Ubuntuz For This Useful Post: [ View ] Gift Ubuntuz Ad-Free
4th August 2013, 05:30 PM |#7  
i compiled the kernel and boot up...
everything works fine except wifi & bt :/
i even replaced my new kernel modules with the old ones but still nothing...
what should i do ?
20th September 2013, 06:05 AM |#8  
Senior Member
Thanks Meter: 546
 
More
50 or so tutorials, hands down the best one out there.
Thanks

Just one clarification
When building roms which have a modified kernel (i.e. Vanir, SaberMod etc) are the toolchain/additions built even if using cm standard kernel tree or should it be modified accordingly as the one present in the sources repo?
export ARCH and export CROSS_COMPILE are to be used when building standalone kernel projects?
16th October 2013, 02:30 PM |#9  
shazzl's Avatar
Senior Member
Flag Banting
Thanks Meter: 230
 
More
If my phone doesn't have device tree how can I built it...
16th October 2013, 03:40 PM |#10  
lozohcum's Avatar
Senior Member
Flag Internety
Thanks Meter: 1,747
 
More
we want to know, how to bring new android versions to our devices, or at least how to build AOSP of already supported (by vendor)android version
19th November 2013, 10:24 PM |#11  
ShinnRa's Avatar
Junior Member
Flag Gdynia
Thanks Meter: 1
 
Donate to Me
More
The link to the repo results in 404 - Not found i think they've moved it somewhere... And is it only me or http://android.git.kernel.org/ does not exists (connection timeout)
Post Reply Subscribe to Thread

Tags
android, code, compile, guide, source

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes