Skip to content

Instantly share code, notes, and snippets.

@gmale
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmale/d75b0fc8c74b81d9c1a5 to your computer and use it in GitHub Desktop.
Save gmale/d75b0fc8c74b81d9c1a5 to your computer and use it in GitHub Desktop.
Android running process utilities.

To use:

git clone https://gist.github.com/d75b0fc8c74b81d9c1a5.git
cd d75b0fc8c74b81d9c1a5

./androidClear <search_text>

# type a number at the prompt to clear an app's data
# warning: this is the same as running "clear data" from the app manager
#          and could have bad effects if ran on the wrong app

Note: replace the search_text with a search term that will find the app you want to clear

For this to work you need two things:

  1. the current directory . should be on your path
  • When this is missing you will see the following error message:
  • ./androidClear:19: command not found: androidProcesses
  1. the adb command should be on your path
  • When this is missing, you will see the following error message:
  • androidProcesses:10: command not found: adb
  • if you use Android Studio on a mac and don't have adb on your path, simply add these directories
    • /Applications/Android\ Studio.app/sdk/platform-tools
    • /Applications/Android\ Studio.app/sdk/tools
#!/bin/zsh
# usages:
# androidClear <search_term> - finds all running processes on the device matching the given search term
# then allows the user to clear app data simply by typing in the number
# listed next to the package name
#
# NOTE: this file requires the 'androidPrcoesses' file to be available on the path
#
if [ -z "$1" ]; then
echo "Please specify the package name to search for"
exit 1
fi
package_names=$(androidProcesses $1 | nl)
while [ -z "$matching_line" ]; do
printf "\n$package_names\n"
read "selection?Which app's data would you like to clear? "
if [ -n "$selection" ]; then
# the sed command first strips of the leading number and white space, then strips off the trailing carriage return that is there for some reason
matching_line=$(echo $package_names | grep "$selection.*$1" | sed -e 's/[^0-9]*[0-9][^a-z]*//' -e 's/[^a-z.]//g')
fi
if [ -z "$matching_line" ]; then
printf "\n*** OOPS! Please enter a number from the list...\n"
fi
done
printf "\n\tAre you sure you want to clear <$matching_line>?\n\n\t**** Press ENTER to continue. Press CTRL+C to cancel ****"
read
echo "clearing: <$matching_line>"
adb shell pm clear $matching_line
#!/bin/zsh
# usages:
# androidProcesses - list all running processes on the attached device
# androidProcesses <search_term> - list all processes on the device matching the given search term
if [ -z "$1" ]; then
adb shell ps | tr -s ' ' | cut -d" " -f9 | sort -u
else
adb shell ps | tr -s ' ' | cut -d" " -f9 | grep "$1" | sort -u
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment