How do you re-install an app without losing personal data?
In this case its Plants vs. Zombies 2 that hangs when loading the game. I thought a re-install might solve the issue. But I don't want to need to redo everything I've done so far.
For the following procedure you'll need adb installed on your computer (if you're not already have that, see: Is there a minimal installation of ADB?). Alternatively, a terminal emulator app should do as well.
Android apps are managed by the "Package manager", which has a command-line interface called pm
. So here's what you can do with it for your case:
# to use ADB, first get a command line adb shell # now tell the package manager to uninstall the app, but keep its data pm uninstall -k com.YOUR_PACKAGE_NAME`
(Note this won't work with a terminal emulator app on device (at least with none I know of) except with root permissions, as "normal apps" lack the required privileges/permissions.)
Of course you have to replace com.YOUR_PACKAGE_NAME
with the package name of the app you want to deal with. Easiest way to find that is visiting its page on Google Play, and take a look at the URL: the package name follows the id=
parameter there. It's the -k
parameter telling the package manager to keep the app's data.
Now, when you re-install the app, its data is already there (was not removed).
All this does not require root (at least as long as we talk about a user-installed app).
adb shell
from a computer or else you must root your phone and use the su
command. If your phone is not rooted, a terminal emulator won't allow you to uninstall anything.
Commented
Jul 13, 2015 at 12:43
adb shell
indeed lacks root privileges. But it does get enough permissions to run pm uninstall -k
. Still, my terminal emulator (jackpal.androidterm) does not have sufficient permissions to run pm uninstall -k
: when I try it, pm
gives me a "Permission denied" error. I did my testing in Android "Jelly Bean" 4.1.2. I wonder if, on a newer OS version, App Ops could grant more permissions to my terminal emulator?
Commented
Jul 16, 2015 at 13:00
adbd
which gets "elevated privileges" (which it e.g. requires for adb backup
). Terminal is dealt with like a "normal app" – so unless it explicitly requests additional permissions, it won't get them.
Android hasn't got function which you described. If you have root access on your phone, you can make a copy of folder /data/data/com.xxx.xxx
, reinstall app, and overwrite generated folder by your copy.
You can also just reinstall the APK file that already exists on the device. After an upgrade to Android 6 somehow default permissions like android.permission.ACCESS_NETWORK_STATE
were lost, that is where this trick comes handy to discover the APK and reinstall it:
adb shell
shell@hammerhead:/ $ pm path com.stackexchange.marvin
/data/app/com.stackexchange.marvin-2/base.apk
shell@hammerhead:/ $ pm install -r /data/app/com.stackexchange.marvin-2/base.apk
pkg: /data/app/com.stackexchange.marvin-2/base.apk
Success
Alternatively if you would like to reinstall a bunch of packages (-3
for third-party packages only), you can parse the obtain the list of installed packages on the device and reinstall all of them with:
pm list packages -f -3 | cut -d: -f2 | cut -d= -f1 | while read apk; do pm install -r $apk; done
cmd package list packages -f -3
and that in that cut -d= -f1
did not work for every package, as some paths include base64 padding (=
characters), but was able to use regex in a text editor to process the list, and then use cat
with the last part of your one-liner.
Commented
Dec 3, 2018 at 5:01