Rootless
Sorry but AFAIK it's not possible :( I've never seen any app/tool/utility successfully modifying other apps' data, not even reading!
Alternatively you could do that in a custom recovery, but who uses a custom recovery without rooting? So read below.
Simple Root (Manual way)
Don't take it too hard. Just move its original data away, uninstall the original app and install the new one, then move the data back and chown
. Sample shell commands are below.
# mv /data/data/com.example.foo /data/media
* UNINSTALL_ORIGINAL_APP
* INSTALL_NEW_APP
# ls -ld /data/data/com.example.foo
* take note of the user (e.g. u0_a123)
# mv /data/media/com.example.foo /data/data
# chown -R 10123.10123 /data/data/com.example.foo
Explanations for each step:
- Move original app data to an alternative place. Because Androis automatically delete irrelevant files in certain directories, we need a safe place as the Alt. AFAIK unknown files are ignored in
/data/media
so that is chosen.
- Replace app. No explanation.
- Obtain the UID of newly installed app. Wrong UID will make Package Installer delete app data.
- Move data back and change UID.
A small note: The builtin chown
command does not accept option -R
(recursive) by default in Android L or lower. You need BusyBox in that case. In Android M and up it just works fine.
Root Utilities
Titanium Backup
With TiBu you can easily back up existing data, then safely remove the old app and install the new app. Then as long as both packages have the same package name, you can restore the data for the app with TiBu, intact!
However, this solution is essentially a data-backup, so it will not be maintained here. If you want to learn more about backing up & restoring data, check out our tag wiki for backup, under the App-based backups section.
Xposed Modules (Easiest, Recommended by iBug)
XInstaller and its successor, InstallerOpt, can hook Package Installer to allow packages with mismatching signatures to be installed (by skipping sig check). Then all your data is preserved and you can easily switch to different versions. This is the method I am currently using and it has been working fine. Sad that Xposed isn't officially available for Android N (so far), so this solution is only for Android M and down.
Theoretically, they should not cause anything wrong because all they do is hooking the signature checking function and change its return value to a truthy value, bypassing the check. If the replacement package have any other defects than mismatching signature, the installation will still fail, so no faulty packages can be installed even if you skip signature check.
Important Notice
It is not recommended to leave that option (skip sig check) enabled all the time. Leaving it enabled will create a big security flaw as hacked apps (almost always use a different signature) can also be installed. I personally enable it only when needed and disable it as soon as I finished my package replacement.