在这种情况下的用例是将文件复制到设备上受保护的位置,而不是检索它;对于检索,此问题中已经有了很好的答案。
这位OP试图将以下3个命令(在交互式shell会话中一个接一个地执行没有问题)组合成单个非交互式命令:
adb shell
run-as com.example.app
cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml
为了简单起见,让我们从一个交互式的adb shell
会话中开始。如果我们试图将最后两个命令组合成一行:
为了方便起见,让我们从一个交互式的adb shell
会话开始。如果我们尝试将最后两个命令合并成一行:
run-as com.example.app cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml
由于shell重定向的工作原理,这种方式行不通 - 命令中只有cat /sdcard/temp_prefs.xml
部分会以com.example.app
UID
执行
许多人“知道”将重定向周围的命令部分加上引号:
run-as com.example.app "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"
这样做不起作用是因为run-as
命令不够智能,无法解析整个命令。它期望下一个参数是可执行文件。正确的方法是改用sh
:
run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"
那我们只需在命令前加上adb shell
就可以了吗?并不是。从PC运行命令还会添加另一个本地shell及其解析器。特定的转义要求取决于您的操作系统。在Linux或OSX中(如果您的命令不包含任何'
),可以很容易地用单引号括起整个命令,如下所示:
adb shell 'run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"'
但有时使用替代解决方案更容易,带有(-out 或较少的)引号:
adb shell run-as com.example.app cp /sdcard/temp_prefs.xml shared_prefs/com.example.app_preferences.xml
或者,如果您的设备没有cp
命令:
adb shell run-as com.example.app dd if=/sdcard/temp_prefs.xml of=shared_prefs/com.example.app_preferences.xml
请注意我使用的是 shared_prefs/com.example.app_preferences.xml
而不是完整路径 /data/data/com.example.app/shared_prefs/com.example.app_preferences.xml
- 通常在 run-as
命令中,您当前所在的目录是您的包的 Home 目录。
按照Chris Stratton的建议,最终让我成功的方法是这样的(将共享首选项复制回设备):
adb push shared_prefs.xml /sdcard/temp_prefs.xml
cat <<EOF | adb shell
run-as com.example.app
cat /sdcard/temp_prefs.xml > /data/data/com.example.app/shared_prefs/com.example.app_preferences.xml
exit
exit
EOF
直接将数据通过管道传输到adb shell run-as
是不能正常工作的,我不知道为什么,但是通过管道传输到adb shell
可以。关键是从交互式shell中调用run-as,并且它仍然从管道接受输入。run-as
,另一个是给adb shell
本身。似乎Adb的shell对文件末尾并不会作出良好的反应。adb shell run-as
,这显然表现不同。 - Chris Strattonadb shell
后,我才想到用这种方式来做。 - Logan Pickup您可以更改目录的权限,然后提取所有文件。但对于我来说,我只是在寻找一个共享首选项文件,并且我能够像这样获取数据:
PACKAGE='com.mypackage.cool'
SHAREDPREF_FILE="${PACKAGE}_preferences.xml"
adb shell "run-as $PACKAGE cat /data/data/$PACKAGE/shared_prefs/$SHAREDPREF_FILE">$SHAREDPREF_FILE
使用最新的adb
(ADB v1.0.41 / 版本33.0.3)和Play Store模拟器映像时,我遇到了未被授权的adb root
。当run-as com.myapp.app
时,我也无法从/data/local/
或/storage/emulated/0/
复制文件,因为没有权限。
new_prefs_path="my_machine.xml"
config="$(cat $new_prefs_path)"
my_app_uri="com.myapp.app"
adb shell "run-as $my_app_uri sh -c 'echo \"$config\" > shared_prefs/on_android.xml'"
以下是我使用Bash脚本修复它的方法。由于需要为不同的应用程序和复杂的有效负载进行配置,稍微复杂了一些。
我们获取一个文件(可以在此脚本中先生成),并将其读取到变量中。
然后启动shell,执行run-as
我的应用程序,并运行echo将读取的文件扩展到shared_prefs中的一个文件。
adb shell run-as com.foo.bar
会话。 - Chris Strattoncp
命令。 - Chris Stratton