Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Getting the source code of Android itself

Author: Ian Darwin
Published? false -- FormatLanguage: W

Problem:

How did they do that?

Solution:

"Use The Source, Luke". Android is an open-source project, just waiting for you to download the code, read and understand it, and even contribute improvements.

Discussion:

The official Android Open Source Project (AOSP) is maintained by Google. There is another full-blown source project derived from it, known as CyanogenMod (CM). AOSP corresponds closely to what the carriers release on their devices. CM tends to be less carrier-oriented and more consumer/geek oriented. For example, CM7 (which corresponds to Android 2.x, Gingerbread), included a number of useful utilities, such as SuperUser (CM loads are always pre-rooted; AOSP loads are not), Torch/Flashlight functionality as part of a more comprehensive home-screen power control, and quite a bit more. You can read about both projects at their official websites, AOSP and CM.

Details of installing system images (often, though incorrectly, referred to as "ROM"s) are covered on the CM pages for the particular device, and are far too varied to cover here.

Details on downloading particular versions varies, so reference should be made to the latest versions at AOSP or CM. The latter gives a build page for each device, but the download steps are the same or similar.

Getting the source, while it does vary somewhat, is almost the same for AOSP and CM. You need to have a fast computer with multiple CPU cores, lots of RAM and disk space, and installed the SDK, the git source code repository, Java (JDK6), and several other tools. And, you pretty well need a Unix-like system such as Linux or OS X (I used OpenBSD to download, but it won't compile there). The following is a general guideline.

You need 'repo', which is a script front end for git to bring together the dozens of individual GIT repos that make up Android.

You can get repo by FTP using the original ftp command, or wget or curl, whatever you have. I used

ftp https://dl-ssl.google.com/dl/googlesource/git-repo/repo

On Unix/Linux/OSX, mark the repo executable with chmod +x.

Create a directory hierarchy with two levels, call them src and src/repo.

cd into src/repo.

Give the command

repo init -u https://android.googlesource.com/platform/manifest

or

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

depending upon whether you want to build AOSP or CM. If you know what branches exist, you can also use -b with a branch name.

Give the command repo sync with an optional -j argument for the number of fetch jobs to run concurrently; CM recomments -j16 as an example.

Check back every few hours, depending on your computer and network.

When it's done, you will have populated your src directory, and can start reading (you should see a Makefile and a dozen or so directories with names like android, dalvik, and system).

You can update your code when you want to refresh it just by reissuing the repo sync command.

If you have Linux or OS X, you can actually build these sources, but the details vary a bit more; see the web pages mentioned above. This Recipe is really intended just to introduce the ability to view the source code.

See Also:

There are web browser sites that let you view individual pieces of the code; you can find these with a web search engine (such as Google :-)).

No records found.