Reputation: 3753
I'm having troubles getting proguard to keep things based on their annotations (under Android).
I have an annotation, com.example.Keep:
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })
public @interface Keep {
}
I have a class, which is only ever constructed through reflection:
public class Bar extends Foo {
@com.example.Keep
Bar(String a, String b) { super(a, b); }
//...
}
It works as expected (class keeps its constructor albeit with an obfuscated class name) When my proguard config includes the following:
-keepattributes Signature,*Annotation*,SourceFile,LineNumberTable
-keepclassmembers class ** extends com.example.Bar {
<init>(...);
}
It drops the constructor if I try change that to:
-keepattributes Signature,*Annotation*,SourceFile,LineNumberTable
-keepclassmembers class ** extends com.example.Bar {
@com.example.Keep <init>(...);
}
I see the same behaviour with both my own annotations and proguard's annotation.jar annotations. I've copied and pasted the annotation name from Foo's import of Keep
, so am confident it's not a typo or incorrect import.
Can anyone think of what I might be missing?
Upvotes: 2
Views: 1658
Reputation: 45678
If your annotations are being processed as program code, you should explicitly preserve them:
-keep,allowobfuscation @interface *
The reason is somewhat technical: ProGuard may remove them in the shrinking step, so they are no longer effective in the optimization step and the obfuscation step.
Cfr. ProGuard manual > Troubleshooting > Classes or class members not being kept.
Upvotes: 7