2019-05-26 Adb常用命令小結(jié)

基礎(chǔ)

adb version  顯示 adb 版本 
adb help  幫助 
adb start-server  啟動 adb
adb kill-server  停止 adb 服務(wù)器

一.設(shè)備控制

-d  指定當(dāng)前唯一通過 USB 連接的 Android 設(shè)備為命令目標(biāo)
-e  指定當(dāng)前唯一運行的模擬器為命令目標(biāo)
-s <serialNumber>   指定相應(yīng) serialNumber 號的設(shè)備/模擬器為命令目標(biāo)

adb devices 顯示設(shè)備
adb reboot 重啟
adb -s 選擇設(shè)備

例:
adb -s emulator-5556 reboot   重啟 emulator-5556

二.安裝卸載

adb install  path_to_apk
adb uninstall packageName

三.文件復(fù)制到設(shè)備

adb pull remote local
adb push local remote

在上述命令中,local 和 remote 指的是開發(fā)計算機(本地)和模擬器/設(shè)備實例(遠程)上目標(biāo)文件/目錄的路徑。例如:

adb push foo.txt /sdcard/foo.txt

四.Shell 在目標(biāo)模擬器/設(shè)備實例中啟動遠程 shell

不進入模擬器/設(shè)備實例上的 adb 遠程 shell 發(fā)出設(shè)備命令:

adb [-d|-e|-s serial_number] shell shell_command

進入模擬器/設(shè)備實例上的遠程 shell:

adb [-d|-e|-s serial_number] shell

當(dāng)您準(zhǔn)備退出遠程 shell 時,按 Control + D 或輸入 exit。

shell 命令二進制文件存儲在模擬器或設(shè)備的文件系統(tǒng)中,其路徑為 /system/bin/

4.1 獲取設(shè)備信息

adb shell getprop

4.2 Window manager

adb shell wm

幫助


Window manager (window) commands:
  help
      Print this help text.
  size [reset|WxH|WdpxHdp]
    Return or override display size.
    width and height in pixels unless suffixed with 'dp'.
  density [reset|DENSITY]
    Return or override display density.
  overscan [reset|LEFT,TOP,RIGHT,BOTTOM]
    Set overscan area for display.
  scaling [off|auto]
    Set display scaling mode.
  dismiss-keyguard


獲取屏幕尺寸命令:
adb shell wm size

返回結(jié)果:
Physical size: 1080x2160

4.3 am Activity Manager

在 adb shell 中,您可以使用 Activity Manager (am) 工具發(fā)出命令以執(zhí)行各種系統(tǒng)操作,如啟動 Activity、強行停止進程、廣播 intent、修改設(shè)備屏幕屬性及其他操作

4.3.1 常用命令:

啟動 
adb shell am start -n 包名/activity名(全路徑)
關(guān)閉
adb shell am force-stop 包名
例:打開瀏覽器
先 adb shell 再:
打開瀏覽器
am start -a android.intent.action.VIEW -d  http://www.google.cn/

4.3.2 start 啟動 intent 指定的 Activity
start [options] intent  啟動 intent 指定的 Activity。
請參閱 intent 參數(shù)的規(guī)范。

選項包括:

-D:啟用調(diào)試。
-W:等待啟動完成。
--start-profiler file:啟動分析器并將結(jié)果發(fā)送到 file。
-P file:類似于 --start-profiler,但當(dāng)應(yīng)用進入空閑狀態(tài)時分析停止。
-R count:重復(fù) Activity 啟動 count 次數(shù)。在每次重復(fù)前,將完成頂部 Activity。
-S:啟動 Activity 前強行停止目標(biāo)應(yīng)用。
--opengl-trace:啟用 OpenGL 函數(shù)的跟蹤。
--user user_id | current:指定要作為哪個用戶運行;如果未指定,則作為當(dāng)前用戶運行。

4.3.3 startService 啟動 intent 指定的 Service
startservice [options] intent
4.3.4 other
broadcast [options] intent  發(fā)出廣播 intent
force-stop package  強行停止與 package(應(yīng)用的包名稱)關(guān)聯(lián)的所有應(yīng)用
kill [options] package  終止與 package(應(yīng)用的包名稱)關(guān)聯(lián)的所有進程。此命令僅終止可安全終止且不會影響用戶體驗的進程
kill-all    終止所有后臺進程
clear-debug-app 使用 set-debug-app 清除以前針對調(diào)試用途設(shè)置的軟件包

...
...
4.3.5 intent 參數(shù)的規(guī)范

對于采用 intent 參數(shù)的 Activity Manager 命令,您可以使用以下選項指定 intent:

-a action
指定 intent 操作,如“android.intent.action.VIEW”。此指定只能聲明一次。
-d data_uri
指定 intent 數(shù)據(jù) URI,如“content://contacts/people/1”。此指定只能聲明一次。
-t mime_type
指定 intent MIME 類型,如“image/png”。此指定只能聲明一次。
-c category
指定 intent 類別,如“android.intent.category.APP_CONTACTS”。
-n component
指定帶有軟件包名稱前綴的組件名稱以創(chuàng)建顯式 intent,如“com.example.app/.ExampleActivity”。
-f flags
將標(biāo)志添加到 setFlags() 支持的 intent。


4.4 pm 調(diào)用軟件包管理器

查看幫助

shell@JDtab:/ $ pm
usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]
       pm list permission-groups
       pm list permissions [-g] [-f] [-d] [-u] [GROUP]
       pm list instrumentation [-f] [TARGET-PACKAGE]
       pm list features
       pm list libraries
       pm list users
       pm path PACKAGE
       pm dump PACKAGE
       pm install [-lrtsfd] [-i PACKAGE] [--user USER_ID] [PATH]
       pm install-create [-lrtsfdp] [-i PACKAGE] [-S BYTES]
               [--install-location 0/1/2]
               [--force-uuid internal|UUID]
       pm install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH]
       pm install-commit SESSION_ID
       pm install-abandon SESSION_ID
       pm uninstall [-k] [--user USER_ID] PACKAGE
       pm set-installer PACKAGE INSTALLER
       pm move-package PACKAGE [internal|UUID]
       pm move-primary-storage [internal|UUID]
       pm clear [--user USER_ID] PACKAGE
       pm enable [--user USER_ID] PACKAGE_OR_COMPONENT
       pm disable [--user USER_ID] PACKAGE_OR_COMPONENT
       pm disable-user [--user USER_ID] PACKAGE_OR_COMPONENT
       pm disable-until-used [--user USER_ID] PACKAGE_OR_COMPONENT
       pm hide [--user USER_ID] PACKAGE_OR_COMPONENT
       pm unhide [--user USER_ID] PACKAGE_OR_COMPONENT
       pm grant [--user USER_ID] PACKAGE PERMISSION
       pm revoke [--user USER_ID] PACKAGE PERMISSION
       pm reset-permissions
       pm set-app-link [--user USER_ID] PACKAGE {always|ask|never|undefined}
       pm get-app-link [--user USER_ID] PACKAGE
       pm set-install-location [0/auto] [1/internal] [2/external]
       pm get-install-location
       pm set-permission-enforced PERMISSION [true|false]
       pm trim-caches DESIRED_FREE_SPACE [internal|UUID]
       pm create-user [--profileOf USER_ID] [--managed] USER_NAME
       pm remove-user USER_ID
       pm get-max-users

pm list packages: prints all packages, optionally only
  those whose package name contains the text in FILTER.  Options:
    -f: see their associated file.
    -d: filter to only show disbled packages.
    -e: filter to only show enabled packages.
    -s: filter to only show system packages.
    -3: filter to only show third party packages.
    -i: see the installer for the packages.
    -u: also include uninstalled packages.

pm list permission-groups: prints all known permission groups.

pm list permissions: 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.

pm list instrumentation: use to list all test packages; optionally
  supply <TARGET-PACKAGE> to list the test packages for a particular
  application.  Options:
    -f: list the .apk file for the test package.

pm list features: prints all features of the system.

pm list users: prints all users on the system.

pm path: print the path to the .apk of the given PACKAGE.

pm dump: print system state associated with the given PACKAGE.

pm install: install a single legacy package
pm install-create: create an install session
    -l: forward lock application
    -r: replace existing application
    -t: allow test packages
    -i: specify the installer package name
    -s: install application on sdcard
    -f: install application on internal flash
    -d: allow version code downgrade
    -p: partial application install
    -g: grant all runtime permissions
    -S: size in bytes of entire session

pm install-write: write a package into existing session; path may
  be '-' to read from stdin
    -S: size in bytes of package, required for stdin

pm install-commit: perform install of fully staged session
pm install-abandon: abandon session

pm set-installer: set installer package name

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

pm clear: deletes all data associated with a package.

pm enable, disable, disable-user, disable-until-used: these commands
  change the enabled state of a given package or component (written
  as "package/class").

pm grant, revoke: these commands either grant or revoke permissions
    to apps. The permissions must be declared as used in the app's
    manifest, be runtime permissions (protection level dangerous),
    and the app targeting SDK greater than Lollipop MR1.

pm reset-permissions: revert all runtime permissions to their default state.

pm get-install-location: returns the current install location.
    0 [auto]: Let system decide the best location
    1 [internal]: Install on internal device storage
    2 [external]: Install on external media

pm set-install-location: changes the default install location.
  NOTE: this is only intended for debugging; using this can cause
  applications to break and other undersireable behavior.
    0 [auto]: Let system decide the best location
    1 [internal]: Install on internal device storage
    2 [external]: Install on external media

pm trim-caches: trim cache files to reach the given free space.

pm create-user: create a new user with the given USER_NAME,
  printing the new user identifier of the user.

pm remove-user: remove the user with the given USER_IDENTIFIER,
  deleting all data associated with that user

1|shell@JDtab:/ $ 
4.4.1 list
list packages [options] filter  輸出所有軟件包,或者,僅輸出包名稱包含 filter 中的文本的軟件包。
 
選項:

-f:查看它們的關(guān)聯(lián)文件。
-d:進行過濾以僅顯示已停用的軟件包。
-e:進行過濾以僅顯示已啟用的軟件包。
-s:進行過濾以僅顯示系統(tǒng)軟件包。
-3:進行過濾以僅顯示第三方軟件包。
-i:查看軟件包的安裝程序。
-u:也包括卸載的軟件包。
--user user_id:要查詢的用戶空間。
例:
adb shell pm list packages
adb shell pm list packages -s 系統(tǒng)app
adb shell pm list packages -3 第三方app
4.4.2 install and uninstall
install [options] path  將軟件包(通過 path 指定)安裝到系統(tǒng)。
選項:

-l:安裝具有轉(zhuǎn)發(fā)鎖定功能的軟件包。
-r:重新安裝現(xiàn)有應(yīng)用,保留其數(shù)據(jù)。
-t:允許安裝測試 APK。
-i installer_package_name:指定安裝程序軟件包名稱。
-s:在共享的大容量存儲(如 sdcard)上安裝軟件包。
-f:在內(nèi)部系統(tǒng)內(nèi)存上安裝軟件包。
-d:允許版本代碼降級。
-g:授予應(yīng)用清單中列出的所有權(quán)限。


uninstall [options] package 從系統(tǒng)中移除軟件包。
選項:

-k:移除軟件包后保留數(shù)據(jù)和緩存目錄。
4.4.3 清除數(shù)據(jù)和緩存
adb shell pm clear 包名

例 清除qq數(shù)據(jù)
1|shell@JDtab:/ $ pm clear com.tencent.mobileqq

4.5 ps 進程查看和操作

幫助

adb shell ps --help

usage: ps [-AadefLlnwZ] [-gG GROUP,] [-k FIELD,] [-o FIELD,] [-p PID,] [-t TTY,] [-uU USER,]

List processes.

Which processes to show (selections may be comma separated lists):

-A  All processes
-a  Processes with terminals that aren't session leaders
-d  All processes that aren't session leaders
-e  Same as -A
-g  Belonging to GROUPs
-G  Belonging to real GROUPs (before sgid)
-p  PIDs (--pid)
-P  Parent PIDs (--ppid)
-s  In session IDs
-t  Attached to selected TTYs
-T  Show threads
-u  Owned by USERs
-U  Owned by real USERs (before suid)

.....

常用進程查看操作

adb shell ps | grep 關(guān)鍵字  :顯示進程
adb shell cat/proc/進程id/oom_adj  顯示進程優(yōu)先級
adb shell kill [pid]

例:顯示所有騰訊app進程

adb shell ps | grep tencent

u0_a156        544   638 1313424  60448 0                   0 S com.tencent.tim:Daemon
u0_a156        623   638 1314488  60152 0                   0 S com.tencent.tim:assist
u0_a182        857   638 1385176  78028 0                   0 S com.tencent.mobileqq:MSF
u0_a165       1636   638 2473248 323928 0                   0 S com.tencent.mm
u0_a165       2204   638 2090388 117860 0                   0 S com.tencent.mm:push
system       10200   637 3924720  40540 0                   0 S com.tencent.soter.soterserver
u0_a156      10335   638 1334496  68572 0                   0 S com.tencent.tim:MSF
u0_a156      23472   638 1396960 107396 0                   0 S com.tencent.tim:mail


4.6 輸入 input

input  <command> [<arg>...]

查看幫助
shell@JDtab:/ $ input help
Error: Unknown command: help
Usage: input [<source>] <command> [<arg>...]

The sources are: 
      mouse
      keyboard
      joystick
      touchnavigation
      touchpad
      trackball
      stylus
      dpad
      touchscreen
      gamepad

The commands and default sources are:
      text <string> (Default: touchscreen)
      keyevent [--longpress] <key code number or name> ... (Default: keyboard)
      tap <x> <y> (Default: touchscreen)
      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      press (Default: trackball)
      roll <dx> <dy> (Default: trackball)
      3dtouch <x> <y> <z>(Default: touchscreen)
shell@JDtab:/ $ 

sources? 好像用不上 主要是commands的使用

模擬輸入文字
adb shell input text "hahah" 注意輸入中文時鍵盤模式需要是中文 同理英文

模擬點擊屏幕
input tap 123 312     

模擬模擬按鍵
input keyevent 3 返回桌面
常用按鍵 event
1   menu    KEYCODE_MENU
3   home    KEYCODE_HOME
4   back    KEYCODE_BACK
21  光標(biāo)左移    KEYCODE_DPAD_LEFT
22  光標(biāo)右移    KEYCODE_DPAD_RIGHT
67  刪除  KEYCODE_DEL


4.7 截圖和錄屏

截圖
adb shell screencap  /sdcard/我是截圖.png  

錄屏
如果不設(shè)置時間默認3分鐘, 使用 --time-limit 設(shè)置截屏?xí)r間
adb shell screenrecord /sdcard/我是視頻.mp4 --time-limit 12
錄屏幫助
screedrecord --help

截圖和錄屏完畢后可退出 shell,使用  `adb pull /sdcard/我是視頻.mp4` 把文件從手機復(fù)制到電腦


4.8 monkey

查看幫助
shell@JDtab:/ $ monkey help                                                    
** Error: Count is not a number
usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
              [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
              [--ignore-crashes] [--ignore-timeouts]
              [--ignore-security-exceptions]
              [--monitor-native-crashes] [--ignore-native-crashes]
              [--kill-process-after-error] [--hprof]
              [--pct-touch PERCENT] [--pct-motion PERCENT]
              [--pct-trackball PERCENT] [--pct-syskeys PERCENT]
              [--pct-nav PERCENT] [--pct-majornav PERCENT]
              [--pct-appswitch PERCENT] [--pct-flip PERCENT]
              [--pct-anyevent PERCENT] [--pct-pinchzoom PERCENT]
              [--pct-permission PERCENT]
              [--pkg-blacklist-file PACKAGE_BLACKLIST_FILE]
              [--pkg-whitelist-file PACKAGE_WHITELIST_FILE]
              [--wait-dbg] [--dbg-no-events]
              [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]
              [--port port]
              [-s SEED] [-v [-v] ...]
              [--throttle MILLISEC] [--randomize-throttle]
              [--profile-wait MILLISEC]
              [--device-sleep-time MILLISEC]
              [--randomize-script]
              [--script-log]
              [--bugreport]
              [--periodic-bugreport]
              [--permission-target-system]
              COUNT

255|shell@JDtab:/ $ 
例:
adb shell monkey -v -p your.package.name 500

五.dumpsys 將系統(tǒng)數(shù)據(jù)轉(zhuǎn)儲到屏幕

developer.android.com

命令格式
adb shell dumpsys [-t timeout] [--help | -l | --skip services | service [arguments] | -c | -h]

選項說明
-t timeout  指定時間 默認10秒
--help   查看幫助
-l  列出可用參數(shù)列表
--skip services Specifies the services that you do not want to include in the output.
service [arguments] Specifies the service that you want to output. Some services may allow you to pass optional arguments. You can learn about these optional arguments by passing the -h option with the service, as shown below:
adb shell dumpsys procstats -h
    

-c  When specifying certain services, append this option to output data in a machine-friendly format.
-h  For certain services, append this option to see help text and additional options for that service.


常用
dumpsys activity AMS服務(wù)相關(guān)信息
dumpsys window  WMS服務(wù)相關(guān)信息
dumpsys cpuinfo CPU信息
dumpsys meminfo 內(nèi)存信息

  • dumpsys activity
查看幫助
adb shell dumpsys activity -h
 
Activity manager dump options:
  [-a] [-c] [-p PACKAGE] [-h] [WHAT] ...
  WHAT may be one of:
    a[ctivities]: activity stack state //查看activity
    r[recents]: recent activities state 
    b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state
    broadcast-stats [PACKAGE_NAME]: aggregated broadcast statistics
    i[ntents] [PACKAGE_NAME]: pending intent state
    p[rocesses] [PACKAGE_NAME]: process state
    o[om]: out of memory management
    perm[issions]: URI permission grant state
    prov[iders] [COMP_SPEC ...]: content provider state
    provider [COMP_SPEC]: provider client-side state
    s[ervices] [COMP_SPEC ...]: service state //服務(wù)
    as[sociations]: tracked app associations
    settings: currently applied config settings
    service [COMP_SPEC]: service client-side state
    package [PACKAGE_NAME]: all state related to given package
    all: dump all activities
    top: dump the top activity
  WHAT may also be a COMP_SPEC to dump activities.
  COMP_SPEC may be a component name (com.foo/.myApp),
    a partial substring in a component name, a
    hex object identifier.
  -a: include all available server state.
  -c: include client state.
  -p: limit output to given package.
  --checkin: output checkin format, resetting data.
  --C: output checkin format, not resetting data.
  --proto: output dump in protocol buffer format.
  --autofill: dump just the autofill-related state of an activity

例:
    查看acitivty信息
    adb shell dumpsys activity activities 或者 adb shell dumpsys activity a   
    查看微信所有服務(wù)
    adb shell dumpsys activity s com.tencent.mm

詳細Adb常用命令

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容