How to build Android from source

Introduction

Building Android from source is a simple, straightforward task. By default, building the Android source results in an installable rootfs directory structure, along with .img files which can be used with the included Android Emulator (more on that later).

Most of the instructions found here can also be found on the Android Source page. This how-to exists to further explain or clarify the exact procedure.

All instructions assume you are running Linux on the host machine. For Windows or Mac OS X, check the Android Source page.

Requirements

The Android Source page has exact instructions on installing the required packages on Ubuntu. I will repeat the basic requirements here:

  • Python 2.4 or newer. Python 3.x is not compatible with the 2.x. Most distributions come with python preinstalled or available, so check your package manager.
  • JDK 5.0, update 12 or higher. JDK 6.0 is incompatible, and cannot be used to build from source. Some distributions do not include JDK 5.0, so you must download from java.sun.com. Check below for instructions on how to install manually.
  • git 1.5.4 or newer. Again, most distributions have git available, so check your package manager.
  • flex, bison, gperf, gcc and g++. All of these are used to compile the source itself. Most distributions have a package installing all of these automatically, such as “build-essential” on Ubuntu. Check your package manager.

Installing JDK 5.0 manually.

To install JDK 5.0 manually, you must go to the Java download page. Download JDK 5.0 Update XX. You will be asked to register, just put your information and a valid e-mail address. Your download link will arrive by e-mail. Download the regular .bin file, not the .rpm.bin file.

The file you downloaded should be named something like this: “jdk-1_5_0_21-linux-i586.bin”. Open a terminal in the same location as the file, make it executable with “chmod +x jdk-1_5_0_21-linux-i586.bin”, and run it with sh: “sh jdk-1_5_0_21-linux-i586.bin”.

Scroll down and accept the licence agreement. It should start extracting files.

I installed the files in /usr/local, because it makes it easier to keep track of them and remove them if I want. Go into the generated directory. Inside there are folders like “bin”, “include”, “sample” and files like “COPYRIGHT”, “src.zip” and so on.

To install the required files, we move them to the correct folder inside /usr/local. Open a terminal in the generated directory and run:

mv bin/* /usr/local/bin/
mv include/* /usr/local/include/
mv lib/* /usr/local/lib/

You might have to add “sudo” at the start of every line if you want to actually move the files.

If /usr/local/bin is in your PATH, you're all done! Check by running “echo $PATH” in a terminal. If it's not, add this line your ~/.bashrc file:

export PATH=$PATH:/usr/local/bin

You might also need to export the JAVA_HOME variable pointing to the “java” binary. Edit ~/.bashrc and add this line:

export JAVA_HOME=/usr/local/bin/java

You're done! You're now ready to download the Android source code.

Installing and configuring 'repo'

Managing the entire source code is complex, so the Android people created a script to help people download and manage the Android source code called 'repo'. First, we download 'repo' and make it executable:

wget http://android.git.kernel.org/repo
chmod +x repo

repo has to be in the PATH, so either move it to /usr/bin or /usr/local/bin, or make a directory and add it in your path.

Next, we create the work directory. All source code will be downloaded and built here.

mkdir mydroid
cd mydroid

Initialize the repo client by running the following command:

repo init -u git://android.git.kernel.org/platform/manifest.git

It will ask you some information and complete with a message like “repo initialized in /mydroid”.

Downloading the Android source code

Downloading the Android source code is simply done by opening a terminal in the work directory (“mydroid” in this guide) and typing “repo sync”:

cd ~/mydroid
repo sync

The source code is 2.1GB in size, so this will take a while. You will also require 6GB free to complete the build. Building Android from source

If you are building Cupcake (1.5, only if you ran repo init … -b cupcake) on Ubuntu, read this:

http://groups.google.com/group/android-platform/msg/14c960aace65e242

You can downgrade GCC on Ubuntu 9.10 by running:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 43 \
--slave /usr/bin/g++ g++ /usr/bin/g++-4.3 \
--slave /usr/bin/gcov gcov /usr/bin/gcov-4.3

sudo update-alternatives --config gcc

You have downloaded the source code, so let's build! To build the source code, open a terminal in the work directory and type “make”:

cd ~/mydroid
make

The process takes several hours, and generates files for both the host machine and the target machine (ARM processor by default). Files are in “out/target/product”.

Generating and Using the Android Emulator

Android comes with an emulator based on QEMU. To use this emulator, you must first generate an SDK image. Open a terminal in the work directory and type “make sdk”.

cd ~/mydroid
make sdk

This will create an sdk archive which you can use to develop for Android and test Android in an emulated environment.

Once the make process finishes, you will find the Android SDK in “out/host/linux-x86/sdk”. You can either take the included zip file and extract wherever you want or go directly into the generated sdk directory which contains folders like “tools”, “platforms” and so on.

To use the emulator, we must first configure an android virtual device. Open a terminal in the sdk directory and type:

cd android-sdk_...
tools/android create avd -n omnia -t 1

This will ask you some questions and create an android virtual device with the name “omnia”, using target id 1. Run “tools/android list targets” to see a list of available targets.

Having created an avd, you're now ready to start the emulator! From the sdk directory, type:

tools/emulator -avd omnia

This will start the emulator using the “omnia” virtual hardware configuration we created above. This should boot Android properly. It might be a little slow, but it's probably OK for testing purposes.

Have fun hacking!

TODO:

-Include configuration for actual deployment to phone