Here is the second script in my series of articles that centers around bash scripts for Android development in Android Studio.
Eventually I will combine all of these bash scripts into one main file and have actions launched based on the shell arguments passed to the shell script.
The script below uses the now familiar loop that will iterate through each device that is connected through adb and uninstall the app based on the package name. Set the PACKAGE_NAME variable to the app you would like to uninstall. The script will check to see if the package is currently installed by grepping the PACKAGE_NAME variable. This line of code does the check:
if $ADBPATH/adb -s $SERIAL shell pm list packages | grep $PACKAGE_NAME > /dev/null; then
It is always best to check and see if the package name exists before deleting it, as adb will complain if the package does not exist.
This script and the others in my series of articles can be downloaded from GitHub:
#!/usr/bin/env bash
echo "\n\nUninstall foo...\n\n"
# get full path to adb just in case script gets confused
ADBPATH="${ANDROID_HOME}/platform-tools"
GREP_PACKAGE=""
MYPACKAGE="com.globieapp.globie"
for SERIAL in $(adb devices | tail -n +2 | cut -sf 1);
do
# only try uninstalling if the package exists
GREP_PACKAGE=$($ADBPATH/adb -s $SERIAL shell pm list packages | grep $MYPACKAGE 2> /dev/null)
echo "\n\n*********************************** "
echo "Can we find the package? :: grepped result ---> $GREP_PACKAGE"
echo "*********************************** "
if [[ -n "${GREP_PACKAGE/[ ]*\n/}" ]]
then
echo "\n\n*********************************** "
echo "Found Package To Uninstall On -> $SERIAL";
echo "*********************************** "
echo "\n\n"
echo "Will attempt uninstall on $SERIAL\n\n";
$ADBPATH/adb -s $SERIAL uninstall $MYPACKAGE
else
echo "*********************************** "
echo "Package Not Found On -> $SERIAL"
echo "*********************************** "
fi
done
echo "\n\n***** Uninstall has left the building *****"