In this guide, we will look at how to use the ADB and Fastboot commands on Android needed to be able to put hands on your Android device to perform some essential functions on the device. First of all, we need to use two important tools that are Android Debug Bridge (ADB) and fastboot that is two programs that can be started via the Windows Command Prompt. Those who have installed the Android SDK on their computer with the Microsoft operating system will be able to find ADB and fastboot programs in the platform tools folder(android_sdk/platform-tools/), programs that can still be found anyway without necessarily installing the entire SDK package as you can install the minimal adb tool that contains only the files needed for ADB and fastboot and can be downloaded from the XDA website. ADB is a versatile command line tool that allows you to communicate with a device. The ADB command will enable you to smoothly perform actions on the device such as access to a Unix shell, useful to be able to execute different commands on a device.
Android debug bridge (ADB), is a versatile command line tool, which allows you to communicate with a device, easily operating certain device functions such as installing and debugging applications and accessing a Unix shell, useful for executing various commands on a device. Android debug bridge (ADB) is a client-server program that includes the following components:
- Client- it is used to send commands from the developer’s computer and it is possible to call it from a command line terminal by sending an adb command
- Demon (adbd): used to execute commands on a device running as a background process on each device
- Server: manages the communication between the client and the daemon. On the developer’s computer, the server runs as a background process
How to use ADB and fastboot commands on Android- how ADB works and fastboot
Now let’s see how ADB works when starting the ADB client, the client must check first if an ADB server process is already running, so as to start the process if it is not detected. The server, once started, will connect to the local TCP port 5037, necessary to be able to communicate with the ADB client and “listen” to all the commands sent by the latter. The connections to all the devices that are running at that moment will then be imposed by the server, identifying the emulators and scanning ports with odd numbers in the range 5555 to 5585, the interval used by the first 16 emulators. When the server finds a daemon (adbd), it proceeds to set up a connection to that port. Why are scans of only odd ports made? The reason is that each emulator uses a pair of sequential ports- a port with an even number for console connections and an odd port instead for ADB connections. Here is an example:
Emulator 1, console- 5554
Emulator 1, adb- 5555
Emulator 2, console- 5556
Emulator 2, adb- 5557
and so on …
The emulator 1, as you can see in the example, is connected to ADB on port 5555, but also to the listening console 5554. Once the server has managed to configure the connections to all the devices, it is possible to use the commands ADB to access these devices. The server, managing the connections to the devices, and managing the commands from multiple ADB clients, allows you to control any device from any client or from a script.
How to use ADB and fastboot commands on Android- how to enable USB debugging on an Android device
How to enable USB debugging on an Android device. In order to use ADB with a device that is connected via USB, USB debugging must be enabled on the operating system settings on Android, under Developer Options. Starting from Android 4.2, the Developer Options item has been hidden by default, but making it visible again is very simple as you just go to Settings> About phone> Press 7 times with your finger on Build Number until you see a small window pop-up indicating that you have become a developer and options associated with it are now active. All you have to do is go back to the Settings, and you should now display the new Developer Options item on which you will need to click on it to access it. However, consider that this step is general, in some devices, the Developer Options screen may be located or be named differently. Once inside the Developer Options screen, you can enable USB debugging to have debug mode when connected via USB and consequently now you can connect your device via USB. To verify that the device is connected, from the directory android_sdk /platform-tools, you need to run adb devices, and if there is a connection, you will see your device listed as a “device”. When connecting an Android device via USB to the computer, in versions 4.2.2.0 and later of Android, the system will display a window asking whether to accept an RSA key, useful for allowing debugging through the computer in use. This is nothing more than a security system, which protects the user’s device by ensuring that USB debugging and other ADB commands cannot be performed unless the device can be unlocked and confirmed.
How to use ADB and fastboot commands on Android- how to connect an Android device via Wi-Fi instead of USB
How to connect an Android device via Wi-Fi instead of USB. Generally ADB is used to communicate with a device via USB, however, this connection cable is not the only way you can use it, in fact, it is possible to use ADB also via Wi-Fi, but after some initial settings made via USB as we will see in the following steps:
- Connect your Android device to the adb host computer to a shared Wi-Fi network (so they must use the same wireless network). During this phase, it is important that the access point has a firewall configured to support ADB
- If you need to connect to a Wear OS device, turn off Bluetooth on the paired phone
- Now connect the Android device to the host computer using the USB cable
- Set the target device for listening to a TCP / IP connection on port 5555 with the adb tcpip 5555 command
- Disconnect the USB cable from the computer
- You must now find the IP address of the Android device. To find it, follow the steps below (general guide for smartphones and tablets) Settings > Device Information > Status > IP Address ; for those who have Wear OS instead- on a Wear OS device, you can find the IP address in Settings > Wi-Fi Settings > Advanced > IP Address
- Now you can connect to the device via its IP address with the adb connect command device_ip_adress (your ip)
- Check now that your host computer is connected to the target device using the following command:
$ adb devices
List of devices attached
device_ip_adress – 5555 device
If you detect the ADB connection ok, otherwise if this is lost you do as follows:
- Make sure your host computer is connected to the exact same Wi-Fi network as your Android device
- Connect again with the adb connect command
- If the ADB connection does not work, reset the ADB host with the adb kill-server command and start again the steps to perform the wireless connection
Once the connection between computer and Android device is present, whether a USB or Wi-Fi connection is used, the ADB commands can be executed. Here are the following:
How to use ADB and fastboot commands on Android- Table 1 – Available adb commands and options
-s
– Install the app on the SD card.-d
– Allows the downgrade of the version code (only for debugging packages).-g
– Grant all runtime permissions.
Global options | Description |
---|---|
-a | Listen on all network interfaces instead of on localhost . |
-d | Directs an adb command on the only connected USB device. Returns an error when more than one USB device is connected. |
-e | Directs an adb command to the only running emulator. Returns an error when more than one emulator is running. |
-s serial_number | Directs an adb command on a specific device, to which the serial number assigned to adb refers (such as emulator-5556). Replaces the serial number value stored in the $ANDROID_SERIAL environment variable. |
-H server | The hostname of the adb server. The default is localhost . |
-P port | The port number of the adb server. The default is 5037 . |
-L socket | Listen on the provided adb server socket. The default is tcp:localhost:5037 . |
General commands | Description |
---|---|
devices [-l] | Print a list of all devices. Use the option -l to include device descriptions. |
help | Print a list of supported adb commands and their descriptions. |
version | Print the adb version number. |
run-as package_name | Execute commands on a device as an app (specified using package_name ). This allows you to execute commands in adb as if the specified app executed the command (that is, you have the same access to the device that the app has), without requiring the log in as root. This may be necessary when using adb on a non-rooted device or an emulator with a Play Store image. The app must be debuggable. |
Network commands | Description | |
---|---|---|
connect host[:port] | Connect to a device over TCP / IP. If you do not specify a port 5555 , the default port is used. | |
disconnect [host | | Log off the specified TCP / IP device running on the specified port. If you do not specify a host or port, all devices are disconnected from all TCP / IP ports. If you specify a host, but not a port 5555 , the default port is used ,. | |
forward --list | List all forwarded socket connections. | |
forward [--no-rebind]local remote | Forward socket connections from the specified local port to the remote port specified on the device. You can specify local and remote ports in the following ways:
| |
forward --remove local | Removes the specified forwarded socket connection. | |
reverse --list | Lists all reverse socket connections from the device. | |
reverse [--no-rebind]remote local | Inverts a socket connection. The --no-rebind option indicates that the inversion fails if the specified socket is already associated with a reverse previous command. You can specify the port for local and remote arguments in the following ways:
| |
reverse --remove remote | Removes the inverted connection specified by the device. | |
reverse --remove-all | Removes all reverse socket connections from the device. | |
File transfer commands | Description | |
push local remote | Copy files and directories from the local device (computer) to a remote location on the device. | |
pull [-a] remote local | Copy remote files and directories to a device. Use -a as an option to preserve the timestamp and file mode. | |
sync [system| | Synchronize a local build from the location specified in the $ANDROID_PRODUCT_OUT device. All modified files are copied from the specified partition. The default is to synchronize all partitions. This command is used only when creating the source of the Android platform. App developers don’t need to use this command $ANDROID_PRODUCT_OUT . The environment variable is automatically set by the Android build system to contain the location of system images. Normally it is not necessary to set it $ANDROID_PRODUCT_OUT when running it adb sync , but it can be useful if you are not in a build tree (but one is available) or if you are synchronizing the build trees without going from one to the other.$ ANDROID_PRODUCT_OUT = / out / target / product / generic adb sync | |
App installation commands | Description | |
install [options]package | Need to install the packages on the device. The possible options are as follows:
| |
install-multiple[options] packages | Same options as install with the addition of the following:-p – Partial installation of the app. | |
uninstall [-k] package | Remove this app package from the device. Add -k as an option to keep the data and directories of the cache. | |
Command backup and restore | Description | |
backup [] [ | ] [ | ] [ | ] [ ] [ | [ ]-f file -apk -noapk -obb -noobb -shared -noshared -all -system -nosystem package_names | Write an archive of device data to file. If you do not specify a file name, the file is the default. The package list is optional when options are specified. The following describes the uses for the other options- backup. -all -shared
| |
restore file | Restore the contents of the device from the file. | |
Debug commands | Description | |
bugreport path | Print bugreport to the specified path. If the path is a directory, then the bug report is saved in that directory using the default file name,. Print on devices that do not support zipped bug reports. bugreport. stdout | |
jdwp | Print a list of JDWP processes available on a given device. To be used to connect to a specific JDWP process. For example:forward jdwp:pid adb forward tcp:8000 jdwp:472 jdb -attach localhost:8000 | |
logcat [-help] [option] [filter-spec] | Print the log data on the screen. For information about the logcat command and the $ANDROID_LOG_TAGS environment variable, see Filtering Log Output on the Logcat Page. The environment $ADB_TRACE variable contains a comma-separated list of debug information to be logged. The values can be any combination of the following: all , adb , sockets , packets , rwx , usb , sync , sysdeps , transport , and jdwp . | |
Security controls | Description | |
disable-verity | Disable build dm-verity control userdebug . The dm-verity is an option ensures that when a user starts a device it is in the same state it was in when it was last used. | |
enable-verity | Reactivate dm-verity control on userdebug build. The dm-verity is an option ensures that when a user starts a device, it is in the same state it was in when it was last used. | |
keygen file | Generate adb encrypted public and private RSA keys. The private key is stored in the file. The public key is stored. An RSA key pair is required when using adb to connect via USB for the first time. You must accept the RSA key of the host computer to grant adb access to the device explicitly. Use the environment variable to point to a file or directory containing pairs of 2048-bit RSA authentication keys generated with the command. These key pairs are added to the RSA key pairs generated by the adb server.file. $ANDROID_VENDOR_KEYS keygen When the adb server needs a key, it first looks in the adb server keystore directory. If no key is found, check the $ANDROID_VENDOR_KEYS environment variable for a location. If no keys are yet found, the local adb server generates and saves a new key pair in the adb server keystore directory. For this reason, only an OEM that creates a new Android device must run 'adb keygen' independently.By default, the key pairs generated by the adb server are stored in the following directories of the key archive as
| |
Scripting commands | Description | |
wait-for [-transport] -state | Wait for the device to be in the specified state.
| |
get-state | Print the adb status of a device. The adb state can be print offline , bootloader or device . | |
get-serialno | Print the string of the serial number of the adb device. | |
get-devpath | Print the path to the adb device. | |
remount | Reassemble i /system , /vendor and /oem partitions in read-write mode. | |
reboot [bootloader | | Restart the device. This command starts automatically when the system image starts but also supports bootloader e recovery .
| |
sideload otapackage | sideload (install in APK format) the complete OTA package specified on the device. | |
root | Restart adbd with root permissions. | |
unroot | Restart adbd without root permissions. | |
usb | Restart the adb server listening on USB. | |
tcpip port-number | Restart the adb server listening on TCP in the specified port. | |
Internal debug commands | Description | |
start-server | Check if the adb server process is running. | |
kill-server | End the adb server procedure. | |
reconnect | Force a reconnection from the host. | |
reconnect device | Force a reconnection from the device to force a reconnection. | |
Shell commands | Description | |
shell | Start a remote interactive shell on the target device. | |
shell -e escape_char [-n] [-T] [-t] [-x] [command] | Issues a shell command to the target device and then exits the remote shell. Use any combination of the following options:
| |
emu command | Execute an emulation console command. |
You can use the command shell
to send device commands via adb, with or without entering the adb remote shell on the device. To issue a single command without entering a remote shell, use the command like shell
this:
adb [-d | -e | -s serial_number ] shell_command shell
Or enter a remote shell on such a device:
adb [-d | -and | -s serial_number ] shell
When you are ready to exit the remote shell, press Ctrl + D or type exit
.
The binaries of the shell command are stored in the device file system in /system/bin/
.
Call activity manager (am). Within an adb shell, you can issue commands with the activity manager tool ( am
) to perform various system actions, such as starting a task, forcing a process to be interrupted, transmitting an intent, changing the properties of the device screen and more. In a shell, the syntax is: am command
You can also send a task management command directly from ADB without inserting a shell
adb shell am start -a android.intent.action.VIEW
How to use ADB and fastboot commands on Android- Table 2 – Available task management commands
Command | Description |
---|---|
start [options] intent | Start a Activity specified by intent . The options are:
|
startservice [options] intent | Start the Service specified by intent . The options are:
|
force-stop package | Force the interruption of everything that is associated package (name of the package of the app). |
kill [options] package | Delete all associated processes package (app package name). This command only kills processes that can be safely killed and that do not affect the user experience. The options are:
|
kill-all | Kill all background processes. |
broadcast [options] intent | Issue a transmission intent.The options are:
|
instrument [options] component | Start monitoring with Instrumentation an instance. The goal component is usually the form. The options are:test_package/runner_class
|
profile start process file | Start the profiler process , write the results to file . |
profile stop process | Stop the profiler process . |
dumpheap [options] process file | Download the bunch of process , write to file .The options are:
|
set-debug-app [options] package | Set the app package to debug. The options are:
|
clear-debug-app | Delete the previous package set for debugging with set-debug-app . |
monitor [options] | Monitoring starts due to crashes or ANR. The options are:
|
screen-compat {on | | Check the screen compatibility mode of package . |
display-size [reset | | Ignore the display size of the device. This command is useful for testing your app on different screen sizes by imitating a small screen resolution using a device with a large screen and vice versa. Example:am display-size 1280x800 |
display-density dpi | Ignore the display density of the device. This command is useful for testing the app on different screen densities on a high density screen environment using a low density screen and vice versa. Example:am display-density 480 |
to-uri intent | Print the specified intent specification as a URI. |
to-intent-uri intent | Print the specified intent specification as a intent: URI. |
Specify for intent arguments
For task manager commands that accept an intent
argument, you can specify the intent with the following options:
Call package manager ( pm
)
Within an adb shell, you can issue commands with the package manager tool ( pm
) to perform actions and queries on the app packages installed on the device. In a shell, the syntax is:
pm command
You can also send a package manager command directly from adb without entering a remote shell. For example:
adb shell pm uninstall com.example.MyApp
How to use ADB and fastboot commands on Android- Table 3 – Package management commands available
filter | Print all packages, optionally only those whose package name contains the text. filter Options:
|
list permission-groups | Print all known permission groups. |
list permissions [options] group | Print all known authorizations, optionally only those in. group Options:
|
list instrumentation [options] | List all test packages. Options:
|
list features | Print all system features. |
list libraries | Print all libraries supported by the current device. |
list users | Print all users on the system. |
path package | Print the path for the data APK package . |
install [options] path | Install a package (specified by path ) in the system. Options:
|
uninstall [options] package | Removes a package from the system. Options:
|
clear package | Delete all data associated with a package. |
enable package_or_component | Enable the package or component indicated (written as “package / class”). |
disable package_or_component | Disable the package or component indicated (written as “package / class”). |
disable-user [options] package_or_component | Options:
|
grant package_name permission | Grant an authorization to an app. On devices with Android 6.0 (API level 23) and later versions, the authorization can be any authorization declared in the app. On devices with Android 5.1 (API level 22) and previous versions, it must be an optional authorization defined by the ‘app. |
revoke package_name permission | Revoke an authorization from an app. On devices with Android 6.0 (API level 23) and later versions, the authorization can be any authorization declared in the app. On devices with Android 5.1 (API level 22) and previous versions, it must be an optional authorization defined by the ‘app. |
set-install-location location | Change the default installation path. Position values:
Note- it is intended only for debugging; using this can cause app crashes and other undesirable behavior. |
get-install-location | Return the current installation path. Return values:
|
set-permission-enforced permission [true | | Specifies whether the given authorization must be applied. |
trim-caches desired_free_space | Cut the cache files to get the available space. |
create-user user_name | Create a new user with the data user_name , printing the user’s new user ID. |
remove-user user_id | Remove the user with the data user_id , deleting all the data associated with that user |
get-max-users | Print the maximum number of users supported by the device. |
Call device policy manager ( dpm
)
To help you develop and test your device management apps (or other companies), you can issue commands to the Device Policy Manager tool ( dpm
). Use the tool to check the active administration app or change the status data of a policy on the device. In a shell, the syntax is:
dpm command
You can also send a device policy management command directly from adb without entering a remote shell:
adb shell dpm command
How to use the ADB and fastboot commands on Android- Table 4 – Management commands for available device policies
Command | Description |
---|---|
set-active-admin [options] component | Set the component as an active administrator. The options are:
|
set-profile-owner [options] component | Set the component as an active administrator and its package as a profile owner for an existing user. The options are:
|
set-device-owner [options] component | Set the component as an active administrator and its package as the device owner. The options are:
|
remove-active-admin [options] component | Disable an active administrator. The app must declare android:testOnly in the manifest. This command also removes device owners and profiles. The options are:
|
Make a screenshot. The command screencap
is a shell utility for capturing a screenshot of a device’s display. In a shell, the syntax is:
screencap filename
To use the command screencap
from the command line, type the following:
adb shell screencap / sdcard / screen . pNG
Here is a sample screenshot session, which uses the adb shell to capture the screenshot and the pull
command to download the file from the device:
$ adb shell shell @ $ screencap / sdcard / screen . pNG shell @ $ exit $ adb pull / sdcard / screen . pNG
To record a video. The command screenrecord
is a shell utility to record the display of devices with Android 4.4 (API level 19) and later versions. The utility records screen activity on an MPEG-4 file. You can use this file to create promotional or training videos or for debugging and testing.
In a shell, use the following syntax:
screenrecord [ options ] filename
To use screenrecord
from the command line, type the following:
$ adb shell shell @ $ screenrecord - verbose / sdcard / demo . mp4 ( press Control + C to stop ) shell @ $ exit $ adb pull / sdcard / demo . mp4
The utility screenrecord
can record in any resolution and supported bit rate, while maintaining the display aspect ratio of the device. The utility records the native resolution and orientation of the screen by default, with a maximum length of three minutes.
Limitations of screenrecord
:
- Audio is not recorded with the video file.
- Video recording is not available for devices with the Wear operating system.
- Some devices may not be able to record with native display resolution. If you experience problems with screen recording, try using a lower screen resolution.
- Screen rotation during recording is not supported. If the screen rotates during recording, part of the screen is cut off during recording.
How to use ADB and fastboot commands on Android- Table 5 – Screenrecord options
Options | Description |
---|---|
--help | View the command syntax and options |
--size widthxheight | Set the video size: 1280x720 . The default is the device’s native display resolution (if supported), 1280 × 720 otherwise. For best results, use a size supported by your device’s Advanced Video Coding (AVC) encoder. |
--bit-rate rate | Set the video bit rate for video, in megabits per second. The default is 4Mbps. You can increase the bit rate to improve the quality of the video, but this way you get larger movie files. The following example sets the recording bit rate to 6Mbps:screenrecord –bit-rate 6000000 /sdcard/demo.mp4 |
--time-limit time | Set the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes). |
--rotate | Rotate the output 90 degrees. This functionality is experimental. |
--verbose | Displays the log information on the command line screen. If this option is not set, the utility does not display any information during execution. |
ART profiles for apps. Starting from Android 7.0 (API 24 level), Android Runtime (ART) collects the execution profiles for the installed apps, used to optimize the performance of the app. You can examine the collected profiles to understand which methods are determined to be run frequently and which classes are used during app startup.
To produce a text form of the profile information, use the command:
adb pull / data / misc / profman / package .txt
Other shell commands
For a list of all available shell programs, use the following command:
adb shell ls / system / bin
The guide is available for most commands.
The following table lists some of the more common adb shell commands.
How to use ADB and fastboot commands on Android- Table 6 – Other adb shell commands
Shell command | Description |
---|---|
dumpsys | Download system data to the screen. |
dumpstate | Dump the status to a file. |
logcat [option]. | Enable system and app registration and print output on the screen. |
dmesg | Print kernel debug messages on the screen. |
start | Start (restart) a device. |
stop | Stop running a device. |
sqlite3 | Start the sqlite3 command line program. The sqlite3 tool includes commands like .dump print the contents of a table and .schema prints the SQL CREATE statement for an existing table. You can also execute SQLite commands on the fly.SQLite3 databases are stored in the folder./data/data/package_name/databases/ For example: $ adb -s emulator-5554 shell $ sqlite3 /data/data/com.example.app/databases/rssitems.db SQLite version 3.3.12 Enter ".help" for instructions |
We have therefore seen how to use the ADB and Fastboot commands on Android , all the commands have been recovered from the official source developer.android.com and for some comprehension problems caused by the translation of the text found in the guide or to read all the descriptions of the commands in original language, I refer you to the reading of the SOURCE.