14

I am trying out new jetpack DataStore library . 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) . setting is the file name given by me . 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

Manohar
  • 22,116
  • 9
  • 108
  • 144
  • 1
    Currently there doesn't seems to be option to view such data. It will be needing parser to understand how its internally stored. Android Studio 4.1 is going to come with Database Inspector(after this many years :P) but don't think there will be any for Datastore through IDE. Only options currently is to retrieve programmatically & check OR write test case to check the basics. – 333 Sep 24 '20 at 16:24
  • 1
    Any update on this? Like what exactly is this file format - .preferences_pb? Is this like a variation of the .pb format? And how to open this? – BATMAN Nov 10 '20 at 22:24
  • 1
    @KashishMalhotra no did get any info , – Manohar Nov 11 '20 at 03:26
  • @Manohar Please check my answer on the same. – Shubham AgaRwal Dec 14 '21 at 08:04

4 Answers4

6

Reference: 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/ . These are the protobuf formatted files, so we can not read them with an ordinary editor. To decode the files, we can use protoc command line.

To be able to use protoc command, we have to pull these files to our workstation’s space using adb command

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

Shubham AgaRwal
  • 4,355
  • 8
  • 41
  • 62
3

Here is the current format for the preferences_pb file: link

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.

John Pang
  • 2,403
  • 25
  • 25
2

Reading preferences_pd file I used an Hex Editor ("Hex Fiend" for macOS) and it seems understandable.

You may refer to the preferences.proto here (thanks @rohit-sathyanarayana)

The binary file is a map of <string, value> pair.

Each pair starts with 0x0A and length (in bytes). For example 0x26 means next 38 bytes.

Name field starts with 0x0A and length (in bytes). For example 0x04 for "name" and 0x05 for "token".

Value field starts with 0x12 and length (in bytes). For example 0x1E = next 30 bytes.

First byte might indicates field type. For example 0x2A = String field.

Second byte is length of value. For example 0x1C = 28 bytes.

Since 0x0A is same as line feed, you can open the preferences_db as text if most of your fields are in String format.

John Pang
  • 2,403
  • 25
  • 25
0

code

I find a workaround from the datastore source code. Just change the pbFile to File(filesDir, "datastore/settings.preferences_pb")

Warm Man
  • 93
  • 7