\subchapter{Android source code}{Download the source code for Android and all its components}

During this labs, you will:
\begin{itemize}
  \item Install all the development packages needed to fetch and compile Android
  \item Download the \code{repo} utility
  \item Use \code{repo} to download the source code for Android
        and for all its components
\end{itemize}

\section{Setup}

Look at the space available on your root partition using the
\code{df -h} command. The first line should display the available
storage size on your root partition. If you have more than 40GB, go to
the \code{/opt} directory.

\begin{verbatim}
sudo mkdir android
sudo chown -R <user>:<group> android
cd android
\end{verbatim}

If you don't have that much space available, go to the
\code{$HOME/android-labs/source} directory.

\section{Install needed packages}

Install the packages needed to fetch Android's source code:

\begin{verbatim}
sudo apt install git curl
\end{verbatim}

\section{Fetch the source code}

Android sources are made of many separate Git repositories, each
containing a piece of software needed for the whole stack. This
organization adds a lot of flexibility, allowing to easily add or
remove a particular piece from the source tree, but also introduces a
lot of overhead to manage all these repos.

To address this issue, Google created a tool called Repo. As Repo is
just a python script, it has not made its way in the Ubuntu packages,
and we need to download it from Google.

\begin{verbatim}
mkdir $HOME/bin
export PATH=$HOME/bin:$PATH
curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > \
    $HOME/bin/repo
chmod a+x $HOME/bin/repo
\end{verbatim}

We can now fetch the Android source code:

\begin{verbatim}
repo init -u https://android.googlesource.com/platform/manifest \
     -b android-4.3_r2.1
repo sync -c -j2
\end{verbatim}

\code{repo sync -c} will synchronize the files for all the source
repositories listed in the manifest. If you have a slow connection to
the Internet, this could even take a few hours to
complete. Fortunately, your instructor will take advantage of this
waiting time to continue with the lectures.

The \code{-c} option makes it download only the branch we will be
using, speeding up the download.

We are using the \code{-jX} option of \code{repo sync} to have \code{X}
downloads in parallel. Otherwise, if the source repositories are fetched
sequentially, a lot of time and available network bandwidth is wasted
between downloads, processing the files downloaded by git.

In our case, we are using \code{2} because we all share the same
internet connection, but you can definitely increase this number up to
\code{8} when you are alone.

If this really takes too much time, your instructor may distribute a
ready-made archive containing what \code{repo sync} would have
downloaded.

To save time, do not wait for the \code{repo sync} command to
complete. You can already jump to the next section.

\section{Install packages needed at compile time}

While \code{repo sync} runs, download the packages that you will need
to compile Android and its components from source:

\begin{verbatim}
sudo apt install xsltproc gnupg flex bison gperf build-essential \
     zlib1g-dev libc6-dev lib32ncurses5-dev ccache unzip zip\
     x11proto-core-dev libx11-dev lib32readline6-dev lib32z1-dev \
     libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown \
     libxml2-utils
sudo apt clean
\end{verbatim}

Again, if you have a slow connection to the Internet, installing these
packages could take several tens of minutes. Anyway, we are getting
back to the lectures.

\section{Install the Java JDK}

The Android build system requires the Oracle Java6 JDK, that is too
old to be bundled in Ubuntu. We'll have to fetch it from another
source, and install it.

To do so, we first need to add the repository that holds the JDK
nowadays.

\begin{verbatim}
sudo add-apt-repository ppa:webupd8team/java
sudo apt update
\end{verbatim}

Now, we can install the latest JDK version with the command:
\begin{verbatim}
sudo apt install oracle-java6-installer
\end{verbatim}

Follow the steps asked, and you should be all set!

\section{Install the ccache}

Jelly Bean takes a lot of time to compile. Fortunately, we can use a
tool called \code{ccache} to speed it up. \code{ccache} caches the
object files compiled for a given project, so that whenever you need
them once again, you don't have to actually cache them.

Ask your trainer for a pre-filled ccache archive that we will use to
speed up a lot the compilation. Once you have retrieved the archive,
extract it in either the \code{/opt/} or \code{$HOME/android-labs/}
directories.
