2

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?

Martin
  • 3,703
  • 2
  • 21
  • 43

1 Answers1

7

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.

Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • This mostly worked for me, but it's still removing the parameters to the annotation... so @Hi(value="bye") becomes just @Hi – Dasmowenator Apr 22 '18 at 01:34
  • Also, could you please explain how this command works? It seems to me that you're only telling Proguard to keep all annotation interfaces you've defined, you're not telling it to keep annotations which have been put on classes or methods. If I'm wrong, could you please explain why? – Dasmowenator Apr 22 '18 at 02:00