Command Line tools

Write App Reviews

Build your own Android App Dev Empire

Get Instant Access

The Android SDK ships with a collection of command-line tools, which are located in the tools subdirectory of your Android SDK installation. While Eclipse and the ADTs provide a great deal of control over our Android development environment, sometimes it is nice to exercise greater control, particularly when considering the power and convenience that scripting can bring to a development platform. We are going to explore two of the command-line tools found in the Android SDK.

It is a good idea to add the tools directory to your search path. For example, if your Android SDK is installed to c:\software\google\androidsdk, you can add the Android SDK to your path by performing the following operation in a command window on your Windows computer:

set path=%path%;c:\software\google\androidsdk\tools;

Or use the following command for Mac OS X and Linux:

export PATH=$PATH:/path_to_Android_SDK_directory/tools ANDROID ASSET PACKAGING TOOL

You may be wondering just how files such as the layout file main.xml get processed and exactly where the file comes from. Who zips up the application file for us into the apk file? Well, you may have already guessed, but it is the Android Asset Packaging Tool, or as it is called from the command line, aapt. This is a versatile tool that combines the functionality of pkzip or jar along with an Android-specific resource compiler. Depending on the command-line options provided to it, aapt wears a number of hats and assists with our design-time Android development tasks. To learn the functionality available in aapt, simply run it from the command line with no arguments. A detailed usage message is written to the screen.

While aapt helps with design-time tasks, another tool, the Android Debug Bridge, assists us at runtime to interact with the Android Emulator.


The Android Debug Bridge (adb) utility permits us to interact with the Android Emulator directly from the command line or script. Have you ever wished you could navigate the filesystem on your smartphone? Well, now you can with the adb! The adb works as a client/server TCP-based application. While there are a couple of background processes that run on the development machine and the emulator to enable our functionality, the important thing to understand is that when we run adb, we get access to a running instance of the Android Emulator. Here are a couple of examples of using adb. First, let's look to see if we have any available Android Emulator sessions running:

adb devices<return>

This command will return a list of available Android Emulators; for example, figure 2.8 shows adb locating two running emulator sessions.

Let's connect to the first Android Emulator session and see if our application is installed. We connect with the syntax adb shell. This is how we would connect if we had a single Android Emulator session active, but because there are two emulators running, we need to specify an identifier to connect to the appropriate session:

adb -d 1 shell

Figure 2.9 shows off the Android filesystem and demonstrates looking for a specific installed application, namely our Chapter2 sample application, which we'll be building in the next section.

This capability can be very handy when we want to remove a specific file from the emulator's filesystem, kill a process, or generally interact with the operating environment of the Android Emulator. If you download an application from the internet, for example, you can use the adb command to install an application. For example, adb shell install someapplication.apk installs the application named someapplication to the Android Emulator. The file is copied to the /data/app directory and is accessible from the Android application

Figure 2.9 Using the shell command, we can browse Android's filesystem.

Figure 2.8 The adb tool provides interaction at runtime with the Android Emulator.

« C:\WIMDDWS\system32\cmd.exe -

adb -d 1 shell

Mt\>adb devices

List of devices attached

1 eiwlato^-tcp-BSSS

device 0

2 emwlatojr-tcp-555?

device 0

M;\>adb -d 1 shell

It cd /data/'app

cd /data/app

H Is -1

Is -1

-rw-rw-rw- root root

12982 2008-02






—rif-i---v— system systen tt _

132S833 2007-12-






Figure 2.9 Using the shell command, we can browse Android's filesystem.

launcher. Similarly, if you desire to remove an application, you can run adb to remove an application from the Android Emulator. For example, if you desire to remove the Chapter2.apk sample application from a running emulator's filesystem, you can execute the following command from a terminal or Windows command window:

adb shell rm /data/app/Chapter2.apk

Mastering the command-line tools in the Android SDK is certainly not a requirement of Android application development, but having an understanding of what is available and where to look for capabilities is a good skill to have in your toolbox. If you need assistance with either the aapt or adb command, simply enter the command at the terminal, and a fairly verbose usage/help page is displayed. Additional information on the tools may be found in the Android SDK documentation.

The Android filesystem is a Linux filesystem. While the adb shell command does not provide a very rich shell programming environment as is found on a desktop Linux or Mac OS X system, basic commands such as ls, ps, kill, and rm are available. If you are new to Linux, you may benefit from learning some very basic shell commands.

One other tool you will want to make sure you are familiar with is telnet. Telnet allows you to connect to a remote system with a character-based UI. In this case, the remote system you connect to is the Android Emulator's console. You can accomplish this with the following command:

telnet localhost 5554

In this case, localhost represents your local development computer where the Android Emulator has been started because the Android Emulator relies on your computer's loopback IP address of Why port 5554? Recall when we employed adb to find running emulator instances that the output of that command included a name with a number at the end. The first Android Emulator can generally be found at IP port 5555. No matter which port number the Android Emulator is using, the Android Emulator's console may be found at a port number equaling 1 less. For example, if the Android Emulator is running and listed at port 5555, the console is at port 5554.

Using a telnet connection to the emulator provides a command-line means for configuring the emulator while it is running and testing telephony features such as calls and text messages.

It is time to write an Android application to exercise the development environment we have been discussing.

Was this article helpful?

0 0

Post a comment