adb命令基本操作

來源:互聯網
上載者:User

ADB (Android Debug Bridge)
說明:下面一些命令需要有root許可權才能執行成功
快速啟動dos視窗執行adb:
1. adb.exe所在路徑添加到系統內容變數中
2. 配置快速鍵啟動dos
進入C:\WINDOWS\system32目錄下,找到cmd.exe.
右擊菜單 "發送到" -> 案頭捷徑。
在案頭上右擊"捷徑 到 cmd.exe" -> "屬性" -> "捷徑"頁
-> 游標高亮"快速鍵" -> 按下自訂快速鍵 (如:Ctrl + Alt + Z)

任何情況下,按下Ctrl + Alt + Z啟動dos視窗就可以執行adb命令了

-----------查看裝置串連狀態 系列-----------
adb get-serialno   擷取裝置的ID和序號serialNumber
adb devices 查詢當前電腦上串連那些裝置(包括模擬器和手機),輸出格式: [serialNumber] [state]
adb get-state  查看模擬器/設施的目前狀態.

說明:
序號[serialNumber]——由adb建立的一個字串,這個字串通過自己的控制連接埠<type>-<consolePort>
唯一地識別一個模擬器/裝置執行個體。一個序號的例子: emulator-5554

-----------發送命令到裝置 系列-----------
adb [-d|-e|-s <serialNumber>] <command>
-d 發送命令給usb串連的裝置
-e 發送命令到模擬器裝置
-s <serialNumber> 發送命令到指定裝置

如啟動手機裝置shell: adb -d shell 

adb forward <local> <remote>發布連接埠,可以設定任意的連接埠號碼,
做為主機向模擬器或裝置的請求連接埠。如:adb forward tcp:5555 tcp:8000

adb reboot 重啟手機
adb remount 將system分區重新掛載為可讀寫分區
adb kill-server 終止adb服務進程
adb start-server 重啟adb服務進程
adb root 已root許可權重啟adb服務
adb wait-for-device 在模擬器/裝置串連之前把命令轉載在adb的命令器中
adb jdwp 查看指定的設施的可用的JDWP資訊.
可以用 forward jdwp:<pid> 連接埠映射資訊來串連指定的JDWP進程.例如:
adb forward tcp:8000 jdwp:472
jdb -attach localhost:8000

adb shell am 命令可以啟動應用程式

adb shell input text <string>  向裝置輸入文本(游標所在的文字框)
adb shell input keyevent <event_code> 向裝置發送按鍵事件
如:      
在編輯簡訊時,往文字框輸入文本:adb shell input text "hello" 
向手機發送索引值回Home:adb shell input keyevent 3
event_code 參考view/KeyEvent.java中的  KEYCODE_*
    public static final int KEYCODE_SOFT_LEFT       = 1;
    public static final int KEYCODE_SOFT_RIGHT      = 2;
    public static final int KEYCODE_HOME            = 3;
    public static final int KEYCODE_BACK            = 4;
    public static final int KEYCODE_CALL            = 5;
    public static final int KEYCODE_ENDCALL         = 6;
   
-----------安裝卸載 系列-----------
adb install [-l] [-r] <file> - push this package file to the device and install it
                               ('-l' means forward-lock the app)
                               ('-r' means reinstall the app, keeping its data)
adb uninstall [-k] <package> - remove this app package from the device
                               ('-k' means keep the data and cache directories)
如:
adb install d:\hello.apk
adb unstall com.huawei.hello
說明:如果帶-r選項重新安裝apk時,安裝在 /data/local/tmp/目錄下,手機重啟後還是使用原來的apk.

-----------檔案操作 系列-----------
adb push <local> <remote>    - copy file/dir to device
adb pull <remote> <local>    - copy file/dir from device
 
-----------基本linux shell命令 系列-----------
adb shell [command]
ls 列出目錄下的檔案和檔案夾
cd 切換目錄
rm 刪除目錄和檔案
cat  查看檔案內容
ps 可以看那個進程再跑
ps -x [PID] 查看單個進程的狀態
top 可以看那個進程的佔用率最高
su 切換到root使用者
kill [pid] 殺死一個進程
chmod 777 <file> 修改該檔案為可執行許可權

詳細使用方式可以登入一台Linux伺服器在shell下查看協助手冊,  man <command>
 
-----------查看系統狀態和資訊 系列-----------
adb shell procrank 查詢各進程記憶體使用量情況
adb shell service list 查看services資訊
adb shell cat /proc/meminfo 查看當前的記憶體情況
adb shell cat /proc/cpuinfo 查看CPU資訊(硬體)
adb shell cat /proc/iomem  查看IO記憶體分區

adb shell getprop 列出系統所有屬性
adb shell getprop | findstr "gsm" 列出包含gsm的屬性
adb shell setprop <key> <value>  修改系統屬性
 
adb shell sqlite3 可以執行sql語句查看資料庫資訊, 具體使用方式待調查
 
-----------Log 系列-----------
adb logcat [ <filter-spec> ] - View device log

1~~~~~~~~~~~查看可用日誌緩衝區:
adb logcat -b radio — 查看緩衝區的相關的資訊.
adb logcat -b events — 查看和事件相關的的緩衝區.
adb logcat -b main — 查看主要的日誌緩衝區

2~~~~~~~~~~~過濾日誌輸出:
過濾器語句按照下面的格式描tag:priority ... , tag 表示是標籤, priority 是表示標籤的報告的最低等級
adb logcat *:W  顯示優先順序為warning或更高的日誌資訊
adb logcat ActivityManager:I MyApp:D *:S

日誌的標籤是系統組件原始資訊的一個簡要的標誌。(比如:“View”就是查看系統的標籤).
優先順序有下列集中,是按照從低到高順利排列的:
V — Verbose (lowest priority)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent (highest priority, on which nothing is ever printed)

如果你電腦上運行logcat ,相比在遠程adbshell端,你還可以為環境變數ANDROID_LOG_TAGS :輸入一個參數來設定預設的過濾
export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
需要注意的是ANDROID_LOG_TAGS 過濾器如果通過遠程shell運行logcat 或用adb shell logcat 來運行模擬器/裝置不能輸出日誌.

3~~~~~~~~~~~控制日誌輸出格式:
日誌資訊包括了許多中繼資料域包括標籤和優先順序。可以修改日誌的輸出格式,所以可以顯示出特定的中繼資料域。可以通過 -v 選項得到格式化輸出日誌的相關資訊.

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.
當啟動了logcat ,你可以通過-v 選項來指定輸出格式:

[adb] logcat [-v <format>]
下面是用 thread 來產生的日誌格式:

adb logcat -v thread
需要注意的是你只能-v 選項來規定輸出格式 option.

4~~~~~~~~~~~Logcat命令列表
-b <buffer>  載入一個可使用的日誌緩衝區供查看,比如event 和radio . 預設值是main 。具體查看Viewing Alternative Log Buffers. 
-c  清楚螢幕上的日誌. 
-d  輸出日誌到螢幕上.
-f <filename>  指定輸出日誌資訊的<filename> ,預設是stdout .
-g  輸出指定的日誌緩衝區,輸出後退出. 
-n <count>  設定日誌的最大數目<count> .,預設值是4,需要和 -r 選項一起使用。
-r <kbytes>  每<kbytes> 時輸出日誌,預設值為16,需要和-f 選項一起使用. 
-s  設定預設的過濾層級為silent. 
-v <format>  設定日誌輸入格式,預設的是brief 格式,要知道更多的支援的格式,參看Controlling Log Output Format 

adb bugreport                - return all information from the device
                                 that should be included in a bug report.

adb shell dmesg     查詢核心緩衝區資訊
adb shell dumpstate 各類資訊,比如進程資訊,記憶體資訊,進程是否異常,kernnel的log等
adb shell dumpcrash
adb shell dumpsys  查詢所有service的狀態

-----------其他 -----------

模擬器使用鏡像sdcard
用SDK裡的mksdcard工具來建立FAT32磁碟鏡像並在模擬器啟動時載入它。這樣建立鏡像:? mksdcard <size> <file>,
比如我要建立一個64M的SD卡類比檔案,檔案路徑是在D:\workspace\sdcard.img
             mksdcard  64000000 D:\workspace\sdcard.img

Emulator  –sdcard  D:\workspace\sdcard.img
或者在eclipse的run菜單的open run dialog對話方塊中配置啟動參數。

#top
Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]
    -m num  Maximum number of processes to display.
    -n num  Updates to show before exiting.
    -d num  Seconds to wait between updates.
    -s col  Column to sort by (cpu,vss,rss,thr).
    -t      Show threads instead of processes.
    -h      Display this help screen.
   
   ********* simple selection *********  ********* selection by list *********
-A all processes                      -C by command name
-N negate selection                   -G by real group ID (supports names)
-a all w/ tty except session leaders  -U by real user ID (supports names)
-d all except session leaders         -g by session OR by effective group name
-e all processes                      -p by process ID
T  all processes on this terminal     -s processes in the sessions given
a  all w/ tty, including other users  -t by tty
g  OBSOLETE -- DO NOT USE             -u by effective user ID (supports names)
r  only running processes             U  processes for specified users
x  processes w/o controlling ttys     t  by tty
*********** output format **********  *********** long options ***********
-o,o user-defined  -f full            --Group --User --pid --cols --ppid
-j,j job control   s  signal          --group --user --sid --rows --info
-O,O preloaded -o  v  virtual memory  --cumulative --format --deselect
-l,l long          u  user-oriented   --sort --tty --forest --version
-F   extra full    X  registers       --heading --no-heading --context
                    ********* misc options *********
-V,V  show version      L  list format codes  f  ASCII art forest
-m,m,-L,-T,H  threads   S  children in sum    -y change -l format
-M,Z  security data     c  true command name  -c scheduling class
-w,w  wide output       n  numeric WCHAN,UID  -H process hierarchy

netstat -ano 查看網路連狀態
顯示協議統計資訊和當前 TCP/IP 網路連接。

NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] [-r] [-s] [-v] [interval]

  -a            顯示所有串連和監聽連接埠。
  -b            顯示包含於建立每個串連或監聽連接埠的
                可動作項目。在某些情況下已知可動作項目
                擁有多個獨立組件,並且在這些情況下
                包含於建立串連或監聽連接埠的組件序列
                被顯示。這種情況下,可動作項目名
                在底部的 [] 中,頂部是其調用的組件,
                等等,直到 TCP/IP 部分。注意此選項
                可能需要很長時間,如果沒有足夠許可權
                可能失敗。
  -e            顯示乙太網路統計資訊。此選項可以與 -s
                選項組合使用。
  -n            以數字形式顯示地址和連接埠號碼。
  -o            顯示與每個串連相關的所屬進程 ID。
  -p proto      顯示 proto 指定的協議的串連;proto 可以是
                下列協議之一: TCP、UDP、TCPv6 或 UDPv6。
                如果與 -s 選項一起使用以顯示按協議統計資訊,proto 可以是下列協議之一:
                IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 或 UDPv6。
  -r            顯示路由表。
  -s            顯示按協議統計資訊。預設地,顯示 IP、
                IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 和 UDPv6 的統計資訊;
                -p 選項用於指定預設情況的子集。
  -v            與 -b 選項一起使用時將顯示包含於
                為所有可動作項目建立串連或監聽連接埠的
                組件。
  interval      重新顯示選定統計資訊,每次顯示之間
                暫停時間間隔(以秒計)。按 CTRL+C 停止重新
                顯示統計資訊。如果省略,netstat 顯示當前
                配置資訊(只顯示一次)
               
               
pm
usage: pm [list|path|install|uninstall]
       pm list packages [-f]
       pm list permission-groups
       pm list permissions [-g] [-f] [-d] [-u] [GROUP]
       pm list instrumentation [-f] [TARGET-PACKAGE]
       pm list features
       pm path PACKAGE
       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] PATH
       pm uninstall [-k] PACKAGE
       pm enable PACKAGE_OR_COMPONENT
       pm disable PACKAGE_OR_COMPONENT

The list packages command prints all packages.  Options:
  -f: see their associated file.

The list permission-groups command prints all known
permission groups.

The list permissions command prints all known
permissions, optionally only those in GROUP.  Options:
  -g: organize by group.
  -f: print all information.
  -s: short summary.
  -d: only list dangerous permissions.
  -u: list only the permissions users will see.

The list instrumentation command prints all instrumentations,
or only those that target a specified package.  Options:
  -f: see their associated file.

The list features command prints all features of the system.

The path command prints the path to the .apk of a package.

The install command installs a package to the system.  Options:
  -l: install the package with FORWARD_LOCK.
  -r: reinstall an exisiting app, keeping its data.
  -t: allow test .apks to be installed.
  -i: specify the installer package name.

The uninstall command removes a package from the system. Options:
  -k: keep the data and cache directories around.
after the package removal.

The enable and disable commands change the enabled state of
a given package or component (written as "package/class").

   
查看stdout 和stderr
在預設狀態下,Android系統有stdout 和 stderr (System.out和System.err )輸出到/dev/null ,
在運行Dalvik VM的進程中,有一個系統可以備份記錄檔。在這種情況下,系統會用stdout 和stderr 和優先順序 I.來記錄日誌資訊

通過這種方法指定輸出的路徑,停止啟動並執行模擬器/裝置,然後通過用setprop 命令遠程輸入日誌

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start系統直到你關閉模擬器/裝置前設定會一直保留,可以通過添加/data/local.prop 可以使用模擬器/裝置上的預設設定

UI/軟體 實驗程式 Monkey
當Monkey程式在模擬器或裝置啟動並執行時候,如果使用者出發了比如點擊,觸摸,手勢或一些系統層級的事件的時候,
它就會產生隨機脈衝,所以可以用Monkey用隨機重複的方法去負荷測試你開發的軟體.
最簡單的方法就是用用下面的命令來使用Monkey,這個命令將會啟動你的軟體並且觸發500個事件.

$ adb shell monkey -v -p your.package.name 500
更多的關於命令Monkey的命令的資訊,可以查看UI/Application Exerciser Monkey documentation page.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.