54

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?

Deepak Azad
  • 7,903
  • 2
  • 34
  • 49
  • Why don' you try running your app and see if it removes the existing app, then installs a new one, or else? – Biu Jan 21 '15 at 21:04
  • It does not remove the existing app, I think. Since the user data for the app still exists after the second install. – Deepak Azad Jan 21 '15 at 21:07
  • @Biu: the default behavior is to simply install the apk, which results in an update. – njzk2 Jan 21 '15 at 21:34
  • apparently the command line `gradle uninstallDebug` should uninstall the app – njzk2 Jan 21 '15 at 21:36

3 Answers3

130

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).

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • 2
    How can i do that embedded in the apk? meaning that before installing a new apk it will uninstall old one? – dowi Nov 23 '15 at 09:01
  • @dowi Why do You want to do so in the APK ? – code2be Feb 11 '16 at 16:17
  • @code2be because i use retrofit which does not have a migration process. the db itself is synced with a server so i had no problem to lose it, install the new apk with new db scheme and download the data. since then i found that i can version the scheme and when it sees a new scheme it empties the db automatically and then i sync from server – dowi Feb 11 '16 at 20:30
  • 1
    why would retrofit have anything to do with a migration process? – njzk2 Feb 11 '16 at 21:06
  • @njzk2 do you know if there is a way to call the task only when a certain build variant is selected? I'd like to uninstall my `release` build but only when I'm about to run it – Jukurrpa Apr 28 '17 at 01:18
  • Fails if the app doesn't installed – ieselisra Apr 16 '18 at 17:27
  • `:app:uninstallAll` gives error Project 'app' not found in root project 'xxxx'. – Amarjit Dhillon Apr 29 '18 at 00:15
  • @dowi if you found solution, please help [here]https://stackoverflow.com/questions/52704991/automatically-uninstall-older-app-before-installing-new-android-app – Geek Guy Oct 08 '18 at 15:01
  • meant GreenDAO, not Retrofit :-\ – dowi Oct 09 '18 at 08:04
  • I wonder if there is a way to uninstall the current build variant. The above solution needs the name ("uninstallDEBUG") – Maciej Beimcik Mar 12 '19 at 08:28
11

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
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

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)

Harvinder Singh
  • 1,919
  • 21
  • 15