How to keep class which implement an interface with annotation

拥有回忆 提交于 2020-01-04 05:31:46

问题


I defined an annotation named @KeepAll.

I have an interface like

@KeepAll
public interface MainEntity {
    //some methods
}

I want to keep all classes which implement this interface from obfuscation. Is this possible on ProGuard?

NOTE I know I can define it as

-keep public class * implements **.MainEntity 

But I don't want to specify interface name but annotation name.


回答1:


After a long trial and error process I get what I want. Here is the solution

Keep class names with annotation KeepAll;

-keep @com.package.name.KeepAll public class **

Keep class members of classes and interface with annotation KeepAll;

-keepclassmembers @com.package.name.KeepAll class ** { public <methods>; <fields>;}

Keep class members of a class which implemets a class that has KeepAll annotation. (This was what I want)

-keepclassmembers public class * implements @com.package.name.KeepAll ** { public <methods>; <fields>;}



回答2:


You can tell ProGuard to keep everything with an annotation like this:

-keep @com.google.inject.Singleton public interface *

The above would keep the interface itself from obfuscation.

To get the implementations of the interface you can do something like this:

-keep public class * implements **.MainEntity 

So Right now I am confused what you want to achieve. If you only annotate the interface it wont be a help for ProGuard. The classes would need this annotation.



来源:https://stackoverflow.com/questions/45833694/how-to-keep-class-which-implement-an-interface-with-annotation

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