Keep annotated class in Proguard

2019-03-14 08:56发布

I have a bunch of classes that use e.g. an @Singleton annotation like so

@Singleton
public class ImageCache

that I would like to keep. How can I configure a proguard -keep statement so it applies to all classes that have that annotation.

Btw in terms of context I need this for an app using Roboguice on Android, which is why I added the tags. Might help others.

2条回答
劫难
2楼-- · 2019-03-14 09:36

First define an annotation

public @interface DoNotStrip {}

Then put this in proguard.cfg:

-keep,allowobfuscation @interface com.something.DoNotStrip

# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.something.DoNotStrip class *
-keepclassmembers class * {
    @com.something.DoNotStrip *;
}
查看更多
混吃等死
3楼-- · 2019-03-14 09:57

ProGuard is based on a java-like configuration with wild-cards. It does require fully qualified class names. This should work:

-keep @com.google.inject.Singleton public class *
查看更多
登录 后发表回答