Android 如何从 adb shell 检查 SharedPreferences?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1243079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:49:38  来源:igfitidea点击:

How to examine SharedPreferences from adb shell?

androidadbandroid-sharedpreferences

提问by an0

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.

现在我们可以从远程外壳检查 sqlite3 数据库,是否可以从 adb shell 中检查 SharedPreferences?因为在调试时从命令行检查和操作 SharedPreferences 会更方便。

Or put in another way, in what files SharedPreferences are saved, and how to view and modify these files?

或者换句话说,SharedPreferences 保存在哪些文件中,以及如何查看和修改这些文件?

回答by an0

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.

它是 下的一个 XML 文件/data/data/your.app.package.name/shared_prefs,文件名是 your.app.package.name_preferences.xml。当您发现内容只是一个键值映射时,修改首选项真的很容易。

回答by joecks

If the app is debugableyou could do:

如果应用程序是可调试的,您可以执行以下操作:

$ adb shell
$ run-as <app-package-id>
$ cat /data/data/<app-package-id>/shared_prefs/prefs.xml

Note that the preference might be stored in another file so better check the directory to find it:

请注意,首选项可能存储在另一个文件中,因此最好检查目录以找到它:

$ ls /data/data/<app-package-id>/shared_prefs/

回答by friederbluemle

I am using this convenient one-liner to pull, edit in vim, and push shared preferences for an app:

我正在使用这个方便的单行程序来拉取、在 vim 中编辑和推送应用程序的共享首选项:

APP_ID=com.myapp; adb pull /data/data/${APP_ID}/shared_prefs/${APP_ID}_preferences.xml /tmp/${APP_ID}_preferences.xml && vim /tmp/${APP_ID}_preferences.xml && adb push /tmp/${APP_ID}_preferences.xml /data/data/${APP_ID}/shared_prefs/

Just set APP_IDto your application id.

只需设置APP_ID为您的应用程序 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

请注意,这假设您使用共享首选项的默认文件名,如从PreferenceManager.getDefaultSharedPreferences(context). 此外,ADB 需要在 root 模式下运行:adb root

回答by eleven

Helper bash function

辅助 bash 功能

function adb-pull-prefs {
    #  - app package
    #  - prefs name
    adb exec-out run-as  cat /data/data//shared_prefs/.xml
}

回答by CFL_Jeff

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-outlike this:

如果其他人像我一样使用上述所有建议遇到​​“权限被拒绝”错误,您可能需要exec-out像这样使用:

adb exec-out run-as <package.name> cat /data/data/<package.name>/shared_prefs/<package.name>_preferences.xml

回答by Shiv Buyya

First pull shared preferences file from device. This requires root permision.

首先从设备中提取共享首选项文件。这需要root权限。

adb pull /data/data/org.test/shared_prefs/MyKeys.xml MyKeys.xml

Now MyKeys.xml is stored in current directory of your system.

现在 MyKeys.xml 存储在您系统的当前目录中。

Modify values by

修改值

vim MyKeys.xml

After editing file save changes and push to device.

编辑文件后保存更改并推送到设备。

adb push MyKeys.xml /data/data/org.test/shared_prefs/MyKeys.xml

To Verify your changes

验证您的更改

adb shell
cat /data/data/org.test/shared_prefs/MyKeys.xml