How to automatically uninstall android app from device before installing a new version

AndroidGradleAndroid StudioAndroid Gradle-Plugin

Android Problem Overview


I am using Android studio with Gradle builds. I test my app on a device and let Android Studio and Gradle build the app and install it on the device. How can I tell Gradle or Android Studio to uninstall the app from the device before installing a new version?

Android Solutions


Solution 1 - Android

Apparently, if you in Run -> Edit Configurations..., on the left side there is a expendable list of Android configurations.

Select yours, on the right side of the window are details on the configuration, at the bottom of that section is the Before launch section.

Create a gradle-aware Make, given the task :app:uninstallAll or :app:uninstallDebug whichever suits you. (There is autocompletion to get all the available tasks, app may vary if you have several modules).

Solution 2 - Android

The current answer is a little outdated, these are the simplest steps:

  • Select Edit Configurations from the dropwdown
  • Find the section Before Launch: Gradle task, Gradle-aware Make
  • Click + and choose Run Gradle task
  • In the Gradle project field, select the root of your project
  • In the Tasks field enter :app:uninstallAll or equivalent
  • Leave the other fields blank and press OK
  • Reorder the tasks so your new one is first / top of the list

Solution 3 - Android

React Native Developers

just replace the android script in your package.json

replace this script

    "android": "react-native run-android",

with this script

    "android": "cd android && ./gradlew :app:uninstallAll && react-native run-android",

the new script will uninstall the previous apk from the connected device before installing the new one just hit yarn android or npm android(your preference)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepak AzadView Question on Stackoverflow
Solution 1 - Androidnjzk2View Answer on Stackoverflow
Solution 2 - AndroidNick CardosoView Answer on Stackoverflow
Solution 3 - AndroidHarvinder SinghView Answer on Stackoverflow