\subchapter{Use ADB}{Learn to use the Android Debug Bridge}

After this lab, you will be able to use ADB to:
\begin{itemize}
  \item Debug your system and applications
  \item Get a shell on a device
  \item Exchange files with a device
  \item Install new applications
\end{itemize}

\section{Setup}

Stay in the \code{$HOME/android-labs/source} directory.

\section{Get ADB}

ADB is usually distributed by Google as part of their SDK for Android.

If you were willing to use it, you would first need to go to
\url{http://developer.android.com/sdk/} to get the SDK for Linux, and
extract the archive that you downloaded.

Then, you would run the \code{tools/android} script, and in the
\code{Packages} window, select \code{Android SDK Platform-tools} under
\code{Tools}, unselect all the other packages, and click on the
\code{Install 1 package...} button.  Once done, you would have
\code{adb} installed in \code{./platform-tools/adb}.

However, the Android source code also has an embedded SDK, so you
already have the \code{adb} binary in the
\code{out/host/linux-x86/bin/}.

To add this directory to your \code{PATH} after the build done in the
previous lab, you can go through the same environment setup steps as
earlier:

\begin{verbatim}
source build/envsetup.sh
lunch beagleboneblack-eng
\end{verbatim}

\section{Get logs from the device}

ADB provides many useful features to work with an existing Android
device.  The first we will see is how to get the system logs from the
whole system. To do this, just run \code{adb logcat}.

You will see the device logs on your terminal. This is a huge amount
of information though, and it is difficult to find your way in all
these lines.

The first thing we can do is download a little wrapper to \code{adb}
to provide colored logs. You can find it here:
\url{http://jsharkey.org/downloads/coloredlogcat.pytxt}. Once
downloaded, just run it and you will see the logs colored and
formatted to be easily readable.

ADB also provides filters to have a clearer output. These are
formatted by the \code{Tag:Priority} syntax. For example, if you want
all logs from \code{MyApp}, just run \code{adb logcat MyApp:*}.

Now try to save all logs from Dalvik to a file using only \code{adb}.

\section{Get a shell on the device}

Having a shell on the device can prove pretty useful. ADB provides
such a feature, even though this embedded shell is quite minimal. To
access this shell, just type \code{adb shell}.

You can also run a command directly on the device thanks to
\code{adb shell}. To do this, just append the command to run at the
end. Now, try to get all the mounted filesystems on the device.

\section{Push/Pull files to a device}

In the same way, ADB allows you to retrieve files from the connected
device and send them to it, using the \code{push} and \code{pull}
commands.

