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.
Important notes:
- 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:
cd ~/bin
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:
mkdir ~/mydroid
Move into it:
cd ~/mydroid
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):
repo sync
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.
gpg --import
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 v1.4.2.2 (GNU/Linux) mQGiBEnnWD4RBACt9/h4v9xnnGDou13y3dvOx6/t43LPPIxeJ8eX9WB+8LLuROSV lFhpHawsVAcFlmi7f7jdSRF+OvtZL9ShPKdLfwBJMNkU66/TZmPewS4m782ndtw7 8tR1cXb197Ob8kOfQB3A9yk2XZ4ei4ZC3i6wVdqHLRxABdncwu5hOF9KXwCgkxM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sjADtAuKW9/d8phb N8bTyOJo856qg4oOEzKG9eeF7oaZTYBy33BTL0408sEBxiMior6b8LrZrAhkqDjA 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:
cd ~/mydroid
The run this command:
source build/envsetup.sh
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):
help
Run lunch command (which is one of functions within envsetup.sh script):
lunch
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:
Troubleshooting
Android build is very complex one and therefore relatively brittle. If your build fails for reasons you can’t understand, run either
make -B
or
m -B
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
> If you want to build Android 2.3 or later,
> you must use 64-bit version of Linux.
The ‘TI-Android-GingerBread-2.3.4-DevKit-2.1 DeveloperGuide’ describes “Host (PC) setup requirements. For Ubuntu on 32-bit machines” and goes on to describe building Android — Linux kernel and all.
Is a 64-bit Ubuntu build machine **really** required?
Paul,
My instructions were written in January 2011. At that moment it was true. When somebody tried to build Android OS on 32 bit Ubuntu build would not even start – the build process checked for version of Ubuntu (64 or 32) and if it was 32, it stopped with corresponding error message.
Re the documentation. My observations are that there is substantial quantity of outdated information in Android documentation; the team just does not always keep the documentation current.
Of course you can try to build Android OS on 32 bit Linux. There is a chance that since January Android team made it working on both 32 and 64, however I would not think it highly likely. If you manage to build it, let me know and I’ll publish your correction.
If you have 32 bit Ubuntu installed on your computer, you can use 64 bit virtual machine to do the build. It worked for me. I posted instructions in my blog under Virtualization on Linux tag.
Thanks for the reply. I am building Android now (rather STILL) in a 32-bit Ubuntu VirtualBox (created before I had a hint of a 64-bit requirement). I’m building TI’s Gingerbread 2.3.4-DevKit-2.1. The kernal, x-loader and u-boot build nicely and cleanly.
However, the Android build starts off with “Only 64-bit build environments are supported beyond froyo/2.2″. Yet, the build proceeds despite the warning generating a whole lotta scary looking compiler warnings along the way. — Potential return using unintialized value?! Pointer of type void* used in arithmetic?! Potenial unintialized value used?!
All-in-all this is not what I expected of presumably professional quality code.
Despite my searches I have not found definitive answer to the 64-bit question about the Android build. Not even on TI’s site. Puzzling. You’d think someone in the know would have somethng to say about this.
Anyway, I’ll let you know if 32 bits were enough to build Android. Given the roughness of the build so far, I doubt it will work. If it fails I probably won’t be able to prove it was the build environment.
Vlad,
I finally found the time to finish all of the steps for a complete Linux kernel and Android build for Beagleboard-xM. Miraculously it works — at least the Gingerbread 2.3.4 DevKit 2.1 release by Texas Instruments works.
So, despite the messages in the build (and the gazillion compiler warnings), a working Android can be built on a 32-bit Ubuntu 11.04.
Paul,
Thank you for the update. Couple questions:
- You probably modified make script to get past the warning, have you?
- How did you test the build result to make sure it worked?
I didn’t need to modify anything after the 64-bit warning. The build just proceeded past the warning anyway. I tested the result by following TI DevKit instructions for putting the images on an SD card and using this to boot a Beagleboard-XM. Below is a summary of what I did.
To get Gingerbread Android up on Beagleboard-XM with a build system on 32-bit Ubuntu 11.04:
Get TI Gingerbread 2.3.4 Devkit 2.1:
http://software-dl.ti.com/dsps/dsps_public_sw/sdo_tii/TI_Android_DevKit/TI_Android_GingerBread_2_3_4_DevKit_2_1/index_FDS.html
From the DevKit page above, download tarballs:
TI_Andorid_GingerBread_2_3_4 Sources.tar.gz
TI_Andorid_GingerBread_2_3_4_DevKit_2_1.tar.gz <– for Tools folder
Follow build instructions in the Developer Guide for using the 'pre-packaged repo'.
The only extra step I took for the Android part of the build ** after x-loader, u-boot and Linux kernel were built ** was to create a docs/gen folder to silence a 'not found' warning. This step is very probably not necessary.
# cd to root of source tree
cd ~/rowboat-android/TI-Android-GingerBread-2.3.4-DevKit-2.1Sources
# create docs/gen to silence 'not found'
mkdir out/target/common/docs
mkdir out/target/common/docs/gen
# Build Android and ignore 64-bit warning.
# Also ignore the gazillion compiler warnings
make
Beagleboard-XM users may find it hard to get the Android internet browser working. Here's how I did it after a lot of hunting around. NOTE: There is no 'eth0' device on the Beagleboard-XM. In its stead use 'usb0'.
To set up static IP address for the Beagleboard-XM, at the serial port console ('#' prompt):
# ifconfig usb0 netmask up
# netcfg <— show the IP address and state of usb0
# setprop net.dns1
# route add default gw dev usb0
To use DHCP:
# netcfg usb0 dhcp
# netcfg <— show the IP address and state of usb0
# getprop net.dns1 <— show DNS IP that SHOULD have been set by DHCP
If pinging other machines on the network doesn't work at this point:
# setprop net.dns1
If you can’t ping http://www.google.com, then:
# route add default gw dev usb0
I hope this helps some folks enjoy their Beagleboard a few minutes sooner than they otherwise would.
Vlad, since I don’t know what markup is allowed to format posts, feel free to edit/combine this and my previous post for easier reading.
To set up a static IP address on the Beagleboard-XM, at the serial port console (‘#’ prompt):
# ifconfig usb0 {BB IP} netmask {NETMASK} up
# netcfg <— show the IP address and state of usb0
# setprop net.dns1 {DNS IP}
# route add default gw {GW IP} dev usb0
To use DHCP:
# netcfg usb0 dhcp
# netcfg <— show the IP address and state of usb0
# getprop net.dns1 <— show DNS IP that SHOULD have been set by DHCP
If pinging other machines on the network doesn't work at this point:
# setprop net.dns1 {DNS IP}
If you can't ping http://www.google.com, then:
# route add default gw {GW IP} dev usb0
Hi Vlad,
great tutorial I have just found that the repo url has changed you now have to do :
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo and then
repo init -u https://android.googlesource.com/platform/manifest
as according to http://source.android.com/source/downloading.html
androidexperiences,
Thank you. I appreciate your corrections and I have added a reference to it at the beginning of the post.
hi this is naseer. I am newbie to this technology.
I have 32-bit linux os in 32-bit processor. I need how to get the complete souce code of android. Pls send me the procedure to do this.
I installed ubuntu 11.10 32-bit os.I am not using virtual os.
pls send me the procedure. Its urgent for me.
Thank you very much in advance
Hi I need a sample code to prepare customized android os and also tell me what technologies i need to build it.
i got samsung p6200 android i want to put windows 7 on it how can i do it must i write windows 7 program for android
I’m not sure what you ask: either you want to put W7 on the device which currently runs Android OS or you want to use Windows development skills/code to develop Android application. I don’t know the answer for the former; for the latter you may want to consider Mono for Android (http://blog.xamarin.com/2012/05/30/new-monotouch-and-mono-for-android-guides-available/)