Adb shell 常用命令
1. 查看IP
adb shell netcfg
2. 查看挂载设备
adb devices
3. 将本地端口转发至手机端口
adb forward tcp:6100 tcp:7100 // PC上所有6100端口通信数据将被重定向到手机端7100端口server上 或者 adb forward tcp:6100 local:logd // PC上所有6100端口通信数据将被重定向到手机端UNIX类型socket上
4. 打开指定应用
am start -n {包(package)名}/{包名}.{活动(activity)名称}
5. 启动相关应用
calendar(日历)的启动方法为: # am start -n com.android.calendar/com.android.calendar.LaunchActivity AlarmClock(闹钟)的启动方法为: # am start -n com.android.alarmclock/com.android.alarmclock.AlarmClock Music 和 Video(音乐和视频)的启动方法为: # am start -n com.android.music/com.android.music.MusicBrowserActivity # am start -n com.android.music/com.android.music.VideoBrowserActivity # am start -n com.android.music/com.android.music.MediaPlaybackActivity Camera(照相机)的启动方法为: # am start -n com.android.camera/com.android.camera.Camera Browser(浏览器)的启动方法为: # am start -n com.android.browser/com.android.browser.BrowserActivity 启动浏览器 : #am start -a android.intent.action.VIEW -d http://www.google.cn/ 拨打电话 : #am start -a android.intent.action.CALL -d tel:10086 启动 google map 直接定位到北京 : #am start -a android.intent.action.VIEW geo:0,0?q=beijing 一般情况希望,一个Android应用对应一个工程。值得注意的是,有一些工程具有多个活动(activity),而有一些应用使用一个工程。例如:在Android界面中,Music和Video是两个应用,但是它们使用的都是packages/apps/Music这一个工程。而在这个工程的AndroidManifest.xml文件中,有包含了不同的活动(activity)。
6. 安装应用
adb install APK_FILE //安装应用, APK_FILE 可以是包名,也可以是apk名 adb install -r APK_FILE //重新安装应用
7. 卸载应用
adb uninstall APK_FILE
8. 启动应用的某一个页面
adb shell am start PACKAGE_NAME/ACTIVITY_IN_PACKAGE
adb shell am start PACKAGE_NAME/FULLY_QUALIFIED_ACTIVITY
9. 进入设备shell
adb shell
10. 截屏:获得一个屏幕截图且使用shell screencap通过perl输出到本地目录。参照:博客
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
11. 解锁
adb shell input keyevent 82
12 显示日志
adb logcat //显示所有日志 adb logcat -s TAG_NAME //根据标签过滤 adb logcat -s TAG_NAME_1 TAG_NAME_2 //example adb logcat -s TEST adb logcat -s TEST MYAPP adb logcat "*:PRIORITY"//显示特定优先级的日志 // example adb logcat "*:W" //优先级说明: # V — 细则 (最低优先级),D — 调试,I — 信息,W — 警告 # E — 错误,F — 致命,S — 静默 (最高优先级,不会打印任何信息) adb logcat | grep "SEARCH_TERM" //使用grep过滤日志 adb logcat | grep "SEARCH_TERM_1\|SEARCH_TERM_2" //example adb logcat | grep "Exception" adb logcat | grep "Exception\|Error"
13 清除日志
adb logcat -c
以下内容来说官网:
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, referredto 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/Device Instances for more information. |
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 , dumpstate , and logcat 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> port-forwarding specification to connect to a specific JDWP process. For example:adb forward tcp:8000 jdwp:472 jdb -attach localhost:8000 |
|
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> <local> |
Copies a specified file from an emulator/device instance to your development computer. | ||
push <local> <remote> |
Copies a specified file from your development computer to an emulator/device instance. | ||
Ports and Networking | forward <local> <remote> |
Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance. | Port specifications can use these schemes:
|
ppp <tty> [parm]... |
Run PPP over USB.
Note that you should not automatically start a PDP connection. |
||
Scripting | get-serialno |
Prints the adb instance serial number string. | See Querying for Emulator/Device 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 isdevice . |
You can prepend this command to other adb commands, in which caseadb
will wait until the emulator/device instance is connected beforeissuing
the other commands. Here's an example:
adb wait-for-device shell getpropNote that this command does notcause adb to wait until the entire system is fully booted. For thatreason, you should not prepend it to other commands that require afully booted system. As an example, the install requires the Android package manager, which is available only after the system is fully booted. A command such as
adb wait-for-device install <app>.apkwould issue the install command as soon as the emulator or device instance connected to the adbserver, but before the Android system was fully booted, so it wouldresult 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 process. | ||
Shell | shell |
Starts a remote shell in the target emulator/device instance. | See Issuing Shell Commands for more information. |
shell [<shellCommand>] |
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. Thesqlite3
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 invokingsqlite3
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:
$ 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, useexit
orCTRL+D
.
UI/Application Exerciser Monkey
The Monkey is a program that runs on your emulator or device and generates pseudo-randomstreams 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 yourapplication 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 Monkey documentation 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 emulatorinstance and use theadb -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 Service (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 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 andviewing system debug output. Logs from various applications andportions of the system are collected in a series of circular buffers,which then can be viewed and filtered by thelogcat
command.
Using logcat Commands
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>] ...
The sections below explain filter specifications and the command options. See Listing of logcat Command Options for a summary of options.
You can use the logcat
command from your developmentcomputer or from a remote adb shell in an emulator/device instance. Toview log output in your development computer, you use
$ adb logcat
and from a remote adb shell you use
# logcat
Filtering Log Output
Every Android log message has a tag and a priority associated with it.
- The tag of a log message is a short string indicating the systemcomponent from which the message originates (for example, "View" forthe view system).
- The priority is one of the following character values, ordered from lowest to highest priority:
V
— Verbose (lowest priority)D
— DebugI
— InfoW
— WarningE
— ErrorF
— FatalS
— Silent (highest priority, on which nothing is ever printed)
You can obtain a list of tags used in the system, together with priorities, by runninglogcat
and observing the first two columns of each message, given as<priority>/<tag>
.
Here's an example of logcat output that shows that the message relates to priority level "I" and tag "ActivityManager":
I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}
To reduce the log output to a manageable level, you can restrict log output usingfilter expressions.Filter expressions let you indicate to the system the tags-prioritycombinations that you are interested in — the system suppresses othermessages for the specified tags.
A filter expression follows this format tag:priority ...
, where tag
indicates the tag of interest and priority
indicates the minimumlevel of priority to report for that tag. Messages for that tag at orabove the specified priority are written to the log. You can supply anynumber oftag:priority
specifications in a single filter expression. The series of specifications is whitespace-delimited.
Here's an example of a filter expression that suppresses all logmessages except those with the tag "ActivityManager", at priority"Info" or above, and all log messages with tag "MyApp", with priority"Debug" or above:
adb logcat ActivityManager:I MyApp:D *:S
The final element in the above expression, *:S
, setsthe priority level for all tags to "silent", thus ensuring only logmessages with "View" and "MyApp" are displayed. Using*:S
is an excellent way to ensure that log output is restricted to thefilters that you have explicitly specified — it lets your filters serveas a "whitelist" for log output.
The following filter expression displays all log messages with priority level "warning" and higher, on all tags:
adb logcat *:W
If you're running logcat
from your development computer(versus running it on a remote adb shell), you can also set a defaultfilter expression by exporting a value for the environment variableANDROID_LOG_TAGS
:
export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
Note that ANDROID_LOG_TAGS
filter is not exported to the emulator/device instance, if you are runninglogcat
from a remote shell or usingadb shell logcat
.
Controlling Log Output Format
Log messages contain a number of metadata fields, in addition to thetag and priority. You can modify the output format for messages so thatthey display a specific metadata field. To do so, you use the-v
option and specify one of the supported output formats listed below.
brief
— Display priority/tag and PID of originating process (the default format).process
— Display PID only.tag
— Display the priority/tag only.thread
— Display process:thread and priority/tag only.raw
— Display the raw log message, with no other metadata fields.time
— Display the date, invocation time, priority/tag, and PID of the originating process.long
— Display all metadata fields and separate messages with a blank lines.
When starting logcat
, you can specify the output format you want by using the-v
option:
[adb] logcat [-v <format>]
Here's an example that shows how to generate messages in thread
output format:
adb logcat -v thread
Note that you can only specify one output format with the -v
option.
Viewing Alternative Log Buffers
The Android logging system keeps multiple circular buffers for logmessages, and not all of the log messages are sent to the defaultcircular buffer. To see additional log messages, you can startlogcat
with the-b
option, to request viewing of an alternate circular buffer. You can view any of these alternate buffers:
radio
— View the buffer that contains radio/telephony related messages.events
— View the buffer containing events-related messages.main
— View the main log buffer (default)
The usage of the -b
option is:
[adb] logcat [-b <buffer>]
Here's an example of how to view a log buffer containing radio and telephony messages:
adb logcat -b radio
Viewing stdout and stderr
By default, the Android system sends stdout
and stderr
(System.out
andSystem.err
) output to/dev/null
.Inprocesses that run the Dalvik VM, you can have the system write a copyof the output to the log file. In this case, the system writes themessages to the log using the log tagsstdout
andstderr
, both with priorityI
.
To route the output in this way, you stop a running emulator/device instance and then use the shell commandsetprop
to enable the redirection of output. Here's how you do it:
$ adb shell stop $ adb shell setprop log.redirect-stdio true $ adb shell start
The system retains this setting until you terminate theemulator/device instance. To use the setting as a default on theemulator/device instance, you can add an entry to/data/local.prop
on the device.
Listing of logcat Command Options
Option | Description |
---|---|
-b <buffer> |
Loads an alternate log buffer for viewing, such as event or radio . The main buffer is used by default. See Viewing Alternative Log Buffers. |
-c |
Clears (flushes) the entire log and exits. |
-d |
Dumps the log to the screen and exits. |
-f <filename> |
Writes log message output to <filename> . The default is stdout . |
-g |
Prints the size of the specified log buffer and exits. |
-n <count> |
Sets the maximum number of rotated logs to <count> . The default value is 4. Requires the-r option. |
-r <kbytes> |
Rotates the log file every <kbytes> of output. The default value is 16. Requires the-f option. |
-s |
Sets the default filter spec to silent. |
-v <format> |
Sets the output format for log messages. The default is brief format. For a list of supported formats, seeControlling Log Output Format. |
Stopping the adb Server
In some cases, you might need to terminate the adb server processand 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 theproblem.
To stop the adb server, use the kill-server
. You can then restart the server by issuing any adb command.