If you want to contribute to Android or to fix a bug (as opposite to just developing your own application to run on Android) you need to create a special development environment. This is quite different from Android application development environment as described here for Windows and here for Ubuntu Linux.
You will never need it if the only you want is to develop your own Android application.
Google published instructions for setting the environment and building Android OS here. Below I describe how I did it.
[Added on March 23, 2012. It is more than a year since I have written the post and androidexperiences kindly pointed out that certain URLs have changed since - please see androidexperiences' comment below.]
Preparing your computer
First you need to prepare your computer by installing Git, Repo and Java JDK. See Google instructions here. Interesting, Google folks apparently use Ubuntu and Mac OS on their development computers since there is no instruction for Windows computers. Luckily for me Ubuntu is my OS of choice as well.
Make sure your computer has at least 8GB of free space.
- If you want to build Android 2.3 or later, you must use 64-bit version of Linux. The build even won’t start on 32-bit Linux. As opposite, earlier versions of Android OS required 32-bit Linux to build. Although some developers reported building Android OS 2.2 and earlier on 64-bit Linux, it didn’t work for everybody. If you already have 64-bit Linux, you may be able to resolve the problem by installing 32-bit Linux as a host system (see instructions here)
- You have to use Java JDK 1.6 for building Android OS 2.3 or later; you have to use Java JDK 1.5 for earlier versions
[Update from Sep 25, 2011. Please see Paul's comments below (my thanks to him for sharing!). He managed to build Android older than 2.2 successfully on 32 bit Linux and got working OS as a result. However in my opinion as a rule it's better to follow vendor's recommendation, so if you have choice, use better 64 bit Linux.]
I will describe how to build Android OS 2.3.
First you need to install necessary packages. You can do it by following command in your terminal console window:
sudo apt-get install git-all gnupg flex bison gperf / build-essential zip curl zlib1g-dev gcc-multilib g++-multilib / libc6-dev-i386 lib32ncurses5-dev ia32-libs x11proto-core-dev / libx11-dev lib32readline5-dev lib32z-dev
I recommend installing git-all package instead of git-core recommended by Google since git-core is an obsolete package.
Install Java JDK 1.6 and make it default one:
sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner" sudo add-apt-repository "deb-src http://archive.canonical.com/ubuntu lucid partner" sudo apt-get update sudo apt-get install sun-java6-jdk sudo update-java-alternatives -s java-6-sun
Optionally Google recommends installing Valgrind. This can be done by the command:
sudo apt-get install valgrind
Now you need to add ~/bin directory to your path. Such directory does not exist by default, but if you followed my instructions on installing application development environment on Ubuntu published here, you should have had it.
Initializing a Repo client
Android uses git as a version control system. git description says:
Git is popular version control system designed to handle very largeprojects with speed and efficiency; it is used for many high profileopen source projects, most notably the Linux kernel.
Git falls in the category of distributed source code management tools.Every Git working directory is a full-fledged repository with fullrevision tracking capabilities, not dependent on network access or acentral server.
You have already installed git among other packages.
Google wrote a long shell script named Repo which runs on top of git and automates certain source management tasks. More about using Repo and git can be found here.
Go into ~/bin directory:
Download Repo script using curl (it should have been installed earlier with other packages):
curl http://android.git.kernel.org/repo >~/bin/repo
This will download repo script into ~/bin directory. Make it executable:
chmod a+x ~/bin/repo
Create a directory to place your working files into and move into it. I named it mydroid, but you can choose any name you want of course:
Move into it:
Download the latest version of Repo:
repo init -u git://android.git.kernel.org/platform/manifest.git
It will ask you of your name and email address. If you plan to contribute a code, use Gmail email address if you have such. However it is not necessarily Gmail account, just a valid email associated with a registered Google account. For more info on this see Google instructions.
Downloading Android OS source code
To download Android source code run following command in mydroid directory, i.e. the directory where you initialized Repo (Repo won’t work outside of it):
Be ready to wait for quite a while since the amount of data to be downloaded is quite big (more than 2GB). Later you can use the same command to download periodically the latest updates to the source code.
Verifying Git Tags
You may want to verify whether the downloaded code is a real one and it was not altered by somebody.
For this you need to load the following public key into your GnuPG key database. The key is used to sign annotated tags that represent releases.
Copy and paste the key(s) below, then enter EOF (Ctrl-D) to end the input and process the keys.
-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v126.96.36.199 (GNU/Linux) mQGiBEnnWD4RBACt9/h4v9xnnGDou13y3dvOx6/t43LPPIxeJ8eX9WB+8LLuROSV lFhpHawsVAcFlmi7f7jdSRF+OvtZL9ShPKdLfwBJMNkU66/TZmPewS4m782ndtw7 8tR1cXb197Ob8kOfQB3A9yk2XZ4ei4ZC3i6wVdqHLRxABdncwu5hOF9KXwCgkxMD u4PVgChaAJzTYJ1EG+UYBIUEAJmfearb0qRAN7dEoff0FeXsE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L0408sEBxiMior6b8LrZrAhkqDjA vUXRwm/fFKgpsOysxC6xi553CxBUCH2omNV6Ka1LNMwzSp9ILz8jEGqmUtkBszwo G1S8fXgE0Lq3cdDM/GJ4QXP/p6LiwNF99faDMTV3+2SAOGvytOX6KjKVzKOSsfJQ hN0DlsIw8hqJc0WISQQYEQIACQUCSedYRAIbDAAKCRDorT+BmrEOeCUOAJ9qmR0l EXzeoxcdoafxqf6gZlJZlACgkWF7wi2YLW3Oa+jv2QSTlrx4KLM= =Wi5D -----END PGP PUBLIC KEY BLOCK-----
After importing the keys, a fag cab be verified with the command:
git tag -v tagname
Building the source
First go into a directory where you downloaded the source code:
The run this command:
envsetup.sh contains a number of shell functions. After executing the command they become available as additional shell commands. There are many functions. You can see a list of them by typing help command (of course, it will work only after the previous command):
Run lunch command (which is one of functions within envsetup.sh script):
lunch command offers you a menu of choices. We’ll discuss in other post later what those choices mean. For now just select e.g. the first one (enter 1).
lunch will set up certain environment variables that will define the result of the build. You will see something like that:
============================================ PLATFORM_VERSION_CODENAME=AOSP PLATFORM_VERSION=AOSP TARGET_PRODUCT=full TARGET_BUILD_VARIANT=eng TARGET_SIMULATOR=false TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv5te HOST_ARCH=x86 HOST_OS=linux HOST_BUILD_TYPE=release BUILD_ID=OPENMASTER ============================================
Now all is ready for the build.
Android OS is build is based on make utility. This may be somewhat surprising since Android contains a lot of Java code and it seems it would be natural to use something more modern like Maven. But the choice is most likely made since Android OS is actually a Linux “in disguise” and Android the most likely “inherited” it from Linux.
You can run either make or m command within your mydroid directory (or whatever name you gave the directory where you downloaded the code). m is the function defined in envsetup.sh script.
Be patient again. The complete build takes around 2 hours on my laptop with 2 core AMD CPU and plenty of RAM.
If the build is successful. The build log will end up with something like that:
target Non-prelinked: sensors.mahimahi (out/target/product/generic/symbols/system/lib/hw/sensors.mahimahi.so) target Strip: sensors.mahimahi (out/target/product/generic/obj/lib/sensors.mahimahi.so) Generated: (out/target/product/generic/android-info.txt) Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img Install system fs image: out/target/product/generic/system.img Target ram disk: out/target/product/generic/ramdisk.img Target userdata fs image: out/target/product/generic/userdata.img Installed file list: out/target/product/generic/installed-files.txt
Congratulations! You have built android.
However if you see something like this
collect2: ld returned 1 exit statusmake: *** [out/host/linux-x86/obj/EXECUTABLES/emulator_intermediates/emulator] Error 1
the build failed. We have to look at the next section:
Android build is very complex one and therefore relatively brittle. If your build fails for reasons you can’t understand, run either
This forces make to re-build all targets, i.e. to do a “clean” build.
If your build fails because of a missing run-java-tool, Google recommends setting the ANDROID_JAVA_HOME environment variable before running the build.
$ export ANDROID_JAVA_HOME=$JAVA_HOME