Status Update
Comments
ch...@google.com <ch...@google.com>
ch...@google.com <ch...@google.com> #2
Thanks for the report and for taking the time to diagnose this. Your analysis that R8 is inlining the value of the sharedPreferencesListener
field into the init
block is correct.
This is a duplicate issue of sharedPreferencesListener
. See also the discussion in
One approach to dealing with this is to introduce a @ProtectWeakReference
annotation, add this to the sharedPreferencesListener
field, and then include the below rule.
-keepclassmembers class * { @ProtectWeakReference <fields>; }
kr...@gmail.com <kr...@gmail.com> #3
Thanks. Could you please describe disadvantages of using
-keepclassmembers,allowobfuscation class * { <fields>; }
Is there any performance or security problem?
ch...@google.com <ch...@google.com> #4
The above rule will effectively keep all fields in the app, which will have a negative impact on the dex/apk size. I would therefore discourage using the above rule.
The rule does not impose any performance or security problems.
Description
Hi! I stacked with odd behavior in my application in release. I suppose it’s related to some R8 optimization. See my sample below:
build.gradle:
app/build.gradle:
PreferenceStorage.kt:
MainActivity.kt:
And this code works fine in debug build. But if I build my app with
debuggable false
my Switch does not move. I looked on my apk and noticed that in casedebuggable false
there is no field sharedPreferencesListener in class PreferenceStorage. I suppose this happens due to optimization by R8. It inlines the sharedPreferencesListener field into init block and my apk lost strong reference on listener, so GC collect it.For repeat this behavior in sample I have to include in proguard-rules
keep
for my PreferenceStorageSee attachments.