Add keep rules for WeaklyReferencedCallback annotation
Ensure fields of types with this annotation are kept, preserving
lifecycle semantics during bytecode optimization.
A follow-up change will refactor these global rules, moving
rules specific to the framework-annotations-lib to that
target, where they should now be properly inherited by downstream
optimized targets.
Bug: 349245577
Test: atest InternalAnnotationsTests
Flag: EXEMPT bugfix
Change-Id: Iaf7694eea3d8de05a007e4c8c9e98c3eb58098ae
diff --git a/core/proguard.flags b/core/proguard.flags
index aa406b9..5148e56 100644
--- a/core/proguard.flags
+++ b/core/proguard.flags
@@ -38,6 +38,17 @@
@com.android.internal.annotations.KeepForWeakReference <fields>;
}
+# Needed to ensure callback field references are kept in their respective
+# owning classes when the downstream callback registrars only store weak refs.
+-if @com.android.internal.annotations.WeaklyReferencedCallback class *
+-keepclassmembers,allowaccessmodification class * {
+ <1> *;
+}
+-if class * extends @com.android.internal.annotations.WeaklyReferencedCallback **
+-keepclassmembers,allowaccessmodification class * {
+ <1> *;
+}
+
# Understand the common @Keep annotation from various Android packages:
# * android.support.annotation
# * androidx.annotation