Following shell script does, check if android application is installed find an apk path if android application is already installed. uninstall if its already installed install new apk if application is not installed #!/bin/bash ADBPATH=/usr/bin/adb PACKAGE_NAME=my.app.helloworld LOCAL_DESKTOP_APK_PATH=$PWD/PACKAGE_NAME.apk IS_PACKAGE_EXISTS=$ADBPATH shell pm list packages $PACKAGE_NAME echo "pm list package returned: $IS_PACKAGE_EXISTS " if test -n "$IS_PACKAGE_EXISTS" then echo "Application $PACKAGE_NAME exists" CMD=$ADBPATH shell pm path $PACKAGE_NAME APK_PATH=${CMD:8} APK_PATH=$(echo $APK_PATH|tr -d '\n'|tr -d '\r') echo "Application is installed at path $APK_PATH" echo "uncomment below line if you want to uninstall app" echo "$ADBPATH uninstall $PACKAGE_NAME" #$ADBPATH uninstall $PACKAGE_NAME else echo "Application $PACKAGE_NAME not found" echo "we can install a new apk now" $ADBPATH install $LOCAL_DESKTOP_APK_PATH/$PACKAGE_NAME.apk fi

Command line execution

Terminalbash
find an apk path if android application is already installed. uninstall if its already installed install new apk if application is not installed #!/bin/bash ADBPATH=/usr/bin/adb PACKAGE_NAME=my.app.helloworld LOCAL_DESKTOP_APK_PATH=$PWD/PACKAGE_NAME.apk IS_PACKAGE_EXISTS=`$ADBPATH shell pm list packages $PACKAGE_NAME` echo "pm list package returned: $IS_PACKAGE_EXISTS " if test -n "$IS_PACKAGE_EXISTS" then         echo "Application $PACKAGE_NAME exists"         CMD=`$ADBPATH shell pm path $PACKAGE_NAME`         APK_PATH=${CMD:8}         APK_PATH=$(echo $APK_PATH|tr -d '\n'|tr -d '\r')         echo "Application is installed at path $APK_PATH"         echo "uncomment below line if you want to uninstall app"         echo "$ADBPATH uninstall $PACKAGE_NAME"         #$ADBPATH uninstall $PACKAGE_NAME else         echo "Application $PACKAGE_NAME not found"         echo "we can install a new apk now"         $ADBPATH install $LOCAL_DESKTOP_APK_PATH/$PACKAGE_NAME.apk fi

Gotchas and common issues

  • Permission checks - verify user access rights and sudo privileges before executing system-level operations.

  • Environment configuration - double-check path variables and dependency versions to prevent runtime failures.

  • Backup safeguards - maintain configuration backups before applying system or database modifications.

Following these steps ensures clean configuration and reliable execution for install and uninstall android application ( apk ) using adb.