Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio : How to uninstall APK (or execute adb command) automatically before Run or Debug?

People also ask

How do I stop adb from running?

To stop the adb server, use the adb kill-server command. You can then restart the server by issuing any other adb command.

How do I uninstall an APK app on Android?

Originally Answered: How do you uninstall APK on Android? Long press on the app icon and drag it to top of the screen, then click on the uninstall press ok.


adb uninstall <package_name>

can be used to uninstall an app via your PC. If you want this to happen automatically every time you launch your app via Android Studio, you can do this:

  1. In Android Studio, click the drop down list to the left of Run button, and select Edit configurations...
  2. Click on app under Android Application, and in General Tab, find the heading 'Before Launch'
  3. Click the + button, select Run external tool, click the + button in the popup window.
  4. Give some name (Eg adb uninstall) and description, and type adb in Program: and uninstall <your-package-name> in Parameters:. Make sure that the new item is selected when you click Ok in the popup window.

Note: If you do not have adb in your PATH environment variable, give the full path to adb in Program: field (eg /home/user/android/sdk/platform-tools/adb).


example

adb uninstall com.my.firstapp

List the packages by:

adb shell su 0 pm list packages

Review which package you want to uninstall and copy the package name from there. For example:

com.android.calculator2

Lastly type in:

adb uninstall com.android.calculator2 

and you are done.


If you want to uninstall when connected to single device/emulator then use below command

adb uninstall <package name>

else with multiple devices then use below command

adb -s <device ID> uninstall <package name>

This command with --user 0 do the job:

adb uninstall --user 0 <package_name>

I am using Android Studio 2.1.2. I had same requirement as OP. Though above two answer seemed to help everyone, it did not work for me . I am sharing what worked for me.

Go to main menu/Run/Edit Configuration . Select app under Android Application on the left.This should open multi-tabbed pane . Select General tab ( would be default), click green + sing at the bottom ( below text Before launch: Gradle -awake ...).

A drop down will appear, select Gradle-aware-make option. Another text box will pop up. enter :app:uninstallAll in this text box . (You can use ctrl + space to use autocomplete todetermine right target without typing everything . And also helps you choose the right app name that is avaiable for you). and set apply/ok. Relaunch your app.

Note : Every time you launch your app now , this new target will try to uninstall your app from your emulator or device. So if your testing device is not available, your launc will probably fail while uninstalling but will continue to start your emulator. So Either start your emulator first, or re-lauch after first fail again ( as first launch will start emulator though uninstall fails).