Status Update
Comments
sg...@google.com <sg...@google.com>
ch...@google.com <ch...@google.com> #2
Thanks for the details. This is a duplicate of SharedPreferences.OnSharedPreferenceChangeListener
is never read, and therefore R8 will remove the field assignment.
As described in SharedPreferences.OnSharedPreferenceChangeListener
field with a @ProtectWeakReference
annotation:
public @interface ProtectWeakReference {}
You can then add the following generic keep rule to ensure that R8 does not remove the field assignment, despite the field never being read.
-keepclassmembers,allowobfuscation,allowshrinking class * {
@pkg.ProtectWeakReference <fields>;
}
do...@traveloka.com <do...@traveloka.com> #3
ch...@google.com <ch...@google.com> #4
There are no recent changes to the handling of weak references. Each new version of R8 should generally shrink better than the previous version. It could be that the field in your app was not removed with the previous version of R8, but that the new version of R8 is now sophisticated enough to optimize it away.
If you are interested in finding the fields that are removed with a new version of R8, you could consider building your app with R8 version X.Y and R8 version U.V, disassembling the two APKs, and computing the diff of the retained fields (you probably want to add -dontobfuscate
to simplify the diffing of the two).
do...@traveloka.com <do...@traveloka.com> #5
Sorry for late reply and thank you for explanation. FYI, This issue happen again after we udpating to 7.3, now we have to remove allowobfuscation,allowshrinking to make it work:
-keepclassmembers class * { @pkg.ProtectWeakReference <fields>; }
Thank you
Description
Kotlin Version: 1.5.30
Gradle version: 7.2
This is similar issue with
This time the SharedPreferences.OnSharedPreferenceChangeListener is not called properly when using R8 on AGP 7.0.2 (issue not exist on AGP 4.2.2).
On our Application class, we have a SharedPreferences.OnSharedPreferenceChangeListener listener variable an assigned it to a singleton dagger provider. This listener should called whenever we made change to the shared preference on the provider. However the listener not called when we upgrade to AGP 7.0.2 with R8 enabled. Listener only called when application on create, after that it is never called anymore.
We assign SharedPreferences.OnSharedPreferenceChangeListener listener as a field variable because OnSharedPreferenceChangeListener is using WeakReference
To solve this issue we need to keep our application class to avoid R8 optimize the SharedPreferences.OnSharedPreferenceChangeListener listener variable.
I think somehow R8 optimize this field so it become weak reference again?
I attached sample project below to show how we use SharedPreferences.OnSharedPreferenceChangeListener on MyApplication.java class. However this project not able to repro the issue.
I also provide more info of our apk and this issue on personal email to Christoffer.
Please let me know if you need additional info about this bug.
Thanks