Android Debug Bridge (ADB)
ADB is a command-line tool that lets you communicate with an Android device. It includes three components: Client, Daemon (adbd) and Server. The client is the adb.exe
file in the platform tools
folder. The daemon runs the command on a device and it's a background process on the device. The server manages communication between the client and the daemon and runs as a background process on your development machine.
When you start an adb
client, it first checks whether there is an adb
server already running. If there isn't it starts the server on your machine.
To communicate to your device using adb
first you need to enable USB debugging in your device by tapping 7 times on the Build number in your device setting enabling USB debugging in Developer options.
List of connected devices
adb devices
It'll list the attached devices, with their serial number.
Install an app
adb install path_to_apk
You must use the -t
option when you install a test APK.
Copy files to/from a device
adb pull remote_path local_path
adb push local_path remote_path
adb push foo.txt /sdcard/foo.txt
Stop the adb server
In some cases, you might need to terminate the adb server to resolve an issue if it doesn't respond to a command. To stop the adb server, run the following command and then when you issue a new command it'll start a new server.
adb kill-server
Issue shell commands
You can issue a shell command which will be run on the device. Android provides most of the usual Unix command-line tools. For a list of available tools, use the following command:
adb shell ls /system/bin
Which lists all the binaries in the device which you can run using the shell
command.
Activity manager
Within an adb shell, you can perform various system actions, such as start and activity, force-stop a process, etc.
adb shell am start -a android.intent.action.VIEW
Package manager
To perform actions and queries on app packages installed on the device, use this command. For example:
adb shell pm uninstall com.example.MyApp
For granting or revoking permission we use this command. For example:
adb shell pm grant com.example.MyApp android.permission.READ_PROFILE
adb shell pm revoke com.example.MyApp android.permission.READ_PROFILE
Take screenshot
To take a screenshot and pull it from the device, use the following commands:
adb shell screencap /sdcard/screen.png
adb pull /sdcard/screen.png
Record a video
To record the display of the device, use the following command:
adb shell screenrecord /sdcard/demo.mp4
Stop the screen recording by pressing Control + C
; otherwise, the recording stops automatically at three minutes or the time limit set by --time-limit
.
sqlite
To examine the sqlite databases, use this command:
adb -s emulator-5554 shell
sqlite3 /data/data/com.example.app/databases/rssitems.db
You can use .dump
to print the content of a table and .schema
to print the SQL CREATE
statement for an existing table.