Skip to content

Instantly share code, notes, and snippets.

@zGrav
Last active April 21, 2018 10:55
Show Gist options
  • Save zGrav/d91a8f2d43ca1c7e612bbc1a0a455d02 to your computer and use it in GitHub Desktop.
Save zGrav/d91a8f2d43ca1c7e612bbc1a0a455d02 to your computer and use it in GitHub Desktop.
backup apk
#!/bin/bash
if [ -z "$1" ]; then
echo "Available packages: "
adb shell pm list packages | sed 's/^package://'
echo "You must pass a package to this function!"
echo "Ex.: android_pull_apk \"com.android.contacts\""
exit 1
fi
fullname=$(adb shell pm list packages | sed 's/^package://' | grep $1)
if [ -z "$fullname" ]; then
echo "Could not find package matching $1"
exit 1
fi
if [ $(echo "$fullname" | wc -l) -ne 1 ]; then
echo "Too many packages matched:"
echo "$fullname"
exit 1
fi
echo "Will fetch APK for package $fullname"
apk_path="`adb shell pm path $fullname | sed -e 's/package://g' | tr '\n' ' ' | tr -d '[:space:]'`"
apk_name="`basename ${apk_path} | tr '\n' ' ' | tr -d '[:space:]'`"
destination="${fullname}.apk"
if [ "$(uname)" == "Darwin" ]; then
tmp="/sdcard/${fullname}.apk"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
tmp=$(mktemp --dry-run --tmpdir=/sdcard --suffix=.apk)
fi
adb shell cp "${apk_path}" "$tmp"
adb pull "$tmp" "$destination"
adb shell rm "$tmp"
[ $? -eq 0 ] && echo -e "\nAPK saved in \"$destination\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment