Keep specific Annotation with proguard

北战南征 提交于 2019-12-04 02:01:48

问题


I have two types of annotation in my project: Annotation1 and Annotation2. Both are runtime annotations. So, my question is how to keep only Annotation1 and strip Annotation2 ? I can not find is it possible. If not, I can not get - why ?

Example:

class Test {
    @Annotation1
    @Annotation2
    String name;
}

I want Annotation2 being removed from all fields and Annotation1 being kept everywhere.


回答1:


ProGuard automatically removes annotations from the classes/fields/methods if the corresponding annotation classes are not used (retrieved, cast, invoked,...) elsewhere. You can't force ProGuard to remove annotations, but you can tell it to keep seemingly unused annotations:

-keep @interface com.example.MyAnnotation



回答2:


Obfuscation is compile-time process, Ideally I would suggest not to obfuscate any annotation, but still if you want to this should solve your problem.

-keep class Annotation1

in case you want to retain members you can use below code

 -keep class Annotation1{*;}


来源:https://stackoverflow.com/questions/47515093/keep-specific-annotation-with-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!