This is a list of adb commands which I often use when doing manual or automated testing of Android apps.
Those unfamiliar with adb can read about it and find installation instructions here. In short, adb is a command-line tools which allows communication with Android device via USB or Wi-Fi and execution of variety of the commands on the device. It is really useful when testing apps on Android, whether manually or via automation.
If used on Windows, it is convenient to install GNU utils for Windows in order to be able to use tools like grep, sed or awk. I recommend UnxUtils (oldie but goodie), but Cygwin or anything else will work as well.
File operations
- Copy file to device:
adb push <src> <dest>
- Copy file from device:
adb pull <src> [<dest>]
Logging
- Clear log:
adb logcat -c
- Start logging to file:
adb logcat -v time > log.txt
App operations
- Install an app:
adb install [-r -g] <local/path/to/apk>
(-r is for reinstalling an existing app while keeping all its data; -g is for granting all permissions listed in the app manifest)adb shell pm install <remote/path/to/apk>
(has more options than adb install)
- Clear package data (effect is similar to re-installing the app):
adb shell pm clear <package_name>
- Check if a package is installed:
adb shell pm list packages | grep <package_name>
- Launch an app as different user/package (this might be helpful if a certain system app doesn’t have a necessary privilege due to a system quirk/bug, like cp on some LG phones):
adb shell run-as <package_name>
- Force stop an app:
adb shell am force-stop <package_name>
Miscellanious
- Get Android version:
adb shell getprop ro.build.version.release
- Get device model:
adb shell getprop ro.product.model
- Get device manufacturer:
adb shell getprop ro.product.manufacturer
- Get list of all available properties:
adb shell getprop
- Reboot phone:
adb reboot
- Get name of the current activity:
adb shell dumpsys activity top | grep ACTIVITY
- Open URL in browser:
- In default browser:
adb shell am start -a android.intent.action.VIEW -d "<url>"
- In Chrome:
adb shell am start -a android.intent.action.VIEW -d "<url-with-http(s)>" -n com.android.chrome/com.google.android.apps.chrome.IntentDispatcher
- In Samsung browser:
adb shell am start -a android.intent.action.VIEW -d "<url>" -n com.sec.android.app.sbrowser/.SBrowserMainActivity
- For re-using the same tab add option
--es "com.android.browser.application_id" "arbitrary_app_id"
, but note, that the tab will be re-used only after it is opened via this command at least once, i.e. it will not re-use any other existing tab.
- In default browser:
- Open app in Google Play Store:
adb am start -a android.intent.action.VIEW -d "market://details?id=<package_name>"
Pressing buttons
- Home button:
adb shell input keyevent KEYCODE_HOME
- Back button:
adb shell input keyevent KEYCODE_BACK
- Enter/Return button:
adb shell input keyevent KEYCODE_ENTER
- Open notification bar:
adb shell input swipe 10 10 10 1000
- Get list of all displayed notifications (more about this here):
adb dumpsys notification --noredact | sed -n /extras/,/stats/p
Permissions
- Enable/disable notification access for an app:
- Android P and above:
adb shell cmd notification allow_listener/disallow_listener <package_name/notification_listener_class_name>
- Android below P (this will disable all other currently enabled listeners; in order to preserve them obtain the list of currently enabled listeners first using
settings get secure enabled_notification_listeners
and then list them up in the “set” command together with the new listener):adb shell settings put secure enabled_notification_listeners <package_name/notification_listener_class_name>
- Android P and above:
- Enable/disable usage access for an app (only Lollipop and above):
adb shell appops set <package_name> android:get_usage_stats allow/deny
(more about appops command and setting permissions with it can be found in my other post here) - Enable/disable accessibility (only for Android N and above):
adb shell settings put secure enabled_accessibility_services <package_name/class_name>
(for disabling put “0” instead of package/class, but keep in mind that it will disable all accessibility services). Android 11 has a quirk when enabling accessibility for an app after clearing its data with pm clear gives an error, and a workaround is to go through enable-disable-enable cycle. - Grant runtime permissions:
adb shell pm grant permission <permission>
(hint: runtime permissions requested by the app can be obtained using PackageManager class:InstrumentationRegistry.getContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS).requestedPermissions)
Modifying the settings
- Enable/disable airplane mode:
- For Android below O_MR1:
adb shell settings put global airplane_mode_on 0/1 && adb shell am broadcast -a android.intent.action.AIRPLANE_MODE
(0 – for disabling, 1 – for enabling) - For Android above O_MR1:
adb shell svc wifi enable/disable
- For Android below O_MR1:
- Launch settings activity (see the list of activities below):
adb shell am start -a <activity>
- android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS
- android.settings.DATE_SETTINGS
- android.settings.ACCESSIBILITY_SETTINGS
- android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS
- android.settings.USAGE_ACCESS_SETTINGS
- com.android.settings.APPLICATION_DEVELOPMENT_SETTINGS
- android.settings.LOCATION_SOURCE_SETTINGS
- android.settings.MEMORY_CARD_SETTINGS
- android.settings.LOCALE_SETTINGS
- android.search.action.SEARCH_SETTINGS
- android.net.vpn.SETTINGS
- android.settings.ACCOUNT_SYNC_SETTINGS
- com.android.settings.DISPLAY_SETTINGS
- android.settings.INPUT_METHOD_SETTINGS
- android.settings.SOUND_SETTINGS
- android.settings.WIFI_SETTINGS
- android.settings.APPLICATION_SETTINGS
- com.android.settings.SOUND_SETTINGS
- android.settings.ACCOUNT_SYNC_SETTINGS_ADD_ACCOUNT
- android.settings.MANAGE_APPLICATIONS_SETTINGS
- android.settings.SYNC_SETTINGS
- android.settings.SETTINGS
- com.android.settings.DOCK_SETTINGS
- android.settings.ADD_ACCOUNT_SETTINGS
- android.settings.SECURITY_SETTINGS
- android.settings.DEVICE_INFO_SETTINGS
- android.settings.WIRELESS_SETTINGS
- android.settings.DISPLAY_SETTINGS
- android.settings.SYSTEM_UPDATE_SETTINGS
- android.settings.MANAGE_ALL_APPLICATIONS_SETTINGS
- android.settings.DATA_ROAMING_SETTINGS
- android.settings.APN_SETTINGS
- android.settings.USER_DICTIONARY_SETTINGS
- com.android.settings.VOICE_INPUT_OUTPUT_SETTINGS
- com.android.settings.TTS_SETTINGS
- android.settings.WIFI_IP_SETTINGS
- android.search.action.WEB_SEARCH_SETTINGS
- android.settings.BLUETOOTH_SETTINGS
- android.settings.AIRPLANE_MODE_SETTINGS
- android.settings.INTERNAL_STORAGE_SETTINGS
- com.android.settings.QUICK_LAUNCH_SETTINGS
- android.settings.PRIVACY_SETTINGS
Working with data saver
- Enable/disable data saver:
adb shell cmd netpolicy set restrict-background true/false
. Note: on Android P and above disabling datasaver will change USB connection mode to (Charging only), which will cause adb to shortly disconnect and reconnect. If you are using STF for managing Android devices, it will cause device IP address (port number to change). The solution is to set “No data transfer” to default USB mode in device’s Developers options. - Check if app is whitelisted in data saver settings:
adb shell cmd netpolicy list restrict-background-whitelist | grep <uid>
(uid can be obtained with commandadb shell dumpsys package --checkin <package> | awk 'BEGIN {FS=","} $1=="pkg" {print $3}'
) - White-/blacklist an app in data saver settings:
adb shell cmd netpolicy add/remove restrict-background-whitelist <uid>
Let me know about other useful adb commands for testing in the comments below and I will add them to the list.
Thank you!!1