android adb command line tool

24
Mr. Pritesh N. Patel ISTAR Page 1 How to Use Android ADB Command Line Tool Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Android phone. ADB is bundled with Android SDK package that can be downloaded from Android SDK download page. This tool is useful for communicating with Android phone such as install application, copy files from/to device and perform some linux shell commands. How to Install Download and install Java SDK (Ex: jdk-6u20-windows-i586.exe) Download Android SDK package (Ex: android-sdk_r06-windows.zip) Extract SDK package into anywhere on your drive (Ex: D:\android-sdk-windows) Phone Setup In order to use ADB, you have to enable USB Debugging option in phone settings (Settings->Applications->Development) How to Use Connect the phone to your pc/notebook usb port Run Windows command line tool (cmd) and go to D:\android-sdk-windows\tool Update!! New Android SDK puts adb executable file on \platform-tools directory instead of tool. So the path should be D:\android-sdk-windows\platform-tools Usefull Commands 1. Check connected phone Syntax: adb devices 2. Login to Android shell

Upload: pritesh-patel-istar

Post on 22-Nov-2014

4.597 views

Category:

Education


1 download

DESCRIPTION

Android adb command line tool

TRANSCRIPT

Page 1: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 1

How to Use Android ADB Command Line Tool

Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Android phone. ADB is bundled with Android SDK package that can be downloaded from Android SDK download page. This tool is useful for communicating

with Android phone such as install application, copy files from/to device and perform some linux shell commands.

How to Install

Download and install Java SDK (Ex: jdk-6u20-windows-i586.exe)

Download Android SDK package (Ex: android-sdk_r06-windows.zip)

Extract SDK package into anywhere on your drive (Ex: D:\android-sdk-windows)

Phone Setup

In order to use ADB, you have to enable USB Debugging option in phone settings (Settings->Applications->Development)

How to Use

Connect the phone to your pc/notebook usb port

Run Windows command line tool (cmd) and go to D:\android-sdk-windows\tool

Update!!

New Android SDK puts adb executable file on \platform-tools directory instead of tool. So the path should be D:\android-sdk-windows\platform-tools

Usefull Commands

1. Check connected phone

Syntax: adb devices

2. Login to Android shell

Page 2: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 2

Syntax: adb shell

Note:

If you get ‘#’ after typing adb shell, you are already get root access on the phone, no need to type su. Otherwise if you get ‘$’ , type ’su’ to get root access (only for rooted device).

3. Some usefull shell commands

ls

List directory

Syntax: ls [path]

Example:

#ls /system/lib

cp

Copy file or directory

Syntax: cp [options] source dest

Note: To copy or delete files in Android root directories you have to change the

directory access mode to ‘read and write (rw)’ using command: remount rw

Example:

#remount rw

#cp /sdcard/libsec-ril.so /system/lib

#remount ro

mv

Move file or directory

Syntax: mv [options] source dest

Example:

#mv /system/lib/libsec-ril.so /sdcard/backup

chmod

Change file/directory permission

Syntax: chmod [-R] mode[,mode] …. file

Example:

#chmod 0644 /system/lib/libsec-ril.so

rm

Remove file or directory

Syntax: rm [options] file

Example:

#rm /system/lib/libsec-ril.so

4. Install application

You can use adb to install aplication from your local drive into phone. Syntax: adb install appname.apk

Page 3: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 3

Example: D:\android-sdk-windows\tools\adb install D:\AnReboot.apk

5. Copy files from phone to local drive

Syntax: adb pull source [destination] Example: D:\android-sdk-windows\tools\adb pull /sdcard/arm11-dvm.zip

D:\android-sdk-windows\tools\adb pull /sdcard/arm11-dvm.zip D:\

5. Copy files from local drive to phone

Syntax: adb push source destination Example:

D:\android-sdk-windows\tools\adb push D:\AnReboot.apk /sdcard

How to Delete Preinstalled Android Application

When buying an Android phone, sometimes it comes with preinstalled applications

either from manufacturer or network operator. These preinstalled applications can not be easily removed from application manager unless you have root access. If you already have root access, follow the following steps to remove the preinstalled applications:

Login into Android shell, you can use adb tool or terminal application such

as Android Terminal Emulator .

Type su to login as root

Type mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system to enable read-

write mode

Type cd /system/app, to enter the applications directory

Tyle ls to view the list of existing applications

Type rm AppilicationName.apk to delete an application. For example, to delete

Facebook app, just type rm Facebook.apk

Coding Tips: How to Use Custom Transition Animation in Android

Page 4: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 4

Android has a set of Animation API that makes you easily create custom animations in your Android application. You can create animations programmatically via Java code or

defined them in xml files inside /res/anim folder. Using your custom animation, you can override the default transition animation between activities to make your app looks more

elegant. Overriding the default transition animation is easy, just create two custom animations and call the overridePendingTransition method of Activity class to use them.

How to use custom transition animation:

Create two animation files (xml), one for incoming activity (/anim/incoming.xml)

and one for outgoing activity (/anim/outgoing.xml).

incoming.xml

1

2 3 4

5 6

7 8 9

10 11 12

13 14

15 16 17

18 19

<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">

<translate android:interpolator="@android:anim/accelerate_interpolator"

android:fromXDelta="0" android:toXDelta="0"

android:duration="300"/> <scale

android:pivotX="50%" android:pivotY="50%"

android:fromXScale="0" android:fromYScale="0" android:toXScale="1.0"

android:toYScale="1.0" android:startOffset="300" android:duration="300" />

</set>

outgoing.xml

Page 5: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 5

1 2 3

4 5

6 7 8

9 10

11 12 13

14 15 16

17 18

<?xml version="1.0" encoding="UTF-8"?> <set

xmlns:android="http://schemas.android.com/apk/res/android">

<translate android:fromXDelta="0" android:toXDelta="-50%p"

android:duration="300" /> <scale

android:pivotX="50%" android:pivotY="50%"

android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale=".1"

android:toYScale=".1" android:startOffset="300"

android:duration="400" /> </set>

Call method overridePendingTransition (int enterAnim, int exitAnim) right after

startActivity(intent) to override default transition animation between current

activity and next activity.

1 2

3 4

Intent intent = new Intent(this, NextActivity.class);

startActivity(intent); overridePendingTransition (R.anim.incoming, R.anim.outgoing)

To use custom transition animation between current activity and previous activity

(when user press the back key to get back to previous activity) override

the onBackPressed () method and call the overridePendingTransition (int enterAnim,

int exitAnim) method.

1

2 3

4 5

@Override public void onBackPressed() { super.onBackPressed();

overridePendingTransition(R.anim.customanim1, R.anim.customanim2)

}

If you are using an IDE, you’ll probably never realize the existence of the commands that

we are going to talk about, because IDEs provide an abstraction layer between the developer and the process implementation. However, if you happen to be an Android

enthusiast who loves to explore, read on to discover some command-line utilities that are useful both during development and debugging.

Page 6: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 6

The command-line utilities that we shall be talking about are located in the Tools directory of your Android SDK installation. So without further ado, let’s delve

in: Adb Adb means ‘Android Debug Bridge’. Adb has a client-server mechanism, the client being

the Android emulator (or the device connected to your system) and the server being the system itself. Obviously, it can handle multiple client devices. To see the connected client devices, run the following command:

adb devices

You can also suffix parameters to the adb command, like:

adb -s <serial-number-of-client>

Other suffix options include -shell (gives you the client console), -logcat, install <.apk filename> (installs an Android app package), etc. Android This command lets you create Android Virtual Devices (AVD), Android projects and even

update the version of your Android platform.

When you create a new AVD/emulator, android creates a separate folder for the AVD with all the files required by it. To create an AVD, use this command:

android create avd -n <sample> -t <sample number>

-n signifies the name of the AVD while -t stands for the target ID of the Android version. To get the target ID, employ:

android list targets

To list the AVDs available on the system, use:

android list avd

To delete an AVD:

android delete avd -n <name>

DDMS

Page 7: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 7

DDMS stands for Dalvik Debug Monitoring Service. It is a simple debugger tool. You can

launch it by using

ddms

command. When launched, it automatically links itself to the running emulators/AVDs.

In the right pane under the Sysinfo tab, it shows you the memory usage of each application, the CPU load, etc. You can view the running processes in the left pane.

Under the Emulator Control tab, if you enter a number in the ‘Incoming Number’ field, it will simulate an incoming call on the device.

In order to view the heap status, select a process from the left pane, click the ‘Show Heap Updates’ button at the top of the left pane and then click the ‘Cause GC’ button

under the VM Heap tab. You shall be able to see the heap update status and other related info.

Emulator

This command can be used to launch and control an emulator instance. Run:

emulator -avd <name>

This utility supports additional parameters, such as to view the console log:

emulator -avd <name> -logcat

To disable the Android animated logo at startup:

emulator -avd <name> -no-boot-anim

To open a root prompt or shell:

emulator -avd <name> -shell

Page 8: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 8

Every emulator instance, by default, runs behind a virtual router/firewall. The default IP addresses of emulators are between 10.0.2.0 and 10.0.2.99

To connect to the emulator control console, use:

telnet localhost <console-port-num, eg. 3432>

Mksdcard

This command creates an SD card disk image (FAT32 file system):

mksdcard -l <label> <size> <filename>

Eg.:

mksdcard -l SB 1024M mobilemag.img

The above example will create an SD card image named mobilemag.img sized 1 GB with

label SB. To ‘insert’ this card image into an emulator, use:

emualator @<name> -sdcard mobilemag.img

Sqlite3

It is used to enter the SQLite database. It can create/modify/edit databases for the emulator/AVD. The command must be run from the shell prompt:

emulator @<name> -shell

# sqlite3

sqlite3>

More often than not, the application database would be stored in /data/data//databases SQLite3 follows the standard SQL syntax. So to create a table awesomeppl with their

names, use:

sqlite3> create table awesomeppl (pname varchar(20));

Page 9: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 9

Traceview

In order to use Traceview to profile apps, you must pass a Debug.startTracingMethod(String tracename)function call at the beginning and

a Debug.stopTracingMethod() method call at the end of the function. The tracing info will be stored onto the SD card, so your emulator must have an SD card image attached to

it. Thus, the application will need permissions to write data to the card image (via AndroidManifest):

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

The trace file will be /sdcard/ .trace

So now when you run the app, you will need to feed the trace file (say, named mobmag.trace) to the traceviewutility:

adb pull /sdcard/mobmag.trace /tmp

Having pulled the file to /tmp, use:

traceview /tmp/mobmag.trace

A window shall appear showing the timeline (execution time of each thread) and the profile (showing the time spent by the CPU on a given function).

Monkey

This command can stress-test an Android app by issuing random event gestures to it. Say, to run monkey on emulator named sbroy2, use:

emulator @sbroy2

adb shell monkey -p com.android.sample 400

Above, monkey is passed on the app package com.android.sample with the number of

tests being 400. The random test gestures also include touch and/or click events.

Execute Shell Command From Java

String cmd = "ls -al"; Runtime run = Runtime.getRuntime(); Process pr = run.exec(cmd); pr.waitFor();

BufferedReader buf = new BufferedReader(new

InputStreamReader(pr.getInputStream())); String line = ""; while ((line=buf.readLine())!=null) { System.out.println(line); }

Page 10: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 10

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate

with an emulator instance or connected Android-powered device. It is a client-server

program that includes three components:

A client, which runs on your development machine. You can invoke a client from a

shell by issuing an adb command. Other Android tools such as the ADT plugin and

DDMS also create adb clients.

A server, which runs as a background process on your development machine. The

server manages communication between the client and the adb daemon running on

an emulator or device.

A daemon, which runs as a background process on each emulator or device instance.

You can find the adb tool in <sdk>/platform-tools/.

When you start an adb client, the client first checks whether there is an adb server

process already running. If there isn't, it starts the server process. When the server

starts, it binds to local TCP port 5037 and listens for commands sent from adb clients—

all adb clients use port 5037 to communicate with the adb server.

The server then sets up connections to all running emulator/device instances. It locates

emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585,

the range used by emulators/devices. Where the server finds an adb daemon, it sets up

a connection to that port. Note that each emulator/device instance acquires a pair of

sequential ports — an even-numbered port for console connections and an odd-

numbered port for adb connections.

For example:

Emulator 1, console: 5554

Emulator 1, adb: 5555

Emulator 2, console: 5556

Emulator 2, adb: 5557 ...

As shown, the emulator instance connected to adb on port 5555 is the same as the

instance whose console listens on port 5554.

Once the server has set up connections to all emulator instances, you can use adb

commands to control and access those instances. Because the server manages

connections to emulator/device instances and handles commands from multiple adb

clients, you can control any emulator/device instance from any client (or from a script).

The sections below describe the commands that you can use to access adb capabilities

and manage the state of an emulator/device. Note that if you are developing Android

applications in Eclipse and have installed the ADT plugin, you do not need to access adb

from the command line. The ADT plugin provides a transparent integration of adb into

the Eclipse IDE. However, you can still use adb directly as necessary, such as for

debugging.

Issuing adb Commands

You can issue adb commands from a command line on your development machine or

from a script. The usage is:

Page 11: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 11

adb [-d|-e|-s <serialNumber>] <command>

When you issue a command, the program invokes an adb client. The client is not

specifically associated with any emulator instance, so if multiple emulators/devices are

running, you need to use the -d option to specify the target instance to which the

command should be directed. For more information about using this option,

see Directing Commands to a Specific Emulator/Device Instance.

Querying for Emulator/Device Instances

Before issuing adb commands, it is helpful to know what emulator/device instances are

connected to the adb server. You can generate a list of attached emulators/devices using

the devices command:

adb devices

In response, adb prints this status information for each instance:

Serial number — A string created by adb to uniquely identify an emulator/device

instance by its console port number. The format of the serial number is <type>-

<consolePort>. Here's an example serial number: emulator-5554

State — The connection state of the instance. Three states are supported:

o offline — the instance is not connected to adb or is not responding.

o device — the instance is now connected to the adb server. Note that this state does

not imply that the Android system is fully booted and operational, since the

instance connects to adb while the system is still booting. However, after boot-up,

this is the normal operational state of an emulator/device instance.

o

The output for each instance is formatted like this:

[serialNumber] [state]

Here's an example showing the devices command and its output:

$ adb devices

List of devices attached emulator-5554 device

emulator-5556 device emulator-5558 device

If there is no emulator/device running, adb returns no device.

Directing Commands to a Specific Emulator/Device Instance

If multiple emulator/device instances are running, you need to specify a target instance

when issuing adb commands. To so so, use the -s option in the commands. The usage

for the -s option is:

Page 12: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 12

adb -s <serialNumber> <command>

As shown, you specify the target instance for a command using its adb-assigned serial

number. You can use the devicescommand to obtain the serial numbers of running

emulator/device instances.

Here is an example:

adb -s emulator-5556 install helloWorld.apk

Note that, if you issue a command without specifying a target emulator/device instance

using -s, adb generates an error.

Installing an Application

You can use adb to copy an application from your development computer and install it

on an emulator/device instance. To do so, use the install command. With the command,

you must specify the path to the .apk file that you want to install:

adb install <path_to_apk>

For more information about how to create an .apk file that you can install on an

emulator/device instance, see Building and Running

Note that, if you are using the Eclipse IDE and have the ADT plugin installed, you do not

need to use adb (or aapt) directly to install your application on the emulator/device.

Instead, the ADT plugin handles the packaging and installation of the application for

you.

Forwarding Ports

You can use the forward command to set up arbitrary port forwarding — forwarding of

requests on a specific host port to a different port on an emulator/device instance.

Here's how you would set up forwarding of host port 6100 to emulator/device port 7100:

adb forward tcp:6100 tcp:7100

You can also use adb to set up forwarding to named abstract UNIX domain sockets, as

illustrated here:

adb forward tcp:6100 local:logd

Copying Files to or from an Emulator/Device Instance

You can use the adb commands pull and push to copy files to and from an

emulator/device instance's data file. Unlike the install command, which only copies an

Page 13: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 13

.apk file to a specific location, the pull and push commands let you copy arbitrary

directories and files to any location in an emulator/device instance.

To copy a file or directory (recursively) from the emulator or device, use

adb pull <remote> <local>

To copy a file or directory (recursively) to the emulator or device, use

adb push <local> <remote>

In the commands, <local> and <remote> refer to the paths to the target files/directory

on your development machine (local) and on the emulator/device instance (remote).

Here's an example:

adb push foo.txt /sdcard/foo.txt

Listing of adb Commands

The table below lists all of the supported adb commands and explains their meaning and

usage.

Category Command Description Comments

Options -d Direct an adb command

to the only attached USB

device.

Returns an

error if more

than one USB

device is

attached.

-e Direct an adb command

to the only running

emulator instance.

Returns an

error if more

than one

emulator

instance is

running.

-s <serialNumber> Direct an adb command a

specific emulator/device

instance, referred to by its

adb-assigned serial

number (such as

"emulator-5556").

If not specified,

adb generates

an error.

General devices Prints a list of all attached

emulator/device

instances.

See Querying

for

Emulator/Devi

ce

Instances for

more

information.

Page 14: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 14

help Prints a list of supported

adb commands.

version Prints the adb version

number.

Debug logcat [<option>]

[<filter-specs>]

Prints log data to the

screen.

bugreport Prints dumpsys, dumpst

ate, andlogcat data to the

screen, for the purposes

of bug reporting.

jdwp Prints a list of available

JDWP processes on a

given device.

You can use

the forward

jdwp:<pid> por

t-forwarding

specification to

connect to a

specific JDWP

process. For

example:

adb forward

tcp:8000

jdwp:472

jdb -attach

localhost:800

0

Data install <path-to-

apk>

Pushes an Android

application (specified as a

full path to an .apk file) to

the data file of an

emulator/device.

pull <remote> <l

ocal>

Copies a specified

file from an

emulator/device

instance to your

development

computer.

push <local> <re

mote>

Copies a specified

file from your

development

computer to an

emulator/device

instance.

Ports and

Networking

forward <local> <r

emote>

Forwards socket

connections from a

specified local port to a

specified remote port on

the emulator/device

Port

specifications

can use these

schemes:

tcp:<portnu

Page 15: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 15

instance. m>

local:<UNIX

domain

socket

name>

dev:<charac

ter device

name>

jdwp:<pid>

ppp <tty> [parm]... Run PPP over USB.

<tty> — the tty for PPP

stream. For

exampledev:/dev/oma

p_csmi_ttyl.

[parm]... — zero or

more PPP/PPPD

options, such

asdefaultroute, local,n

otty, etc.

Note that you should not

automatically start a PPP

connection.

Scripting get-serialno Prints the adb instance

serial number string.

See Querying

for

Emulator/Devi

ce

Instances for

more

information.

get-state Prints the adb state of an

emulator/device instance.

wait-for-device Blocks execution until the

device is online — that is,

until the instance state

is device.

You can

prepend this

command to

other adb

commands, in

which case adb

will wait until

the

emulator/devic

e instance is

connected

before issuing

the other

commands.

Here's an

example:

adb wait-for-device shell

getprop

Page 16: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 16

Note that this

command

does not cause

adb to wait

until the entire

system is fully

booted. For

that reason,

you should not

prepend it to

other

commands that

require a fully

booted system.

As an example,

the install req

uires the

Android

package

manager,

which is

available only

after the

system is fully

booted. A

command such

as

adb wait-for-device install

<app>.apk

would issue

the install com

mand as soon

as the

emulator or

device instance

connected to

the adb server,

but before the

Android system

was fully

booted, so it

would result in

an error.

Server start-server Checks whether the adb

server process is running

and starts it, if not.

kill-server Terminates the adb server

Page 17: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 17

process.

Shell shell Starts a remote shell in

the target

emulator/device instance.

See Issuing

Shell

Commands for

more

information. shell [<shellComm

and>]

Issues a shell command

in the target

emulator/device instance

and then exits the remote

shell.

Issuing Shell Commands

Adb provides an ash shell that you can use to run a variety of commands on an

emulator or device. The command binaries are stored in the file system of the emulator

or device, in this location:

/system/bin/...

You can use the shell command to issue commands, with or without entering the adb

remote shell on the emulator/device.

To issue a single command without entering a remote shell, use the shell command like

this:

adb [-d|-e|-s {<serialNumber>}] shell <shellCommand>

To drop into a remote shell on a emulator/device instance, use the shell command like

this:

adb [-d|-e|-s {<serialNumber>}] shell

When you are ready to exit the remote shell, use CTRL+D or exit to end the shell

session.

The sections below provide more information about shell commands that you can use.

Examining sqlite3 Databases from a Remote Shell

From an adb remote shell, you can use the sqlite3 command-line program to manage

SQLite databases created by Android applications. The sqlite3 tool includes many useful

commands, such as .dump to print out the contents of a table and .schema to print the

SQL CREATE statement for an existing table. The tool also gives you the ability to

execute SQLite commands on the fly.

To use sqlite3, enter a remote shell on the emulator instance, as described above, then

invoke the tool using thesqlite3 command. Optionally, when invoking sqlite3 you can

specify the full path to the database you want to explore. Emulator/device instances store SQLite3 databases in the folder /data/data/<package_name>/databases/.

Here's an example:

Page 18: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 18

$ adb -s emulator-5554 shell

# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db

SQLite version 3.3.12

Enter ".help" for instructions

.... enter commands, then quit...

sqlite> .exit

Once you've invoked sqlite3, you can issue sqlite3 commands in the shell. To exit and

return to the adb remote shell, use exit or CTRL+D.

UI/Application Exerciser Monkey

The Monkey is a program that runs on your emulator or device and generates pseudo-

random streams of user events such as clicks, touches, or gestures, as well as a number

of system-level events. You can use the Monkey to stress-test applications that you are

developing, in a random yet repeatable manner.

The simplest way to use the monkey is with the following command, which will launch

your application and send 500 pseudo-random events to it.

$ adb shell monkey -v -p your.package.name 500

For more information about command options for Monkey, see the

complete UI/Application Exerciser Monkeydocumentation page.

Other Shell Commands

The table below lists several of the adb shell commands available. For a complete list of

commands and programs, start an emulator instance and use the adb -help command.

adb shell ls /system/bin

Help is available for most of the commands.

Shell Command Description Comments

dumpsys Dumps system data

to the screen.

The Dalvik Debug Monitor

Server (DDMS) tool offers

integrated debug environment

that you may find easier to use.

dumpstate Dumps state to a file.

logcat [<option>]... [<filter-

spec>]...

Enables radio logging

and prints output to

the screen.

dmesg Prints kernel

debugging messages

Page 19: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 19

to the screen.

start Starts (restarts) an

emulator/device

instance.

stop Stops execution of an

emulator/device

instance.

Enabling logcat Logging

The Android logging system provides a mechanism for collecting and viewing system

debug output. Logs from various applications and portions of the system are collected in

a series of circular buffers, which then can be viewed and filtered by

the logcat command.

You can use the logcat command to view and follow the contents of the system's log

buffers. The general usage is:

[adb] logcat [<option>] ... [<filter-spec>] ...

You can use the logcat command from your development computer or from a remote

adb shell in an emulator/device instance. To view log output in your development

computer, you use

$ adb logcat

and from a remote adb shell you use

# logcat

See Reading and Writing Logs for complete information about logcat commend options

and filter specifications.

Stopping the adb Server

In some cases, you might need to terminate the adb server process and then restart it.

For example, if adb does not respond to a command, you can terminate the server and

restart it and that may resolve the problem.

To stop the adb server, use the kill-server. You can then restart the server by issuing

any adb command.

Page 20: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 20

Managing AVDs from the Command Line

The android tool lets you manage AVDs on the command line. For a complete reference

of the command line options that you can use, see the reference for the android tool.

Listing Targets

To generate a list of system image targets, use this command:

android list targets

The android tool scans the <sdk>/platforms/ and <sdk>/add-ons/directories looking

for valid system images and then generates the list of targets. Here's an example of the

command output:

Available Android targets:

id: 1 or "android-3" Name: Android 1.5 Type: Platform

API level: 3 Revision: 4 Skins: QVGA-L, HVGA-L, HVGA (default), HVGA-P, QVGA-P

id: 2 or "android-4" Name: Android 1.6

Type: Platform API level: 4 Revision: 3

Skins: QVGA, HVGA (default), WVGA800, WVGA854 id: 3 or "android-7" Name: Android 2.1-update1

Type: Platform API level: 7

Revision: 2 Skins: QVGA, WQVGA400, HVGA (default), WVGA854, WQVGA432, WVGA800 id: 4 or "android-8"

Name: Android 2.2 Type: Platform

API level: 8 Revision: 2 Skins: WQVGA400, QVGA, WVGA854, HVGA (default), WVGA800, WQVGA432

id: 5 or "android-9" Name: Android 2.3 Type: Platform

API level: 9 Revision: 1

Skins: HVGA (default), WVGA800, WQVGA432, QVGA, WVGA854, WQVGA400

Page 21: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 21

Creating AVDs

In addition to creating AVDs with the AVD Manager user interface, you can also create

them by passing in command line arguments to the android tool.

Open a terminal window and change to the <sdk>/tools/ directory, if needed.

To create each AVD, you issue the command android create avd, with options that

specify a name for the new AVD and the system image you want to run on the emulator

when the AVD is invoked. You can specify other options on the command line also, such

as the emulated SD card size, the emulator skin, or a custom location for the user data

files.

Here's the command-line usage for creating an AVD:

android create avd -n <name> -t <targetID> [-<option> <value>] ...

You can use any name you want for the AVD, but since you are likely to be creating

multiple AVDs, you should choose a name that lets you recognize the general

characteristics offered by the AVD. The target ID is an integer assigned by

theandroid tool. The target ID is not derived from the system image name, version, or

API Level, or other attribute, so you need to run the android list targets command to

list the target ID of each system image. You should do this beforeyou run the android

create avd command. See the android tool documentation for more information on the

command line options.

When you've selected the target you want to use and made a note of its ID, use

the android create avd command to create the AVD, supplying the target ID as the -

t argument. Here's an example that creates an AVD with name "my_android1.5" and

target ID "2" (the standard Android 1.5 system image in the list above):

android create avd -n my_android1.5 -t 2

If the target you selected was a standard Android system image ("Type: platform"),

the android tool next asks you whether you want to create a custom hardware profile.

Android 1.5 is a basic Android platform. Do you wish to create a custom hardware profile [no]

If you want to set custom hardware emulation options for the AVD, enter "yes" and set

values as needed. If you want to use the default hardware emulation options for the

AVD, just press the return key (the default is "no"). The android tool creates the AVD

with name and system image mapping you requested, with the options you specified. For

more information, see Setting Hardware Emulation Options.

Note: If you are creating an AVD whose target is an SDK add-on, the android tool does

not allow you to set hardware emulation options. It assumes that the provider of the

add-on has set emulation options appropriately for the device that the add-on is

modeling, and so prevents you from resetting the options.

Page 22: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 22

Customize the device resolution or density

When testing your application, we recommend that you test your application in several

different AVDs, using different screen configurations (different combinations of size and

density). In addition, you should set up the AVDs to run at a physical size that closely

matches an actual device.

To set up your AVDs for a specific resolution or density, follow these steps:

1. Use the create avd command to create a new AVD, specifying the --skin option with a

value that references either a default skin name (such as "WVGA800") or a custom

skin resolution (such as 240x432). Here's an example:

android create avd -n <name> -t <targetID> --skin WVGA800

2. To specify a custom density for the skin, answer "yes" when asked whether you want

to create a custom hardware profile for the new AVD.

3. Continue through the various profile settings until the tool asks you to specify

"Abstracted LCD density" (hw.lcd.density). Enter an appropriate value, such as "120"

for a low-density screen, "160" for a medium density screen, or "240" for a high-

density screen.

4. Set any other hardware options and complete the AVD creation.

In the example above (WVGA medium density), the new AVD will emulate a 5.8" WVGA

screen.

As an alternative to adjusting the emulator skin configuration, you can use the emulator

skin's default density and add the -dpi-device option to the emulator command line

when starting the AVD. For example:

emulator -avd WVGA800 -scale 96dpi -dpi-device 160

Default location of AVD files

When you create an AVD, the android tool creates a dedicated directory for it on your

development computer. The directory contains the AVD configuration file, the user data

image and SD card image (if available), and any other files associated with the device.

Note that the directory does not contain a system image — instead, the AVD

configuration file contains a mapping to the system image, which it loads when the AVD

is launched.

The android tool also creates an <AVD_name>.ini file for the AVD at the root of

the .android/avd/ directory on your computer. The file specifies the location of the AVD

directory and always remains at the root the .android directory.

By default, the android tool creates the AVD directory inside ~/.android/avd/ (on

Linux/Mac), C:\Documents and Settings\<user>\.android\ on Windows XP,

and C:\Users\<user>\.android\ on Windows 7 and Vista. If you want to use a custom

location for the AVD directory, you can do so by using the -p <path> option when you

create the AVD:

android create avd -n my_android1.5 -t 2 -p path/to/my/avd

If the .android directory is hosted on a network drive, we recommend using the -p option

to place the AVD directory in another location. The AVD's .ini file remains in the .android

directory on the network drive, regardless of the location of the AVD directory.

Page 23: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 23

Setting hardware emulation options

When you are creating a new AVD that uses a standard Android system image ("Type:

platform"), the android tool lets you set hardware emulation options for virtual device.

The table below lists the options available and the default values, as well as the names of

properties that store the emulated hardware options in the AVD's configuration file (the

config.ini file in the AVD's local directory).

Table 1. Available hardware profile options for AVDs and the default values

Characteristic Description Property

Device ram size The amount of physical RAM on

the device, in megabytes. Default

value is "96".

hw.ramSize

Touch-screen

support

Whether there is a touch screen

or not on the device. Default

value is "yes".

hw.touchScreen

Trackball

support

Whether there is a trackball on

the device. Default value is "yes".

hw.trackBall

Keyboard

support

Whether the device has a

QWERTY keyboard. Default value

is "yes".

hw.keyboard

DPad support Whether the device has DPad

keys. Default value is "yes".

hw.dPad

GSM modem

support

Whether there is a GSM modem

in the device. Default value is

"yes".

hw.gsmModem

Camera support Whether the device has a

camera. Default value is "no".

hw.camera

Maximum

horizontal

camera pixels

Default value is "640". hw.camera.maxHorizontalPixels

Maximum

vertical camera

pixels

Default value is "480". hw.camera.maxVerticalPixels

GPS support Whether there is a GPS in the

device. Default value is "yes".

hw.gps

Battery support Whether the device can run on a

battery. Default value is "yes".

hw.battery

Accelerometer Whether there is an

accelerometer in the device.

Default value is "yes".

hw.accelerometer

Audio recording

support

Whether the device can record

audio. Default value is "yes".

hw.audioInput

Audio playback Whether the device can play hw.audioOutput

Page 24: Android adb command line tool

Mr. Pritesh N. Patel ISTAR Page 24

support audio. Default value is "yes".

SD Card

support

Whether the device supports

insertion/removal of virtual SD

Cards. Default value is "yes".

hw.sdCard

Cache partition

support

Whether we use a /cache

partition on the device. Default

value is "yes".

disk.cachePartition

Cache partition

size

Default value is "66MB". disk.cachePartition.size

Abstracted LCD

density

Sets the generalized density

characteristic used by the AVD's

screen. Default value is "160".

hw.lcd.density

Trackball

support

Whether there is a trackball

present.

hw.trackBall

Moving an AVD

If you want to move or rename an AVD, you can do so using this command:

android move avd -n <name> [-<option> <value>] ...

Updating an AVD

If, for any reason, the platform/add-on root folder has its name changed (maybe because

the user has installed an update of the platform/add-on) then the AVD will not be able

to load the system image that it is mapped to. In this case, theandroid list

targets command will produce this output:

The following Android Virtual Devices could not be loaded: Name: foo

Path: <path>/.android/avd/foo.avd Error: Invalid value in image.sysdir. Run 'android update avd -n foo'

To fix this error, use the android update avd command to recompute the path to the

system images.

Deleting an AVD

You can use the android tool to delete an AVD. Here is the command usage:

android delete avd -n <name>

When you issue the command, the android tool looks for an AVD matching the specified

name deletes the AVD's directory and files.