如何从 adb shell 检查 SharedPreferences?
现在我们可以从远程 Shell 检查 sqlite3 数据库,可以从 adb shell 检查 SharedPreferences 吗? 因为在调试时从命令行检查和操作 SharedPreferences 会更加方便。
或者换句话来说,SharedPreferences保存在哪些文件中,以及如何查看和修改这些文件?
Now that we can Examining sqlite3 Databases from a Remote Shell, is it possible to examine SharedPreferences from adb shell? Since it would be much more convenient to examine and manipulate SharedPreferences from command line when debugging.
Or put in another way, in what files SharedPreferences are saved, and how to view and modify these files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果应用程序可调试,您可以这样做:
请注意,首选项可能存储在另一个文件中,这样更好
检查目录找到它:
If the app is debugable you could do:
Note that the preference might be stored in another file so better
check the directory to find it:
我正在使用这一方便的单行代码来拉取、在 vim 中编辑以及推送应用程序的共享首选项:
只需将
APP_ID
设置为您的应用程序 ID。请注意,这假设您使用共享首选项的默认文件名,该文件名是从
PreferenceManager.getDefaultSharedPreferences(context)
获取的。 此外,ADB 需要在 root 模式下运行:adb root
I am using this convenient one-liner to pull, edit in vim, and push shared preferences for an app:
Just set
APP_ID
to your application id.Note that this assumes you are using the default file name for shared preferences, as obtained from
PreferenceManager.getDefaultSharedPreferences(context)
. Also, ADB needs to be running in root mode:adb root
好吧,我在提出上述问题后就找到了该文件。 (公开提问似乎刺激我更加努力地自己寻找答案,因为我不想让我亲爱的同行将我视为一个懒惰的程序员。)
它是一个 XML 文件,位于
/data/data/your .app.package.name/shared_prefs
,文件名为 your.app.package.name_preferences.xml。 当您发现内容只是一个键值映射时,修改首选项真的很容易。Fine, I found the file just after I raised the question above. (It seem asking questions publicly stimulate me to search answers by myself much more diligently, since I don't want my dear peers to view me as a lazy programmer.)
It is an XML file under
/data/data/your.app.package.name/shared_prefs
, and the file name is your.app.package.name_preferences.xml. It is really easy to modify the preferences when you figure out that the content is just a key-value map.如果您想从 adb 脚本编辑共享首选项,请参阅如何在 Android 模拟器上获取 root 权限此处。 (如果在您的情况下使用“adb root”还不够)。
如果您想在 Android 上安装 vi 编辑器/busybox,请前往此处。 [可选]
要编辑共享首选项值,您需要首先将应用的 xml 文件复制到 SDstorage,将其复制到本地文件系统,进行编辑,然后上传回手机。
不要忘记重新启动应用程序以查看结果。
If you'd like to edit Shared Preferences from adb scripts, please see how to get root on Android emulator here. (If using 'adb root' is not enought in your case).
If you wish to install vi editor/busybox on Android go here. [OPTIONAL]
To edit a shared preference value, you need to first COPY app's xml file to SDstorage, copy it to your local filesystem, edit and then upload back to the phone.
Don't forget to RESTART the app to see the results.
bash 辅助函数
Helper bash function
首先从设备中提取共享首选项文件。 这需要root权限。
现在 MyKeys.xml 存储在系统的当前目录中。
修改值
通过编辑文件后保存更改并推送到设备来
。 验证您的更改
First pull shared preferences file from device. This requires root permision.
Now MyKeys.xml is stored in current directory of your system.
Modify values by
After editing file save changes and push to device.
To Verify your changes
如果您对 Flutter 使用 shared_preferences,则该文件为
/data/data/your.app .package.name/shared_prefs/FlutterSharedPreferences.xml
。 请注意,如果您编辑该文件,则必须重新启动应用程序才能使更改对应用程序可见。 进行热重载/热重启不会暴露您对应用程序的手动更改。If you are using shared_preferences for Flutter, the file is
/data/data/your.app.package.name/shared_prefs/FlutterSharedPreferences.xml
. Note that if you edit the file, you must restart your app for your changes to be visible to your app. Doing a hot reload/hot restart doesn't expose your manual changes to your app.如果其他人像我一样使用上述所有建议遇到“权限被拒绝”错误,您可能需要像这样使用
exec-out
:In case anyone else is running into "Permission Denied" errors using all of the above suggestions like I was, you may need to use
exec-out
like this: