\subchapter{Building a library}{Add a native library to the build system}

After this lab, you will be able to
\begin{itemize}
  \item Add an external library to the Android build system
  \item Compile it dynamically
  \item Add a component to a build
\end{itemize}

\section{Build a shared library}

The Android team already has put the libusb source code, but for some
reason isn't actually using it or made a Makefile for it. Since we
need a more recent version anyway, we will need to edit the manifest
so that repo grabs a newer version instead.

Create a new local manifest to download the libusb from the git
repository \code{git://github.com/libusb/libusb.git}. You'll need to use the
version 1.0.9, that is in git \code{refs/tags/1.0.9}. You'll also need
to remove both the existing \code{libusb} and \code{libusb_aah}
projects from the manifests.

Then, do a \code{repo sync -c}, and everything should be downloaded.
Note that you may have to use \code{repo sync --force-sync
external/libusb}.

For this library, all the needed \code{.c} files are located in the
\code{libusb} folder and its subfolders. The headers are located in
the same folders. You shouldn't modify the \code{libusb} source code.

Remember that you have make functions in the Android build system that
allows you to list all the source files in a directory. You can also
use the \code{filter-out} make macro to remove files that match a
given pattern.

You will find one missing header that you will need to
generate. Indeed, the \code{config.h} generated by autoconf is not
generated at all, because the Android build system ignores other build
systems. You will have to generate it by yourself, by running the
\code{configure} script\footnote{You can have some hints about the
  available options available by passing the \code{--help} argument to
  \code{configure}} that you can find in the \code{libusb} source
code. The git repository doesn't have this \code{configure} script
however. This script is also usually generated through the
\code{autogen.sh} script that you'll find in the sources.

To be able to use this script, we will need to install the packages
\code{libtool} and \code{automake}. Once installed, you can generate
the configure script using \code{./autogen.sh}. It should also
generate the needed \code{config.h} header.

Along the building process, you might need to define values in the
source code. We prefer to avoid modifying directly the source code,
since it will end up in merging problems in the future if we ever want
to upgrade that component. \code{gcc} accepts on its command line the
\code{-D} argument, that you can use either to define a value
(\code{-DFOOBAR=42}) or a macro (\code{-D'FOO(bar)=(bar * 42)'}).

If successful, the build system should go through the build process
and you should have a directory generated in the \code{out} directory.

\section{Integrate the library into the Android image}

As you can see, your library has been compiled during the build
process, but if you boot the generated image or look inside the
folder, you can see that the shared object is not present.

Modify the appropriate files so that the library gets included in your
image.
