Posts | Tags | Archive

Reinstalling an Android app without losing data

When an application isn't opening (and clearing the cache doesn't help) it sometimes helps to reinstall it. However, uninstalling then reinstalling the app normally will delete all the data associated with it.

The way around this is to directly call the package manager from the shell and give it the -k argument, which tells it to keep the data and cache directories.

Simply connect the device to a computer with ADB installed and run:

adb -d shell "pm uninstall -k"

Then just reinstall the app (either from an apk file or the Play Store) and the app will be back with all of its data intact.

Git checkout - autocomplete local branches only

Having Git autocomplete branch names when doing a checkout is super useful. Having the autocomplete hang for 30 seconds because it has to look up all 3000 of the remote branches in a massive repo is not so useful. It's actually pretty frustrating.

My solution: changing the git checkout autocomplete to only look at local branches, while using a git checkoutr alias to preserve the original behaviour (because sometimes it's actually needed).

How it's done:

Define an alias for checkout

We're going to use the same checkout command for the remote checkout, but need to have a different command so the script can differentiate between them. Making an alias does exactly this.

Create the alias by running git config --global alias.checkoutr checkout

Change the autocompletion function

The checkout autocomplete behaviour is defined in a function called _git_checkout in the git autocompletion file. We're going to override the function with our own version that has different autocomplete logic in it.

The location of the file varies over different operating systems and configurations, but here are a few spots to look:

  • /etc/bash_completion.d/git
  • /usr/share/bash-completion/completions/git

For a brew-installed git autocomplete on macOS, the file will probably be $(brew --prefix)/etc/bash_completion.d/git-completion.bash

Once you've found the file, copy the entire _git_checkout function into your .bashrc (or equivalent non-login shell startup script). Now look for the line

__git_complete_refs $track_opt

We're going to replace that line with:

if [ "$command" = "checkoutr" ]; then
    __git_complete_refs $track_opt
    __gitcomp_direct "$(__git_heads "" "$cur" " ")"

After saving and re-sourcing your .bashrc file, git will autocomplete local branches and tags when using git checkout, but will go back to the default behaviour of autocompleting all references when using git checkoutr.

Credits to a combination of answers on this StackOverflow post.

EDIT 2017-10-02: Updated to work with git v2.13.0 thanks to Alexander Ko's comment.
EDIT 2018-03-24: Updated to speed up branch and tag completion.

Preserving the Xposed Framework through a ROM flash

The Xposed Framework is a neat Android program that enables user-made modules to change the behaviour of the lower-level Android OS. It does this by modifying /system/bin/app_process to add some hooks into it.

The issue is that when flashing a new ROM, the modified app_process is reverted back to the original version, disabling the Xposed Framework until it's restored.


The Xposed Framework installer now includes the option to flash a zip file to install the framework instead of patching app_process directly. The method detailed in this post will still work, but the new way to make sure the framework is installed after flashing a new ROM is to simply flash the Xposed installer zip right after flashing the ROM.

The solution to this is a script that will automatically back up the existing app_process before a flash, then restore it after the flash completes.

The script does exactly that. To enable it, download it to the Android device, move it to /system/addon.d/, and give it executable permissions.

This can be done on the device with a few different programs, but it's much easier to do from a computer with adb.

Download the script and push it to the SD card of the Android device with

adb push /sdcard/

Once the file is on the device, log into it (using adb shell) and run the following commands:

su  # Get root permissions
cp /sdcard/ /system/addon.d/  # Copy the file to the correct folder
chmod 755 /system/addon.d/  # Make the file executable

After logging out of the device, the Xposed framework will stay installed and active even after flashing a new ROM. Note that this should only really be used in situations where the ROM isn't changing too much (like flashing a new nightly ROM).

In situations where app_process would actually be changed by the flash, this script could cause issues as it would restore an incorrect version of the file. If this happens, delete app_process and app_process.orig in the /system/bin/ directory, then reflash. The script won't interfere, allowing the flash to update app_process to the correct version. After rebooting, install the Xposed Framework again.

© Carey Metcalfe. Built using Pelican. Theme is subtle by Carey Metcalfe. Based on svbhack by Giulio Fidente.