Only keep enclosing method and inner classes for kept classes
Bug: 110876022
Change-Id: Ic26e5236305685f378edd927cad18431aeb61a16
diff --git a/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java b/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
index 28ca9fc..459fe7a 100644
--- a/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
+++ b/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
@@ -139,10 +139,20 @@
}
private void stripAttributes(DexProgramClass clazz) {
- if (!keep.enclosingMethod && clazz.getEnclosingMethod() != null) {
+ // If [clazz] is mentioned by a keep rule, it could be used for reflection, and we therefore
+ // need to keep the enclosing method and inner classes attributes, if requested. In Proguard
+ // compatibility mode we keep these attributes independent of whether the given class is kept.
+ if (appInfo.isPinned(clazz.type) || options.forceProguardCompatibility) {
+ if (!keep.enclosingMethod) {
+ clazz.clearEnclosingMethod();
+ }
+ if (!keep.innerClasses) {
+ clazz.clearInnerClasses();
+ }
+ } else {
+ // These attributes are only relevant for reflection, and this class is not used for
+ // reflection. (Note that clearing these attributes can enable more vertical class merging.)
clazz.clearEnclosingMethod();
- }
- if (!keep.innerClasses && !clazz.getInnerClasses().isEmpty()) {
clazz.clearInnerClasses();
}
}