Using annotations for Keep directive does not keep them during optimization
Java class file shrinker, optimizer, obfuscator, and preverifier
Brought to you by:
guardsquare
I use annotation like @Keep to keep classes, methods and fields that are used only with reflection. But, the annotations seem not to be respected during optimisation.
The attached example only works if the class “test.ColorIntAdapter” is kept explicitly, but not when kept via annotations: constructor and methods are removed:
@KeepPublicClassMembers
public class ColorIntAdapter extends IntAdapter<Color>
{
@Keep
public ColorIntAdapter()
{
}
@Keep
@Override
public int toInt(Color t) {
return t.getRGB();
}
}
Some parts of the config:
-keepclassmembers class * {
@proguard.annotation.Keep *;
}
-keepclassmembers @proguard.annotation.KeepPublicClassMembers class * {
public *;
}
(But maybe I misunderstood the documentation - you may correct my config)
Sorry about the delay... The annotation KeepPublicClassMembers corresponds to the option -keepclassmembers. It specifies to keep all public class members (fields and methods) of the annotated class -- not necessarily the annotated class itself. You should specify Keep, which corresponds to -keep -- they keep the annotated class. The Keep annotations on the methods already keep the methods.