page.title=Get Android Source Code
doc.type=source
@jd:body

<div>
  <p>This document describes how to set up your local work environment, how to use Repo to get the Android files, and how to build the files on your machine.</p>
  <p>Related reading:
    <ul>
      <li>For an overview of the entire code-review and code-update process, see
<a href="{@docRoot}source/life-of-a-patch.html">Life of a Patch</a>.</li>
      <li>For reference details about Repo, see <a href="{@docRoot}source/git-repo.html">Using Repo and Git</a>.</li>
    </ul>
  </p>
</div>

<div>
  <h2>What's in the source?</h2>
  <p>To see snapshots and histories of the files available in the public Android repositories, visit the <a href="http://android.git.kernel.org/">GitWeb</a> web interface.</p>
  <p>The source is approximately 2.6GB in size. You will need 6GB free to complete the build.</p>
</div>

<div>
  <h2>Setting up your machine</h2>
  <p>To build the Android source files, you will need to use Linux or Mac OS. Building under Windows is not currently supported.</p>

  <h3>Linux</h3>
  <p>The Android build is routinely tested in house on recent versions of Ubuntu (10.04 and later), but most distributions should have the required build tools available.  Reports of successes or failures on other distributions are welcome.</p>
  <p>In general you will need:
      <ul>
        <li>Python 2.4, which you can download from <a href="http://www.python.org/download/">python.org</a>.</li>
        <li>JDK 6 if you wish to build Gingerbread or newer; JDK 5 for Froyo or older.  You can download either from <a href="http://java.sun.com/javase/downloads/">java.sun.com</a>.</li>
        <li>Git 1.5.4 or newer. You can find it at <a href="http://git.or.cz/">http://git.or.cz/</a>.</li>
      </ul>
  </p>

  <h4>Ubuntu Linux (64-bit)</h4>
  <p>The Sun JDK is no longer in Ubuntu's main package repository.  In order to download it, you need to add the appropriate repository and indicate to the system which JDK should be used.
    <p>Java 6: for Gingerbread and newer
      <div class=code>sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"<br/>
                      sudo add-apt-repository "deb-src http://archive.canonical.com/ubuntu lucid partner"<br/>
                      sudo apt-get update<br/>
                      sudo apt-get install sun-java6-jdk<br/>
                      sudo update-java-alternatives -s java-6-sun
      </div>
    </p>
    <p>Java 5: for Froyo and older
      <div class=code>sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu dapper main multiverse"<br/>
                      sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu dapper-updates main multiverse"<br/>
                      sudo apt-get update<br/>
                      sudo apt-get install sun-java5-jdk<br/>
                      sudo update-java-alternatives -s java-1.5.0-sun
      </div>
    </p>
  </p>
  <p>To set up your development environment, install the following required packages:
    <div class=code>$ sudo apt-get install git-core 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</div>
  </p>
  <p>You might also want Valgrind, a tool that will help you find memory leaks, stack corruption, array bounds overflows, etc.</p>

  <h4>Running Linux in a virtual machine</h4>
If you are running Linux in a virtual machine, you will need at least 1.5GB of RAM and 10GB or more of disk space in order to build the Android tree.<br>
</div> 

<div>
  <h3>Mac OS X</h3>
  <p>To build the Android files in a Mac OS environment, you need an Intel/x86 machine running MacOS 10.4 (Tiger), 10.5 (Leopard), or 10.6 (Snow Leopard). The Android build system and tools do not support the obsolete PowerPC architecture.</p>
  <p>Android must be built on a case-sensitive file system because the sources contain files that differ only in case. We recommend that you build Android on a partition that has been formatted with the journaled file system HFS+.  HFS+ is required to successfully build Mac OS applications such as the Android Emulator for OS X.</p>
  <div>
    <h4>Creating a case sensitive disk image</h4>
    <p>If you want to avoid partitioning/formatting your hard drive, you can use a case-sensitive disk image instead. To create the image, launch Disk Utility and select "New Image".  A size of 8 GB is sufficient, or more if you prefer.  Be sure to select "case sensitive, journaled" as the volume format.</p>
    <p>This will create a .dmg file which, once mounted, acts as a drive with the required formatting for Android development. For a disk image named "android.dmg" stored in your home directory, you can add the following to your <tt>~/.bash_profile</tt> to mount the image when you execute "mountAndroid": 
    <div class=code># mount the android file image<br/>
                    function mountAndroid{ hdiutil attach ~/android.dmg-mountpoint /Volumes/android; }</div>
    Once mounted, you'll do all your work in the "android" volume. You can eject it (unmount it) just like you would with an external drive.</p>
  </div>

  <p>To set up your Mac OS development environment, follow these steps:
    <ol>
      <li>Install XCode from <a href="http://developer.apple.com/">http://developer.apple.com</a>. We recommend version 3.0 or newer.  If you are not already registered as an Apple developer, you will have to create an Apple ID in order to download.</li>
      <li>Install MacPorts from <a href="http://www.macports.org/install.php">http://www.macports.org/</a>.</li>
      <li>Make sure that /opt/local/bin appears in your path BEFORE /usr/bin.  If not, add 
        <div class=code>export PATH=/opt/local/bin:$PATH</div>
          to your <tt>~/.bash_profile</tt>.</li>
      <li>Get make, git, and GPG packages from port: 
          <div class=code>$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupg</div>
          If using Mac OS 10.4, also install bison:
          <div class=code>$ POSIXLY_CORRECT=1 sudo port install bison</div>
      </li>
      <li>Temporary step: There is a bug in gmake 3.82 that prevents android from building.  You can install version 3.81 using MacPorts by taking the following steps:
        <p>Edit <tt>/opt/local/etc/macports/sources.conf</tt> and a line that says <div class=code>file:///Users/Shared/dports</div> above the rsync line.  Then create this directory: <div class=code>$ mkdir /Users/Shared/dports</div>
          In the new <tt>dports</tt> directory, run <div class=code>$ svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/</div>
          Create a port index for your new local repository: <div class=code>$ portindex /Users/Shared/dports</div>
          Finally, install the old version of gmake with <div class=code>$ sudo port install gmake @3.81</div>
        </p>
      </li>
      <li>Set an appropriate per-process file descriptor limit. To do this, add the following lines to your <tt>~/.bash_profile</tt>: <div class=code># set the number of open files to be 1024<br/>
                      ulimit -S -n 1024</div>
      </li>
    </ol>
  </p>
</div>


<div>
  <h2>Installing Repo</h2>
  <p>Repo is a tool that makes it easier to work with Git in the context of Android. For more information about Repo, see <a href="{@docRoot}source/git-repo.html">Using Repo and Git</a>.</p>
  <p>To install, initialize, and configure Repo, follow these steps:
    <ol>
      <li>Make sure you have a bin/ directory in your home directory, and that it is included in your path:
        <div class=code>$ mkdir ~/bin<br/>
                        $ PATH=~/bin:$PATH</div>
      </li>
      <li>Download the Repo script and ensure it is executable:
        <div class=code>$ curl http://android.git.kernel.org/repo &gt; ~/bin/repo<br/>
                        $ chmod a+x ~/bin/repo</div>
      </li>
    </ol>
</div>

<div>
  <h2>Initializing a Repo client</h2>
  <p>After installing Repo, set up your client to access the android source repository:
    <ol>
      <li>Create an empty directory to hold your working files:
        <div class=code>$ mkdir <i>directory</i><br/>
                        $ cd <i>directory</i></div>
      </li>
      <li>Run <tt>repo init</tt> to bring down the latest version of Repo with all its most recent bug fixes.  You must specify a URL for the manifest, which specifies where the various repositories included in the Android source will be placed within your working directory.
        <div class=code>$ repo init -u git://android.git.kernel.org/platform/manifest.git</div>
        To check out a branch other than "master", specify it with -b:
        <div class=code>$ repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake</div>
      </li>
      <li>When prompted, please configure Repo with your real name and email address.  To use the Gerrit code-review tool, you will need an email address that is connected with a <a href="http://www.google.com/accounts">registered Google account</a>.  Make sure this is a live address at which you can receive messages.  The name that you provide here will show up in attributions for your code submissions.
      </li>
    </ol>
    A successful initialization will end with a message stating that Repo is initialized in your working directory.  Your client directory should now contain a <tt>.repo</tt> directory where files such as the manifest will be kept.</p>
</div>

<div>
  <h2>Getting the files</h2>
  <p>To pull down files to your working directory from the repositories as specified in the default manifest, run
    <div class=code>$ repo sync</div>
    For more about <tt>repo sync</tt> and other Repo commands, see <a href="{@docRoot}source/git-repo.html">Using Repo and Git</a>.</span>
  <p>The Android source files will be located in your working directory under their project names.</p>
</div>

<div>
  <h2>Verifying Git Tags</h2>
  <p>Load the following public key into your GnuPG key database. The key is used to sign annotated tags that represent releases.
    <div class=code>$ gpg --import</div>
    Copy and paste the key(s) below, then enter EOF (Ctrl-D) to end the input and process the keys.
    <div class=code>-----BEGIN PGP PUBLIC KEY BLOCK-----<br>
      Version: GnuPG v1.4.2.2 (GNU/Linux)<br><br>
      mQGiBEnnWD4RBACt9/h4v9xnnGDou13y3dvOx6/t43LPPIxeJ8eX9WB+8LLuROSV <br>
      lFhpHawsVAcFlmi7f7jdSRF+OvtZL9ShPKdLfwBJMNkU66/TZmPewS4m782ndtw7<br>
      8tR1cXb197Ob8kOfQB3A9yk2XZ4ei4ZC3i6wVdqHLRxABdncwu5hOF9KXwCgkxMD <br>
      u4PVgChaAJzTYJ1EG+UYBIUEAJmfearb0qRAN7dEoff0FeXsEaUA6U90sEoVks0Z <br>
      wNj96SA8BL+a1OoEUUfpMhiHyLuQSftxisJxTh+2QclzDviDyaTrkANjdYY7p2cq <br>
      /HMdOY7LJlHaqtXmZxXjjtw5Uc2QG8UY8aziU3IE9nTjSwCXeJnuyvoizl9/I1S5<br>
      jU5SA/9WwIps4SC84ielIXiGWEqq6i6/sk4I9q1YemZF2XVVKnmI1F4iCMtNKsR4<br>
      MGSa1gA8s4iQbsKNWPgp7M3a51JCVCu6l/8zTpA+uUGapw4tWCp4o0dpIvDPBEa9<br>
      b/aF/ygcR8mh5hgUfpF9IpXdknOsbKCvM9lSSfRciETykZc4wrRCVGhlIEFuZHJv <br>
      aWQgT3BlbiBTb3VyY2UgUHJvamVjdCA8aW5pdGlhbC1jb250cmlidXRpb25AYW5k <br>
      cm9pZC5jb20+iGAEExECACAFAknnWD4CGwMGCwkIBwMCBBUCCAMEFgIDAQIeAQIX <br>
      gAAKCRDorT+BmrEOeNr+AJ42Xy6tEW7r3KzrJxnRX8mij9z8tgCdFfQYiHpYngkI <br>
      2t09Ed+9Bm4gmEO5Ag0ESedYRBAIAKVW1JcMBWvV/0Bo9WiByJ9WJ5swMN36/vAl <br>
      QN4mWRhfzDOk/Rosdb0csAO/l8Kz0gKQPOfObtyYjvI8JMC3rmi+LIvSUT9806Up <br>
      hisyEmmHv6U8gUb/xHLIanXGxwhYzjgeuAXVCsv+EvoPIHbY4L/KvP5x+oCJIDbk <br>
      C2b1TvVk9PryzmE4BPIQL/NtgR1oLWm/uWR9zRUFtBnE411aMAN3qnAHBBMZzKMX <br>
      LWBGWE0znfRrnczI5p49i2YZJAjyX1P2WzmScK49CV82dzLo71MnrF6fj+Udtb5+<br>
      OgTg7Cow+8PRaTkJEW5Y2JIZpnRUq0CYxAmHYX79EMKHDSThf/8AAwUIAJPWsB/M <br>
      pK+KMs/s3r6nJrnYLTfdZhtmQXimpoDMJg1zxmL8UfNUKiQZ6esoAWtDgpqt7Y7s <br>
      KZ8laHRARonte394hidZzM5nb6hQvpPjt2OlPRsyqVxw4c/KsjADtAuKW9/d8phb <br>
      N8bTyOJo856qg4oOEzKG9eeF7oaZTYBy33BTL0408sEBxiMior6b8LrZrAhkqDjA <br>
      vUXRwm/fFKgpsOysxC6xi553CxBUCH2omNV6Ka1LNMwzSp9ILz8jEGqmUtkBszwo <br>
      G1S8fXgE0Lq3cdDM/GJ4QXP/p6LiwNF99faDMTV3+2SAOGvytOX6KjKVzKOSsfJQ <br>
      hN0DlsIw8hqJc0WISQQYEQIACQUCSedYRAIbDAAKCRDorT+BmrEOeCUOAJ9qmR0l <br>
      EXzeoxcdoafxqf6gZlJZlACgkWF7wi2YLW3Oa+jv2QSTlrx4KLM=<br>
      =Wi5D <br>
      -----END PGP PUBLIC KEY BLOCK-----
    </div>
    After importing the keys, you can verify any tag with <div class=code>$ git tag -v <i>tagname</i></div>
  </p>
</div>

<div>
  <h2>Building the code</h2>
  <p>To build the files, run envsetup, lunch, and make from within your working directory:
    <div class=code>$ cd ~/<i>directory</i><br/>
                    $ source build/envsetup.sh<br/>
                    $ lunch<br/>
                    $ make<br/>
    </div>
    If your build fails because of a missing <tt>run-java-tool</tt>, try setting the <tt>ANDROID_JAVA_HOME</tt> environment variable before making. 
    <div class=code>$ export ANDROID_JAVA_HOME=$JAVA_HOME</div>
  </p>
</div>

<div>
  <h2>What's next?</h2>
  <p>See <a href="{@docRoot}source/using-eclipse.html">Using Eclipse</a> for instructions on how to use an IDE for Android platform development.  To learn about reporting an issue and searching previously reported issues, see <a href="{@docRoot}source/report-bugs.html">Report bugs</a>.  For information about editing the files and uploading changes to the code-review server, see <a href="{@docRoot}source/submit-patches.html">Contribute</a>.
</div>
