该答案基于我对其他答案的经验以及答案中的评论。我希望我可以帮助处于类似情况的人。
我正在通过终端在OSX上执行此操作。
以前,Vinicius Avellar 的回答对我很有用。我只是大部分时间都需要来自调试应用程序的设备中的数据库。
今天我有一个需要多个私有文件的用例。我最终得到了两种适用于这种情况的解决方案。
使用接受的答案以及 Somewhere 的 OSX 特定评论。创建备份并使用第 3 方解决方案
sourceforge.net/projects/adbextractor/files/?source=navbar解压缩成 tar。我将在这个答案的底部写更多关于我使用这个解决方案的经验。如果这是您要查找的内容,请向下滚动。
我解决了一个更快的解决方案。我创建了一个用于提取多个文件的脚本,类似于 Tamas 的答案。我能够这样做是因为我的应用程序是一个调试应用程序,并且我可以在我的设备上访问 run-as。如果您无权访问 run-as,则此方法在 OSX 上对您不起作用。
这是我的脚本,用于提取多个私人文件,我将与你分享,读者也在调查这个很棒的问题;):
#!/bin/bash
#
# Strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
#
# Usage: script -f fileToPull -p packageName
#
# This script is for pulling private files from an Android device
# using run-as. Note: not all devices have run-as access, and
# application must be a debug version for run-as to work.
#
# If run-as is deactivated on your device use one of the
# alternative methods here:
# http://stackoverflow.com/questions/15558353/how-can-one-pull-the-private-data-of-ones-own-android-app
#
# If you have encrypted backup files use:
# sourceforge.net/projects/adbextractor/files/?source=navbar
# From comments in the accepted answer in the above SO question
#
# If your files aren't encrypted use the accepted answer
# ( see comments and other answers for OSX compatibility )
#
# This script is open to expansions to allow selecting
# device used. Currently first selected device from
# adb shell will be used.
#Check we have one connected device
adb devices -l | grep -e 'device\b' > /dev/null
if [ $? -gt 0 ]; then
echo "No device connected to adb."
exit 1
fi
# Set filename or directory to pull from device
# Set package name we will run as
while getopts f:p: opt; do
case $opt in
f)
fileToPull=$OPTARG
;;
p)
packageName=$OPTARG
;;
esac
done;
# Block file arg from being blank
if [ -z "$fileToPull" ]; then
echo "Please specify file or folder to pull with -f argument"
exit 1
fi
# Block package name arg from being blank
if [ -z "$packageName" ]; then
echo "Please specify package name to run as when pulling file"
exit 1
fi
# Check package exists
adb shell pm list packages | grep "$packageName" > /dev/null
if [ $? -gt 0 ]; then
echo "Package name $packageName does not exist on device"
exit 1
fi
# Check file exists and has permission with run-as
fileCheck=`adb shell "run-as $packageName ls $fileToPull"`
if [[ $fileCheck =~ "Permission denied" ]] || [[ $fileCheck =~ "No such file or directory" ]]; then
echo "Error: $fileCheck"
echo "With file -> $fileToPull"
exit 1
fi
# Function to pull private file
#
# param 1 = package name
# param 2 = file to pull
# param 3 = output file
function pull_private_file () {
mkdir -p `dirname $3`
echo -e "\033[0;35m***" >&2
echo -e "\033[0;36m Coping file $2 -> $3" >&2
echo -e "\033[0;35m***\033[0m" >&2
adb shell "run-as $1 cat $2" > $3
}
# Check if a file is a directory
#
# param 1 = directory to check
function is_file_dir() {
adb shell "if [ -d \"$1\" ]; then echo TRUE; fi"
}
# Check if a file is a symbolic link
#
# param 1 = directory to check
function is_file_symlink() {
adb shell "if [ -L \"$1\" ]; then echo TRUE; fi"
}
# recursively pull files from device connected to adb
#
# param 1 = package name
# param 2 = file to pull
# param 3 = output file
function recurse_pull_private_files() {
is_dir=`is_file_dir "$2"`
is_symlink=`is_file_symlink "$2"`
if [ -n "$is_dir" ]; then
files=`adb shell "run-as $1 ls \"$2\""`
# Handle the case where directory is a symbolic link
if [ -n "$is_symlink" ]; then
correctPath=`adb shell "run-as $1 ls -l \"$2\"" | sed 's/.*-> //' | tr -d '\r'`
files=`adb shell "run-as $1 ls \"$correctPath\""`
fi
for i in $files; do
# Android adds nasty carriage return that screws with bash vars
# This removes it. Otherwise weird behavior happens
fileName=`echo "$i" | tr -d '\r'`
nextFile="$2/$fileName"
nextOutput="$3/$fileName"
recurse_pull_private_files "$1" "$nextFile" "$nextOutput"
done
else
pull_private_file "$1" "$2" "$3"
fi
}
recurse_pull_private_files "$packageName" "$fileToPull" "`basename "$fileToPull"`"
要点:
https ://gist.github.com/davethomas11/6c88f92c6221ffe6bc26de7335107dd4
回到方法 1 ,使用Android Backup Extractor解密备份
以下是我在 Mac 上采取的步骤,以及遇到的问题:
首先,我排队备份(并设置密码来加密我的备份,我的设备需要它):
adb backup -f myAndroidBackup.ab com.corp.appName
其次,我从这里下载了 abe.jar:https ://sourceforge.net/projects/adbextractor/files/abe.jar/download
接下来我跑了:
java -jar ./abe.jar unpack myAndroidBackup.ab myAndroidBackup.tar
此时我收到一条错误消息。因为我的存档是加密的,java 给了我一个错误,我需要安装一些安全策略库。
最后我刚刚跑了(再次运行上一个命令之后)
tar xvf myAndroidBackup.tar
现在重要的是要注意,如果您可以只 run-as 和 cat,它会快得多。一,你只得到你想要的文件,而不是整个应用程序。第二,文件越多(对我来说是 + 加密),传输速度就越慢。因此,如果您在 OSX 上没有运行方式,那么知道这样做很重要,但脚本应该首先转到调试应用程序。
请注意,我今天才写它并测试了几次,所以请通知我任何错误!