Sei sulla pagina 1di 5

_____________________________________________________________________________________________ Let's Try  | Developers

Android Application Development


CLI Tools
This second article in the series on Android Application Development introduces you to the
command line utilities that come packaged with the Android SDK, all of which are extensively used
to create an Android Application.

W
hen using an IDE (Integrated Adb
Development Environment) like Adb stands for ‘Android Debug Bridge’. Adb has a client-
Eclipse, the developer never realises server mechanism, where the ‘client’ is the Android
the existence of these utilities, because Emulator instance, or the Android device that’s connected
IDEs provide an abstraction between the developer and to the PC, and the ‘server’ is your development machine
the implementation of the process to create an application (PC). The server communicates with client(s) through a
package. We, being open source enthusiasts and Linux daemon process which runs on the PC. The adb server
users, should know how to build an application from can have multiple clients: for example, two emulator
the command line, and also be aware of exactly what instances and one Android phone connected via USB. To
is going on behind the scenes. This gives us a better find clients available on the development machine, run
understanding of the process of creating an Android the following command:
application and will also help in debugging.
Under Tools directory in the folder where the Android $ adb devices
SDK is installed there are many command-line utilities.
Let’s look at those that are used most often in the Figure 1 shows sample results of running the
following sections of this article: command.
ƒƒ adb
ƒƒ android Having obtained the list of clients, we can run a
ƒƒ ddms command targeting a particular client/instance, by adding
ƒƒ emulator the -s <serial-number> parameter to the command,
ƒƒ mksdcard where serial-number is one of the device names listed by
ƒƒ sqlite3 the adb devices command. For example:
ƒƒ traceview
$ adb -s emulator-5554 shell

Note: The following invocations/


Figure 2 shows the result of running such a
command lines assume that you have added the
command. But to install an Android application into the
Android Tools directory to your $PATH, and
connected device, the adb command given can be one of
thus do not need to precede each command with
several, including…
an absolute path to its location.
ƒƒ install <.apk file>: Installs the given Android

www.LinuxForU.com  | LINUX For You  |  November 2010  |  63


Developers  |  Let's Try ____________________________________________________________________________________________________
application package on the device.
ƒƒ shell: Presents you the adb client’s console.
ƒƒ logcat: Displays the contents of the device’s system log.
To see all the commands that you can use with adb, visit:
http://goo.gl/pQ0s
Figure 1: Adb listing devices
android
This is a very important tool for Android application
development. It lets you create and delete Android Virtual
Devices (AVDs/emulators), create Android projects,
and update the Android platform that you have on your Figure 2: Adb command with serial parameter
development machine.
When you create a new AVD/emulator instance, android
creates a dedicated folder for your AVD, with a user-data
image, SD-card image, a mapping to the system image, and
some other files required by each AVD. Each AVD does not
contain a separate copy of the system image, but a line in the
config.ini file maps the AVD to the system image, which is the
target of the AVD. Before creating an AVD, you need to know
the available Android targets supported by your version of the
SDK. To find this, run the following command:

$ android list targets


Figure 3: Listing of Android targets in SDK
Figure 3 shows the output of running this command. Once
you decide the Android version target ID for your AVD—for
example, 1 for Android 2.1 (Eclair), or 3 for Android 2.2
(Froyo)--you can create the AVD with, for example, the
command given below:

$ android create avd -n myandy_2.2 -t 3

Here, -n specifies the name of the AVD, and -t the target ID of


the Android version for the AVD. Figure 4 shows you a sample of
the queries/prompts for the new AVD’s hardware profile.
The android list avd command lists the AVDs created on
Figure 4: Prompts while creating an AVD
the system; to delete an AVD, use…

android delete avd -n <avd_name>.

The android utility is also used to create and update Android


projects. The Eclipse IDE lets you create a new Android Project
in a very user-friendly way, but internally, after collecting the
required information, it actually issues a command like the one
below (whose output is shown in Figure 5):

$ android create project --target 3 \


--name HelloWorld \
--path ./Workspace/MyAndroidAppProject \
--activity HelloWorldActivity \
--package home.saket Figure 5: Creating a project

You can update the Android platform on your ons, etc, by launching the SDK and AVD Manager with the
development machine with the latest Android releases, add- simple command, android. This is illustrated in Figure 6.

64  |  NOVEMBER 2010  | LINUX For You  |  www.LinuxForU.com


___________________________________________________________________________________________________ Let's Try  | Developers

Figure 6: Launching the Android SDK and AVD Manager

Figure 7: DDMS window Figure 8: DDMS Sysinfo tab

Figure 9: Emulator: simulated incoming call Figure 10: Emulator instance control console

DDMS on the emulator, and also see the CPU load, etc, on the
The Dalvik Debug Monitoring Service is a debugger tool that SysInfo tab (see Figure 8). You can even test an application’s
lets you debug an application running on the Android device, handling of incoming phone calls by simulating a call to the
remotely, which is your development machine. To launch it, emulator instance, by entering the ‘calling’ phone number in
run ddms. Figure 7 shows what the DDMS screen looks like. the ‘Incoming Number’ field on the Emulator Control tab.
When the debugger launches, it automatically attaches Figure 9 is a screen capture of the emulator, showing the
to running emulator instances/devices. You can view the result of this action.
processes running on each instance in the left pane. The right We can also view the heap status of the device, by causing
pane has tabs to monitor process and emulator properties. You garbage collection on the device, with the following steps:
can see the memory usage of each of the applications running 1. Select a process from the left pane.

www.LinuxForU.com  | LINUX For You  |  NOVEMBER 2010  |  65


Developers  |  Let's Try ____________________________________________________________________________________________________

Figure 11: Traceview information [traceview.png]


instances are in the range of 10.0.2.0 – 10.0.2.99.
2. On the VM Heap tab, if you see, “No heap updates are
available for this client,” then click the Show Heap
Updates button at the top of the left pane, and then click Note: To access the development machine’s localhost,
the Cause GC button on the VM Heap tab. it is by default set to 10.0.2.2.
You will now be able to see the heap update status,
showing available memory on the device, along with some Once the emulator instance is running, we can connect to
extra information about the heap. the instance’s control console using a simple telnet connection
like telnet localhost <console_port>. For example, telnet
Emulator localhost 5554. Type help at the Android console for a list of
This tool is used to launch and control an emulator instance available commands, as shown in Figure 10. Similarly, there
(an instance of an AVD created by the android utility). To are plenty of options available for the emulator; read about
launch an AVD, run… them at http://goo.gl/Mmqh.

emulator -avd <avd name> Mksdcard


This tool lets you create an SD card disk image, with a
For example, emulator -avd myandy_2.2. We can FAT32 file system, which you can load into the emulator.
use the emulator utility to control various hardware The syntax is…
simulations of the emulator instance, like networking,
media, etc, using start-up options—additional parameters mksdcard -l <label> <size>[K][M] <file>
on the emulator command-line. The general syntax is
emulator -avd <avd name> <options> <values>. Some For example: mksdcard -l S 512M saket.img. To ‘insert’
of the commonly-used options are -logcat, to view the this SD card image into an emulator instance, we use:
console log while Android is booting; -shell to open a emulator @myandy_2.2 -sdcard saket.img.
root shell on the emulator instance; and -no-boot-anim to
disable the Android logo animation during boot. Sqlite3
This command is used to enter the SQLite database system.
Networking in the emulator You can use it to create and modify databases present in your
When an emulator instance is powered up, it always runs emulator instance or device. To run the SQLite client, obtain
behind a virtual router/firewall and hence is isolated from the a shell in the emulator instance and run the sqlite3 command.
development environment. IP addresses used by emulator For example:

66  |  NOVEMBER 2010  | LINUX For You  |  www.LinuxForU.com


___________________________________________________________________________________________________ Let's Try  | Developers
$ emulator @myandy_2.2 -shell emulator instance, you need to retrieve
# sqlite3 the trace file and feed it to the traceview
sqlite3> .help utility, as follows:

Generally, when you have a $ adb pull /sdcard/HelloWorld.trace /tmp


database-based Android application, $ traceview /tmp/HelloWorld.trace
the application database is stored in
/data/data/<application package>/ Now, you will be presented with a
databases. For example, the email traceview window as shown in Figure
application’s database is in the 11, which is divided into two sections:
folder /data/data/com.android. ƒƒ The timeline section, which shows
email/databases. you the execution time of each
At the sqlite3 prompt, you thread in corresponding rows, and
can perform data definition and ƒƒ The profile section, which shows
manipulation statements with SQL you the time that is spent by the
syntax. For example: CPU inside a method, with some
statistics included in separate
sqlite3> create table employee( columns.
emp_id int, Some other tools that are used
emp_name varchar(20) internally are:
); ƒƒ dx: To convert a .class file to a .dex
sqlite3> .tables file.
employee ƒƒ Monkey: To stress-test an Android
application, this device console tool
issues random events to a given
Traceview application, such as touch, gesture,
This tool is used to profile or click events.
applications. To be able to use it, To run Monkey on the default Android
there are some requirements for your email application, in a new emulator
application—its code must have a instance, issue the following command:
Debug.startTracingMethod(String
tracename) method call at $ emulator @andy_2.2
the beginning, and a Debug. $ adb shell monkey -p com.android.email
stopTracingMethod() call at the end -v 500
of the method. This method stores
the tracing information onto an SD Here, Monkey is passed on the
card—hence, your emulator instance application package name (com.
should have an SD card image android.email), and the number of tests
attached. The tracing writes a file (500) to run.
at /sdcard/<tracename>.trace and I hope you enjoyed this article,
hence we need to give the application and that the knowledge I’ve shared
permission to write the file on the is helpful in your quest to learn more
SD card. This permission is given in about Android. I will be back with
AndroidManifest, as: other interesting information on
Android Application Development
<uses-permission android:name=”android. in upcoming issues. We will
permission.WRITE_EXTERNAL_STORAGE” /> also discuss building advanced
applications on Android and for
When the application is run in the Android. 

By: Saketkumar Srivastav


The author is an open source enthusiast and Android developer. Find him online at
http://bit.ly/saket_blog and http://bit.ly/saket_wordpress.

www.LinuxForU.com  | LINUX For You  |  NOVEMBER 2010  |  67

Potrebbero piacerti anche