• No results found

Android’s development environment

2.2 Exploring the development environment

2.2.3 Command-line tools

The Android SDK ships with a collection of command-line tools, which are located in the tools subdirectory of your Android SDK installation. Eclipse and the ADT provide a great deal of control over the Android development environment, but sometimes it’s nice to exercise greater control, particularly when considering the power and conve- nience that scripting can bring to a development platform. Next, we’re going to explore two of the command-line tools found in the Android SDK.

TIP It’s a good idea to add the tools directory to your search path. For exam- ple, 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 might be wondering just how files such as the layout file main.xml get processed and exactly where the R.java file comes from. Who zips up the application file for you into the apk file? Well, you might have already guessed the answer from the heading of this section—it’s the Android Asset Packaging Tool, or as it’s called from the command line, aapt. This versatile tool combines the functionality of pkzip or jar along with an Android-specific resource compiler. Depending on the command-line options you provide to it, aapt wears a number of hats and assists with your design-time Android development tasks. To learn the functionality available in aapt, run it from the com- mand line with no arguments. A detailed usage message is written to the screen.

Whereas aapt helps with design-time tasks, another tool, the Android Debug Bridge, assists you at runtime to interact with the Android emulator.

41

Exploring the development environment

ANDROID DEBUG BRIDGE

The Android Debug Bridge (adb) utility permits you to interact with the Android emula- tor directly from the command line or script. Have you ever wished you could navigate the filesystem on your smartphone? Now you can with the adb! The adb works as a client/server TCP-based application. Although a couple of background processes run on the development machine and the emulator to enable your functionality, the important thing to understand is that when you run adb, you 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 returns a list of available Android emulators; figure 2.8 demonstrates adb locating two running emulator sessions.

Let’s connect to the first Android emulator session and see if your application is installed. You connect to a device or emulator with the syntax adbshell. You would connect this way if you had a single Android emulator session active, but because two emulators are running, you need to specify the serial number, or identifier, to connect to the appropriate session:

adb –s "serialnumber" shell

Figure 2.9 shows off the Android filesystem and demonstrates looking for a specific installed application, namely our chapter2 sample application, which you’ll build in section 2.3.

Using the shell can be handy when you want to remove a specific file from the emulator’s filesystem, kill a process, or generally interact with the operating environ- ment of the Android emulator. If you download an application from the internet, for example, you can use the adb command to install the application:

adb [-s serialnumber] shell install someapplication.apk

This command installs the application named someapplication to the Android emu- lator. The file is copied to the /data/app directory and is accessible from the Android application launcher. Similarly, if you want to remove an application, you can run adb to remove an application from the Android emulator. If you want to remove the

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

com.manning.unlockingandroid.apk sample application from a running emulator’s filesystem, for example, you can execute the following command from a terminal or Windows command window:

adb shell rm /data/app/com.manning.unlockingandroid.apk

You certainly don’t need to master the command-line tools in the Android SDK to develop applications in Android, but understanding what’s 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, enter the command at the terminal, and a fairly ver- bose usage/help page is displayed. You can find additional information about the tools in the Android SDK documentation.

TIP The Android filesystem is a Linux filesystem. Though the adb shell command doesn’t provide a rich shell programming environment, as you find on a Linux or Mac OSX system, basic commands such as ls, ps, kill, and rm are available. If you’re new to Linux, you might benefit from learning some basic shell commands.

TELNET

One other tool you’ll want to make sure you’re familiar with is telnet. Telnet allows you to connect to a remote system with a character-based UI. In this case, the remote sys- tem you connect to is the Android emulator’s console. You can connect to it 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 127.0.0.1. Why port 5554? Recall that when we employed adb to find running emulator instances, 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 5554.

43

Building an Android application in Eclipse

NOTE In early versions of the Android SDK, the emulator ran at port 5555 and the Android console—where we could connect via Telnet—ran at 5554, or one number less than the number shown in DDMS. If you’re having diffi- culty identifying which port number to connect on, be sure run netstat on your development machine to assist in finding the port number. Note that a physical device listens at port 5037.

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

So far you’ve learned about the Eclipse environment and some of the command- line elements of the Android tool chain. At this point, it’s time to create your own Android application to exercise this development environment.