1

Is it even possible to keep selected Annotations with Proguard?

For instance I would like to remove all annotations from

javax.xml.bind.annotation.*

But keep all from from

org.mycompany.annotations.MyAnnotation

I was trying to tell to Proguard to keep annotetion like below but does not work at all.

-keep @interface org.mycompany.annotations.MyAnnotation

Or should I keepattributes Annotation and then remove somehow rest of not needed annotations?

tomasz-mer
  • 3,753
  • 10
  • 50
  • 69
  • Just curious what do you want to achieve by removing annotation, code might not work at runtime. Am I missing something ? – Shailesh Chandra Dec 05 '18 at 04:35
  • It is quite simple. For instance you have an annotation MyModelClass which is used by for instance reflection. You need this one but you don't need some annotation from jaxb for instance. – tomasz-mer Dec 05 '18 at 08:16

1 Answers1

0

Not sure if I understood completely, generally we retain all annotations by using

-keepattributes *Annotation*,

If you remove this from -keepAttributes list you will lose other annotation too, and code might not work.

Ideally I will play with classes on which annotation is applied , not the annotation itself, for instance if you have annotation MyModelClass which is applied on MyService class

 @ MyModelClass
    public class MyService {
    }

and I want nothing to happen to MyService class, then I will use

-keep @MyModelClass class * {
    <methods>;
    <fields>;
}

As you mentioned you don't need some jaxb annotation, I assume they are compile time annotation.

In case you want to remove all compile time Annnotations and retain runtime annotation probably you should use -keepattributes RuntimeVisibleAnnotations

Piotr Findeisen
  • 19,480
  • 2
  • 52
  • 82
Shailesh Chandra
  • 2,164
  • 2
  • 17
  • 25
  • Maybe wrong example. Both my annotations are runtime and annotated the same classes. – tomasz-mer Dec 05 '18 at 11:59
  • so what do you want ? I guess you don't want annotation to be removed when proguard optimise the code or you want do keep those classes unchanged wherever these annotation is applied . – Shailesh Chandra Dec 06 '18 at 04:56
  • Let's look here https://stackoverflow.com/questions/47515093/keep-specific-annotation-with-proguard I want to keep class and Annotation1. So as an output I wanna to have class annotated only by Annotation1 even both are runtime. Is it clear right now? – tomasz-mer Dec 06 '18 at 08:42
  • Thanks for the link, even I could remember few of my comments there which I almost forgot. I am sorry but I am stuck at same point, If you don't need a runtime annotation, why don't you remove it from code on the first place instead of relying on proguard. or is there is something which I am missing – Shailesh Chandra Dec 06 '18 at 09:04