Adb經常使用命令小結

基礎

adb version  顯示 adb 版本 
adb help  幫助 
adb start-server  啓動 adb
adb kill-server  中止 adb 服務器
複製代碼

一.設備控制

-d	指定當前惟一經過 USB 鏈接的 Android 設備爲命令目標
-e	指定當前惟一運行的模擬器爲命令目標
-s <serialNumber>	指定相應 serialNumber 號的設備/模擬器爲命令目標

adb devices 顯示設備
adb reboot 重啓
adb -s 選擇設備

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

複製代碼

二.安裝卸載

adb install  path_to_apk
adb uninstall packageName
複製代碼

三.文件複製到設備

adb pull remote local
adb push local remote

複製代碼

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

adb push foo.txt /sdcard/foo.txt
複製代碼

四.Shell 在目標模擬器/設備實例中啓動遠程 shell

不進入模擬器/設備實例上的 adb 遠程 shell 發出設備命令:shell

adb [-d|-e|-s serial_number] shell shell_command
複製代碼

進入模擬器/設備實例上的遠程 shell:瀏覽器

adb [-d|-e|-s serial_number] shell
複製代碼

當您準備退出遠程 shell 時,按 Control + D 或輸入 exit。緩存

shell 命令二進制文件存儲在模擬器或設備的文件系統中,其路徑爲 /system/bin/安全

4.1 獲取設備信息

adb shell getprop
複製代碼

4.2 Window manager

adb shell wm
複製代碼

幫助bash

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

返回結果:
Physical size: 1080x2160

複製代碼

4.3 am Activity Manager

在 adb shell 中,您可使用 Activity Manager (am) 工具發出命令以執行各類系統操做,如啓動 Activity、強行中止進程、廣播 intent、修改設備屏幕屬性及其餘操做微信

4.3.1 經常使用命令:
啓動 
adb shell am start -n 包名/activity名(全路徑)
關閉
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 參數的規範。

選項包括:

-D:啓用調試。
-W:等待啓動完成。
--start-profiler file:啓動分析器並將結果發送到 file。
-P file:相似於 --start-profiler,但當應用進入空閒狀態時分析中止。
-R count:重複 Activity 啓動 count 次數。在每次重複前,將完成頂部 Activity。
-S:啓動 Activity 前強行中止目標應用。
--opengl-trace:啓用 OpenGL 函數的跟蹤。
--user user_id | current:指定要做爲哪一個用戶運行;若是未指定,則做爲當前用戶運行。

複製代碼
4.3.3 startService 啓動 intent 指定的 Service
startservice [options] intent
複製代碼
4.3.4 other
broadcast [options] intent	發出廣播 intent
force-stop package	強行中止與 package(應用的包名稱)關聯的全部應用
kill [options] package	終止與 package(應用的包名稱)關聯的全部進程。此命令僅終止可安全終止且不會影響用戶體驗的進程
kill-all	終止全部後臺進程
clear-debug-app	使用 set-debug-app 清除之前針對調試用途設置的軟件包

...
...
複製代碼
4.3.5 intent 參數的規範
對於採用 intent 參數的 Activity Manager 命令,您可使用如下選項指定 intent:

-a action
指定 intent 操做,如「android.intent.action.VIEW」。此指定只能聲明一次。
-d data_uri
指定 intent 數據 URI,如「content://contacts/people/1」。此指定只能聲明一次。
-t mime_type
指定 intent MIME 類型,如「image/png」。此指定只能聲明一次。
-c category
指定 intent 類別,如「android.intent.category.APP_CONTACTS」。
-n component
指定帶有軟件包名稱前綴的組件名稱以建立顯式 intent,如「com.example.app/.ExampleActivity」。
-f flags
將標誌添加到 setFlags() 支持的 intent。


複製代碼

4.4 pm 調用軟件包管理器

查看幫助session

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:查看它們的關聯文件。
-d:進行過濾以僅顯示已停用的軟件包。
-e:進行過濾以僅顯示已啓用的軟件包。
-s:進行過濾以僅顯示系統軟件包。
-3:進行過濾以僅顯示第三方軟件包。
-i:查看軟件包的安裝程序。
-u:也包括卸載的軟件包。
--user user_id:要查詢的用戶空間。
例:
adb shell pm list packages
adb shell pm list packages -s 系統app
adb shell pm list packages -3 第三方app
複製代碼
4.4.2 install and uninstall
install [options] path	將軟件包(經過 path 指定)安裝到系統。
選項:

-l:安裝具備轉發鎖定功能的軟件包。
-r:從新安裝現有應用,保留其數據。
-t:容許安裝測試 APK。
-i installer_package_name:指定安裝程序軟件包名稱。
-s:在共享的大容量存儲(如 sdcard)上安裝軟件包。
-f:在內部系統內存上安裝軟件包。
-d:容許版本代碼降級。
-g:授予應用清單中列出的全部權限。


uninstall [options] package	從系統中移除軟件包。
選項:

-k:移除軟件包後保留數據和緩存目錄。
複製代碼
4.4.3 清除數據和緩存
adb shell pm clear 包名

例 清除qq數據
1|shell@JDtab:/ $ pm clear com.tencent.mobileqq
複製代碼

4.5 ps 進程查看和操做

幫助app

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 關鍵字  :顯示進程
adb shell cat/proc/進程id/oom_adj  顯示進程優先級
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	光標左移	KEYCODE_DPAD_LEFT
22	光標右移	KEYCODE_DPAD_RIGHT
67	刪除	KEYCODE_DEL


複製代碼

4.7 截圖和錄屏

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

錄屏
若是不設置時間默認3分鐘, 使用 --time-limit 設置截屏時間
adb shell screenrecord /sdcard/我是視頻.mp4 --time-limit 12
錄屏幫助
screedrecord --help

截圖和錄屏完畢後可退出 shell,使用  `adb pull /sdcard/我是視頻.mp4` 把文件從手機複製到電腦


複製代碼

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 將系統數據轉儲到屏幕

developer.android.com

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

選項說明
-t timeout	指定時間 默認10秒
--help	 查看幫助
-l	列出可用參數列表
--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服務相關信息
dumpsys window  WMS服務相關信息
dumpsys cpuinfo CPU信息
dumpsys meminfo 內存信息

複製代碼
  • 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 //服務
    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   
	查看微信全部服務
 	adb shell dumpsys activity s com.tencent.mm

複製代碼

詳細Adb經常使用命令

相關文章
相關標籤/搜索