An overview of the

Android Debug Bridge (ADB) is a general command line tool that is a must for Android developers/testers. For more information on ADB, see Android Debug Bridge, the official Chinese documentation for ADB, which summarizes common commands

Description of the commands involved in the article

  • The commands in this article are only for connecting to one device. If you are connecting to more than one device, you need to specify the device serial number after adb shell -s in the following format:
Format: adb-s225278f8 shell Obtain device serial number: ADB DevicesCopy the code
  • <serial number>: Equipment serial number
  • <package_name>: Indicates the package name of Apk
  • <local>: Indicates the path on the PC
  • <remote>: Path on an Android device
  • <filepath_in_device>: Indicates the file path of the Android device
  • <local_apk_path>: Indicates the PATH of the APK on the PC
  • <package_name>/<main_class>: Apk package name/startup class in the following format
Format: Google. Architecture. The universal /. ActivityMain opened the Apk, and then enter the following command to see: adb shell dumpsys window | grep mCurrentFocusCopy the code

Command summary

All of the following commands refer to Android Debug Bridge, the official Google ADB documentation, which lists the most commonly used commands

Common commands

note The command
Viewing Connected Devices adb devices
View the list of connected devices adb devices -l
The specified device adb -s <serial number> shell

Upload files and download files

note The command
A device that uploads files from a computer adb push <local> <remote>
Files from the device are downloaded to the computer adb pull <remote> <local>

Installing and uninstalling applications

note The command
Install the Apk adb install <local_apk_path>
Install Apk and grant all permissions to Apk adb install -g <local_apk_path>
Overrides the Apk installation and preserves data adb install <local_apk_path>
Degrade the Apk installation adb install -d <local_apk_path>
Uninstall the Apk adb uninstall <package_name>
Unmount Apk and retain data adb shell pm uninstall -k <package_name>
Silent Application Installation adb shell pm install -t -r <local_apk_path>
Uninstalling all applications (including system applications) adb shell pm uninstall -k --user 0 <package_name>

pm

note The command
Display the third application adb shell pm list package -3
Display system application adb shell pm list packages -s
Displays the APK file path and package name list adb shell pm list packages -f
View the APK path adb shell pm path <package_name>
Clear data and cache adb shell pm clear <package_name>

dumpsys

note The command
View the package name on which Apk is running adb shell dumpsys window | grep mCurrentFocus
View the Activity task stack adb shell dumpsys activity activities
View heap allocation adb shell dumpsys meminfo <package_name>
Viewing Application Information adb shell dumpsys package <package_name>
Obtain device display parameters adb shell dumpsys window displays
Clear bugreport information adb shell dumpsys batterystats --reset
Filter keyword information from bugreport The adb shell dumpsys batterystats | grep keywords
Enter dozeModel deep adb shell dumpsys deviceidle force-idle
Enter dozeModel Light adb shell dumpsys deviceidle force-idle light
Exit dozeModel adb shell dumpsys deviceidle unforce
Reactivate the device adb shell dumpsys battery reset
Exit charging state adb shell dumpsys battery unplug
Disable doze mode adb shell dumpsys deviceidle disable

adb shell dumpsys deviceidle whitelist

getprop

note The command
Obtaining the device model adb shell getprop ro.product.model
Obtain the Android version of the device adb shell getprop ro.build.version.release

wm

note The command
Get device screen resolution adb shell wm size
Obtain device screen density (unit: DPI) adb shell wm density

Debugging commands

note The command
View the application process adb shell ps -ef | grep <package_name>
Check the memory usage adb shell ps | grep <package_name> | awk '{ print $2 }'
Check the start time of the Activity ​adb shell am start -W <package_name>/<main_class>
Forcing an application to close ​adb shell am force-stop <package_name>
View the CPU and memory usage of the device in real time adb shell top
View the applications with the highest memory usage in real time adb shell top -m <number>
Run number monkey tests for Apk adb shell monkey -v -p <package_name> <number>
Obtain the MAC address of the device adb shell cat /sys/class/net/wlan0/address
Obtain the memory usage of the device adb shell cat /proc/meminfo

Other commands

note The command
See the log The adb shell logcat | grep 'keyword'
Output logs to log.txt file The adb shell logcat | grep 'keyword' > the TXT
Capture the screen adb shell screencap -p <filepath_in_device>
Recording the screen adb shell screenrecord -p <filepath_in_device>

FAQ Summary

List some common problems and improve them later

1. No permissions for Using ADB in Ubuntu

Running Sudo ADB Devices produces the following

List of devices attached 
2aca417d	no permissions
Copy the code

How to solve no Permissions?

Run the command lsusb when you are not connected to an Android device to take a look at usb in Ubuntu

Bus 002 Device 001: ID 1D6B :0003 Linux Foundation 3.0 Root Hub Bus 001 Device 003: ID 413c:2113 Dell Computer Corp. Bus 001 Device 002: ID 413c:301a Dell Computer Corp. Bus 001 Device 001: ID 1D6b :0002 Linux Foundation 2.0 Root HubCopy the code

Connect your Android device, turn on USB debug mode, and run the command lsusb to check out usb in Ubuntu

Bus 002 Device 001: ID 1D6B :0003 Linux Foundation 3.0 Root Hub Bus 001 Device 003: ID 413c:2113 Dell Computer Corp. Bus 001 Device 002: ID 413c:301a Dell Computer Corp. Bus 001 Device 055: ID 18D1:4EE7 Google Inc. Bus 001 Device 001: ID 1D6b :0002 Linux Foundation 2.0 Root HubCopy the code

By comparing the two results, you can view the information about the newly connected Android device. Note its ID number, which is 18d1:4EE7

Then go to /etc/udev/rules.d/ and check whether the.rules file exists. If not, you can create a new one (name can be arbitrary, no Chinese) and add port information to the file

#  Open a file
sudo vim 51-android.rules

# add port information to 51-Android.rules
SUBSYSTEM=="usb",ATTRS{idVendor}=="18d1",ATTRS{idProduct}=="4ee7",MODE="0666"
Copy the code

In this command, 18d1 and 4ee7 are the Android device information displayed in the previous step respectively. MODE indicates the permission. Run the following command

sudo chmod a+rx /etc/udev/rules.d/51-android.rules
sudo service udev restart
Copy the code

Finally unplug the USB and reconnect, then restart the ADB service by running the following command

sudo adb kill-server
sudo adb start-server
sudo adb devices
Copy the code

If all the above steps are done correctly, run sudo ADB Devices as shown below and you are ready to use ADB to manipulate the device

List of devices attached 
2aca417d  device
Copy the code

reference

  • Google adb official Chinese document
  • Adb common command collation
  • awesome-adb
  • ADB- Common command
  • Optimized for low power consumption mode and application standby mode

conclusion

I am committed to sharing a series of articles related to Android system source code, reverse analysis and algorithm. Each article will be carefully reviewed and combined with new technologies to bring some new thinking. If you like coding like me, I would like to learn it together and look forward to growing up with you

The article lists

Android 10 source code series

  • How is Apk generated
  • Apk installation process
  • 0xA03 Android 10 source code analysis: Apk loading process of resource loading
  • Android 10 source code: Apk
  • Dialog loading and drawing process and use in Kotlin, DataBinding

Tool series

  • Shortcuts to AndroidStudio that few people know
  • Shortcuts to AndroidStudio that few people know
  • All you need to know about ADB commands
  • How to get video screenshots efficiently
  • 10 minutes introduction to Shell scripting
  • How to package Kotlin + Android Databinding in your project

The reverse series

  • Dynamically debug APP based on Smali file Android Studio
  • The Android Device Monitor tool cannot be found in Android Studio 3.2