Summary of adb utility commands

Posted by plasmahba on Sat, 18 May 2019 05:02:03 +0200

For Android programmers, adb is frequently used in their daily work. Now, we summarize the commonly used adb commands in their work, which is convenient for inquiry and also for your reference. Viewing application memory usage, power consumption, startup time, wakelock, running monkey commands played a small role in previous application performance optimization. The test machine for the following adb command is millet 3, where package_name represents the package name.

Basic scripts:

1. Start the adb service

adb start-server

2. Termination of adb services

adb kill-server

3. Enter the adb Operating Environment

adb shell

4. Introduction of various commands and parameters of adb in getting help

adb help

5. View the adb version

adb version

6. Restart adb with root privilege

adb root

7. Re-hanging system partitions to read-write partitions is important in operating system directories

adb remount

8. Restart the device, optional parameters into bootloader (brush mode) or recovery (recovery mode)

adb reboot [bootloader|recovery]

apk related:

1. Installing apk

Adb install test. apk - R overrides installation, retains data and caches file - d solves low version problem - s installs apk to sd card

2. Unloading apk

adb uninstall -k <package_name>

Optional parameter - k is used to uninstall the software but retain configuration and cache files

3. View all app-related information, including action,codepath,version, required permissions, etc.

adb shell dumpsys package <package_name>

4. View app paths

adb shell pm path <package_name>

Look at the path of an ordinary app, as follows, a common app under data/app

5. View version information of apk

adb shell dumpsys package <package_name> | grep version

If you get two versions of the system app in the figure below, then the system app is the version of the system app itself, and the version information of the upgraded system app is above.

versionCode=8 targetSdk=22     versionName=V0.08 versionCode=6 targetSdk=22     versionName=V0.0

6. Start the activity

adb shell am start -n <package_name>/.<activity_class_name>

7. Getting the start-up time of the application can easily get the start-up time of the application.

adb shell am start -W <package_name>/.<activity_class_name>

The results are as follows:

adb shell am start -W Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] } Status: ok Activity: ThisTime: 355 TotalTime: 355 WaitTime: 365 Complete

Several results are returned, whichever is WaitTime, from startActivity to the time when the first frame is fully displayed.

8. Start the service. The - n parameter of AM represents the component, the - a parameter represents the command, and the later parameter of - A is the action of service defined in manifest.

adb shell am startservice -n <package_name>/.<service_class_name>


adb shell am startservice -a "android.intent.action.CALL"

9. Broadcasting

adb shell am broadcast -a "android.intent.action.AdupsFota.WriteCommandReceiver"

Broadcasting can take different types of parameters, - es is a string parameter type, - ei is an int parameter type, and - ez is a boolean parameter type.

adb shell am broadcast -a "android.intent.action.AdupsFota.WriteCommandReceiver" --es test_string "this is test string"

10. View the process-related information of an app

adb shell ps <package_name|PID>

for example

adb shell ps  USER      PID   PPID  VSIZE  RSS   WCHAN            PC  NAME u0_a116   5483  304   1776564 55112 sys_epoll_ 00000000 S

On another device, if the parameter behind ps is the package name, it will not show the details of the process. See the following way to get it.


adb shell ps | grep <package_name>

11. Kill a process, usually to simulate a bug replication

adb shell kill pidNumber

12. View the memory footprint of an app

adb shell dumpsys meminfo <package_name|PID>

The results are as follows: Heap size includes Dalvik Heap and Native Heap. Usually what we call memory limitation is Dalvik Heap.

 Pss  Private  Private  Swapped     Heap     Heap     Heap                  Total    Dirty    Clean    Dirty     Size    Alloc     Free                 ------   ------   ------   ------   ------   ------   ------   Native Heap    18956    18940        0     4696    44288    21352    22935   Dalvik Heap    60102    60088        0    26192   104486    88285    16201  App Summary                        Pss(KB)                         ------            Java Heap:    61640          Native Heap:    18940                 Code:     3356                Stack:      428             Graphics:    16876        Private Other:     3840               System:     2031 ...

13. Viewing the maximum memory limit for a single application

adb shell getprop | grep heapgrowthlimit

The results are 128M: [dalvik.vm.heapgrowth hlimit]: [128m]

This means that when the maximum value of Dalvik Heap size exceeds 128M, OOM is likely to occur.

14. Obtaining Power Consumption Information for a Single Application
Battery Historian was introduced in Android 5.0. The following command is to obtain the power consumption information of a single app and the system power consumption information. See the next section.

adb shell dumpsys batterystats > <package_name> > xxx.txt

The power information above is the original data, which can be converted into a readable html file through the script written by google, similar to the list data generated by TraceView, which played an important role in app performance optimization before.

python xxx.txt > xxx.html

15. Running monkey, I like this command very much. In the process of running, the application will constantly switch the screen, according to the different levels of feedback information selected, you can also see the execution process report and generated events. It is practical to test the stability of the application. Now studio also has monkeyrunner's tool

ADB shell monkey-v-p < package_name > 500-p object package-v feedback information level
:Monkey: seed=1493061025112 count=500 :AllowPackage: :IncludeCategory: android.intent.category.LAUNCHER :IncludeCategory: android.intent.category.MONKEY ** No activities found to run, monkey aborted.

System related

1. Check the name of the device, pea pod and other applications to get the name of the device.

adb shell cat /system/build.prop/


ro.product.model=MI 3W ro.product.brand=Xiaomi

2. There are two ways to view the resolution of mobile phones. The second way is the most concise.

adb shell dumpsys window | grep Surface

grep is a very useful parameter, the specific meaning and use of our own google, the test results are 1080 * 1920:

Surface: shown=false layer=21000 alpha=1.0 rect=(0.0,0.0) 1080.0 x 1920.0


adb shell wm size

The result is as follows:

Physical size: 1080x1920

3. View sdk version of mobile phone

adb shell getprop | grep version

After running the above command, the listed version []: [6.0.1] is the sdk version of the mobile phone.

4. Viewing Mobile Phone Model Information

adb shell getprop | grep product

After running this command, you can see the models of product,board,brand, cpu, etc.

5. Get the serial number, which is the serial number listed in adb devices.

adb get-serialno

6. Viewing Connected Devices

adb devices

If you have multiple devices connected and want to operate on one of them, you need to add parameters after this command.

ADB [- D | - e | - s < serial Number >] < command > - d: real machine (only one real machine in multiple devices applies) - e: simulator (only one simulator in multiple devices applies) - s: serial number

If there are two real machines connected to my computer, the data adb devices get are as follows

List of devices attached 1b71651    device 12sdfsd   device

The command to enter the 1b71651 device is:

adb -s 1b71651 shell

7. View wifi passwords (root privileges are required)

adb shell cat data/misc/wifi/*.conf

8. View wifi_mac

adb shell cat /sys/class/net/wlan0/address

Run this command, 93:a1:a2:91:d1:c1 is the wifi_mac address of millet 3

9. Viewing Background services Information

adb shell service list

The results are as follows:

Found 126 services: 0 [miui.whetstone.INetworkService] 1    miui.whetstone.power: [miui.whetstone.power] ...

10. Review the current memory usage of the system

adb shell cat /proc/meminfo

Operation results:

MemTotal:        1893504 kB MemFree:           81200 kB Buffers:           14828 kB Cached:           244152 kB SwapCached:        15152 kB Active:           541680 kB Inactive:         575280 kB ...

11. There are several ways to view the detailed memory footprint of each process and the memory footprint of the system.


adb shell dumpsys meminfo
Total PSS SWAP by process:     97858 kB:   34944 kB: (pid 2769)     ... Total RAM: 1893504 kB (status normal) Free RAM: 344212 kB (117992 cached pss + 136016 cached kernel + 90204 free)

Total PSS information is the size of memory your application really occupies. Through this information, you can easily identify which programs in the mobile phone occupy more memory.

11.2 Another way to view the memory of processes is not supported by all devices.

adb shell procrank

The results are as follows:

PID       Vss      Rss      Pss      Uss  cmdline   496  1810184K   92744K   57607K   48348K  system_server   743  1847492K   84492K   52117K   46796K  ....                            ------   ------  ------                           328259K  261484K  TOTAL  RAM: 2061012K total, 889152K free, 40940K buffers, 612032K cached, 300K shmem, 62556K slab


VSS-Virtual Set Size Virtual Consumption Memory (including the memory occupied by shared libraries) RSS-Resident Set Size Actual Use Physical Memory (including the memory occupied by shared libraries) PSS-Proportional Set Size Actual Use Physical Memory (memory occupied by shared libraries) USS-Unique Set Size Process Individual Occupation of Physical Memory (excluding shared libraries) Memory used)

11.3 View the cpu and memory usage of processes on the device

adb shell top

12. Check the power consumption of the system

adb shell dumpsys batterystats > xxx.txt

13. View the alarm clock set by the system

adb shell dumpsys alarm

14. Looking at the wakelock of the system, unreasonable use of wakelock will lead to increased power consumption of the system.

adb shell dumpsys power

Returns the result:

Wake Locks: size=2   PARTIAL_WAKE_LOCK              'AudioMix' (uid=1013, pid=236, ws=WorkSource{10018})   PARTIAL_WAKE_LOCK              '' ON_AFTER_RELEASE (uid=10018, pid=24023, ws=null)

Document operations are relevant:

1. Copy files/directories to devices

adb push <local>...<remote>

2. Copy files/directories from devices, -a parameter preserves the timestamp and mode of files

adb pull [-a] <remote>...<local>

3. View device log, which is the same as studio and eclipse logcat, and can control the output log by parameters.

Adb logcat-s filter specified parameter log-v time retention log time > Add write > overwrite write

The following command means that all logs that print out the time in the log information and contain the keyword "Test" are saved to the test file by overwriting.

adb logcat -v time -s Test > test.txt

4. List the files and folders under the directory. Optional parameter - al can view the details of the files and folders.

adb shell ls [-al]

5. Enter folders

adb shell cd <folder>

6. Viewing Files

adb shell cat <filename>

7. Rename files

adb shell rename path/oldfilename path/newfilename

8. Delete Files/Folders

The ADB shell RM path/filename-r optional parameter is used to delete the folder and all files eg: ADB shell rm-r < folder >

9. Mobile files

adb shell mv path/filename newpath/filename

10. Copying documents

adb shell cp file newpath/file1

11. Create directories

adb shell mkdir path/folder

12. Setting the Maximum Read and Write Permission for Files

adb shell chmod 777 filename

13. Mobile phone does not have root to view data/data/an app file information

The author's millet 3 has no root, but also wants to view some files in the data/data/directory conveniently. If you enter the data directly, you will be prompted to have no permission. The way to view is to enter the data/data/and run the following command, you can directly enter the package of your application. It can be copied or moved to the sdcard directory by general cp or mv for other operations.

run-as package_name

Database correlation

The operation of the database has little to do with today's topic. Additions, deletions and modifications are no longer listed. But usually in the process of development, we just look at a table or a field in the database, and we don't need to pull it out every time before looking at it. It's more convenient and quick to use the command line. After entering the directory of test.db, run the following command
1, operation db

sqlite3 test.db

2. After using sqlite3 command on db, check various usage instructions through. help


3. List the table names of databases


4. The schema of the list database. I think the following two commands are very useful and have the same effect. It is convenient and quick to view the data types corresponding to the database fields. The schema is more concise.



select * from sqlite_master where type = "table";

ps: Original articles, reprinted with origin

Change from:

Topics: shell Android Mobile Database