Proguard and Preference DataStore
Asked Answered
N

3

11

I use proguard on my project which is used android DataStore. It worked fine without proguard. But when it is obfuscated with proguard the following error occured:

java.lang.RuntimeException: Field preferences_ for f.k.k.h not found. Known fields are [public f.k.k.l.k0 f.k.k.h.i, public static final f.k.k.h f.k.k.h.j, public static volatile f.k.k.l.x0 f.k.k.h.k]
        at f.k.k.l.s0.K(Unknown Source:62)
        at f.k.k.l.s0.y(:6)
        at f.k.k.l.s0.x(Unknown Source:12)
        at f.k.k.l.a1.a(:32)
        at f.k.k.l.a1.b(Unknown Source:4)
        at f.k.k.l.x.r(:4)
        at f.k.k.h.s(:2)
        at f.k.k.k.b(:2)
        at f.k.i.e(Unknown Source:48)
        at f.k.i.f(:2)
        at f.k.i.d(:4)
        at f.k.i$b.e(:4)
        at i.n.j.a.a.l(:2)
        at d.a.p0.run(:4)
        at d.a.f2.a.u(Unknown Source:0)
        at d.a.f2.a$a.run(:7)

There is no proguard rule mentioned in the official document. Should I add some rules to my project to prevent DataStore from obfuscation?

Nagual answered 27/9, 2020 at 8:27 Comment(0)
N
9

It was solved when I add the following rule to my project:

-keep class androidx.datastore.*.** {*;}

I have reported it to the google, you can follow it there:

https://issuetracker.google.com/issues/168580258

Nagual answered 27/9, 2020 at 8:41 Comment(0)
T
6

In my case, I had problems when implementing the datastore proto, which stores objects. For me it solved like this:

my build.gradle(Module) looks like this

 //  DataStore Proto
implementation  "androidx.datastore:datastore-core:1.0.0"
implementation  "com.google.protobuf:protobuf-javalite:3.14.0"
implementation "androidx.datastore:datastore-preferences:1.0.0"

proguard-rules.pro

-keepclassmembers class * extends com.google.protobuf.GeneratedMessageLite* {
   <fields>;
}
 
Toxicosis answered 18/2, 2022 at 7:40 Comment(0)
M
0

adding to this this because I had a similar issue very recently, perhaps it may help someone else until it is fixed.

If the above solution provided above for proto-datastore does not work, put the new proguard rule in the Module's consumer-rules.pro instead.

Mcclellan answered 17/11, 2023 at 20:2 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Barnwell
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewAdonai

© 2022 - 2024 — McMap. All rights reserved.