ADB Shell Command (with examples)
1: Starting a remote interactive shell on the emulator or device
adb shell
Motivation:
The adb shell
command allows developers to start a remote interactive shell on an Android emulator or connected device. This is useful for performing various tasks and executing commands directly on the emulator or device.
Explanation:
adb shell
is the basic command to start a remote interactive shell. It opens a terminal-like environment where you can run shell commands directly on the connected Android device or emulator. Once the shell is started, you can execute any shell command or interact with the device as if you were using a terminal on the device itself.
Example Output:
$ adb shell
<device prompt>
2: Getting all the properties from the emulator or device
adb shell getprop
Motivation:
To gather information about the connected Android device or emulator, using adb shell getprop
provides access to various properties and system-related details. This can be useful for debugging, troubleshooting, or extracting specific information about the device.
Explanation:
The getprop
command retrieves system properties from the Android emulator or connected device. These properties include information about the device model, manufacturer, operating system version, and other system-level details. Running adb shell getprop
allows you to fetch all available properties in the output.
Example Output:
$ adb shell getprop
[output containing various properties]
3: Reverting all runtime permissions to their default
adb shell pm reset-permissions
Motivation:
During the development process, applications may request various runtime permissions from the user. Resetting these permissions to their default state may be necessary for testing purposes or to observe how the application behaves in a clean environment. The pm reset-permissions
command simplifies this process.
Explanation:
adb shell pm reset-permissions
is a command used to reset all runtime permissions to their default state on the emulator or connected device. This command clears all granted, denied, and user-defined permissions for all installed applications. After executing this command, all applications will start with fresh permission settings.
Example Output:
$ adb shell pm reset-permissions
Success
4: Revoking a dangerous permission for an application
adb shell pm revoke package permission
Motivation:
When testing applications, there may be instances where it is necessary to revoke specific permissions from an application. This allows developers to observe how the application behaves without certain permissions or to test edge cases. The pm revoke
command helps achieve this by revoking a specific permission specified by its package name.
Explanation:
The pm revoke
command is used to revoke a specific permission for an application. The command requires two arguments: package
and permission
. package
refers to the package name of the application, and permission
refers to the specific permission to be revoked. After executing this command, the specified permission will be revoked for the given application.
Example Output:
$ adb shell pm revoke com.example.app android.permission.CAMERA
Success
5: Triggering a key event
adb shell input keyevent keycode
Motivation:
Simulating key events can be helpful for testing user interactions or automating user actions on an emulator or connected device. The input keyevent
command allows developers to trigger key events as if they were being performed physically on the device.
Explanation:
The input keyevent
command is used to simulate key events on the Android device or emulator. The keycode
argument specifies the key event to be triggered. For example, KEYCODE_HOME
can be used to simulate the home button press. Different keycodes are available for various actions like volume control, power button, and navigation controls.
Example Output:
$ adb shell input keyevent 3
6: Clearing the data of an application on an emulator or device
adb shell pm clear package
Motivation:
During application development, it may be necessary to clear the data of an installed application to start with a clean slate. The pm clear
command simplifies this process by allowing developers to delete all data associated with a specific package.
Explanation:
The pm clear
command is used to clear the data of an installed application. The package
argument specifies the package name of the application whose data needs to be cleared. After executing this command, all data associated with the specified application will be deleted, including the app’s settings, files, and databases.
Example Output:
$ adb shell pm clear com.example.app
Success
7: Starting an activity on an emulator or device
adb shell am start -n package/activity
Motivation:
Starting an activity on an emulator or connected device is often required during development to launch specific parts of an application or test specific activities. The am start
command allows developers to start activities directly from the command line.
Explanation:
The am start
command is used to start an activity on the emulator or connected device. The -n
option is used to specify the fully qualified name of the component (package/activity) to start. The package
argument refers to the package name of the application, and the activity
argument refers to the specific activity to be launched.
Example Output:
$ adb shell am start -n com.example.app/.MainActivity
8: Starting the home activity on an emulator or device
adb shell am start -W -c android.intent.category.HOME -a android.intent.action.MAIN
Motivation:
Starting the home activity on an emulator or connected device can be useful when automating tasks or navigating to the home screen programmatically. The am start
command, combined with specific options and parameters, allows developers to start the home activity directly.
Explanation:
To start the home activity on an emulator or device, the am start
command is used with specific options and parameters. The -W
option waits for the launch to complete, providing information about the target activity. The -c
option and -a
option specify the category and action to be used to launch the home activity.
Example Output:
$ adb shell am start -W -c android.intent.category.HOME -a android.intent.action.MAIN