Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude some classes from Proguard's keep rules

Tags:

proguard

I have a library that is about to be obfuscated using ProGuard. "Library mode" is almost applicable for my use case, i.e. it is almost fine to keep all public and protected classes and class members. However due to Java's visibility requirements some members cannot be made package private or private and thus they are public classes although they should not be in the library. I would like to have them obfuscated to make it more clearly that these classes do not belong to the public api, as well as to get better obfuscation and smaller library jars. Is there a way to exclude some items from a proguard "keep" rule without specifying each of these items by name (using the '!'). Ideally I would like to annotate these classes and members with a tagging annotation, but as far as I understand Proguard can only be told to keep items with certain annotations.

like image 663
Sebastian Avatar asked Nov 07 '11 14:11

Sebastian


People also ask

Does ProGuard remove unused classes?

Shrinking Options By default, ProGuard shrinks the code: it removes all unused classes and class members.

What is Dontwarn in ProGuard?

If your code works fine without the missing classes, you can suppress the warnings with '-dontwarn' options. (


1 Answers

You can only keep items indeed. If you want to exclude certain class members, you have to do so by listing or annotating the class members that you do want to keep. When specifying a class name, you can provide a list, optionally with "!" to exclude names. When specifying a class member name and type, that is not possible. Still, in both cases, you can use wildcards. If you pick special names for your internal classes, this might work:

-keep public class * {
  public protected *** !myInternalField*;
  public protected *** !myInternalMethod*(...);
}
like image 179
Eric Lafortune Avatar answered Oct 12 '22 00:10

Eric Lafortune