简体   繁体   English

如何打开 jetpack DataStore 文件 (.preferences_pb)

[英]How to open jetpack DataStore file (.preferences_pb)

I am trying out new jetpack DataStore library.我正在尝试新的 jetpack DataStore库。 I saved data using the library.我使用库保存了数据。 This created a file settings.preferences_pb in app data directory (/data/data/my.package.name/files/datastore/settings.preferences_pb).这在应用数据目录(/data/data/my.package.name/files/datastore/settings.preferences_pb)中创建了一个文件settings.preferences_pb setting is the file name given by me. setting是我给的文件名。 The data doesn't show properly with a text viewer.使用文本查看器无法正确显示数据。 I can make out Key name but the value is garbage.我可以找出键名,但值是垃圾。 How do I open this file and view it?如何打开并查看此文件?

Here is the drive link for file settings.preferences_pb这是文件settings.preferences_pb的驱动器链接

Here is the current format for the preferences_pb file: link这是首选项_pb 文件的当前格式: 链接

You can parse the file using this schema and print it out if you need to.您可以使用此模式解析文件并在需要时将其打印出来。

Alternatively, you can just use the toString() method on the Preferences object and you should get a nice readable output.或者,您可以只在 Preferences 对象上使用 toString() 方法,您应该会得到一个很好的可读输出。

读取preferences_pd文件 I used an Hex Editor ("Hex Fiend" for macOS) and it seems understandable.我使用了十六进制编辑器(macOS 为“Hex Fiend”),这似乎可以理解。

You may refer to the preferences.proto here (thanks @rohit-sathyanarayana)你可以参考这里的preferences.proto (感谢@rohit-sathyanarayana)

The binary file is a map of <string, value> pair.二进制文件是 <string, value> 对的映射。

Each pair starts with 0x0A and length (in bytes).每对以0x0A和长度(以字节为单位)开头。 For example 0x26 means next 38 bytes.例如0x26表示接下来的 38 个字节。

Name field starts with 0x0A and length (in bytes).名称字段以0x0A和长度(以字节为单位)开头。 For example 0x04 for "name" and 0x05 for "token".例如, 0x04代表“名称”, 0x05代表“令牌”。

Value field starts with 0x12 and length (in bytes).值字段以0x12和长度(以字节为单位)开头。 For example 0x1E = next 30 bytes.例如0x1E = 接下来的 30 个字节。

First byte might indicates field type.第一个字节可能表示字段类型。 For example 0x2A = String field.例如0x2A = 字符串字段。

Second byte is length of value.第二个字节是值的长度。 For example 0x1C = 28 bytes.例如0x1C = 28 个字节。

Since 0x0A is same as line feed, you can open the preferences_db as text if most of your fields are in String format.由于0x0A与换行符相同,如果您的大多数字段都是字符串格式,您可以将首选项数据库作为文本打开。

Reference: https://medium.com/swlh/get-your-hand-dirty-with-jetpack-datastore-b1f1dfb0a5c1参考: https : //medium.com/swlh/get-your-hand-dirty-with-jetpack-datastore-b1f1dfb0a5c1


The protobuf files will be located in /data/data/{application.package}/files/datastore/ . protobuf 文件将位于 /data/data/{application.package}/files/datastore/ 中。 These are the protobuf formatted files, so we can not read them with an ordinary editor.这些是 protobuf 格式的文件,所以我们不能用普通的编辑器读取它们。 To decode the files, we can use protoc command line.要解码文件,我们可以使用protoc命令行。

To be able to use protoc command, we have to pull these files to our workstation's space using adb command为了能够使用 protoc 命令,我们必须使用 adb 命令将这些文件拉到我们的工作站空间

For preference datastore对于偏好数据存储

protoc --decode_raw < app_name.preferences_pb

The result will be similar to this:结果将与此类似:

1 {
    1: "app_name"
    2 {
        5: "Datastore sample"
    }
}
1 {
    1: "is_demo_mode"
    2 {
        1: 1
    }
}

Note: value 1 of is_demo_mode represents a true注意:is_demo_mode 的值 1 代表一个真

代码

I find a workaround from the datastore source code.我从数据存储源代码中找到了一种解决方法。 Just change the pbFile to File(filesDir, "datastore/settings.preferences_pb")只需将 pbFile 更改为 File(filesDir, "datastore/settings.preferences_pb")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM