Home » Android » java – How to configure Proguard to keep static methods with different return times

java – How to configure Proguard to keep static methods with different return times

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a set of classes (more than 50) that contain a few static methods called parse() that returns an instance of that class. This is an example of one of those classes:

class SomeType {

   // Parse methods
   public static SomeType parse(String text) { ... }
   public static SomeType parse(Object obj) { ... }
   ...

   // Other methods
   public void static somethingStatic(...) { ... }
   public void somethingNotStatic(...) { ... }
   ...
}

I’m trying to configure ProGuard to obfuscate these classes and all their methods and fields except the parse(...) methods. Basically, I want to obfuscate the class name, all static and non-static methods, plus the class fields.

I’ve tried to use:

-keepclassmembers class SomeType {
  public static SomeType parse(***);
}

and that worked fine for SomeType, but I don’t want to have to write this rule for each of my 50 classes… so how can I generalize it?

I tried:

-keepclassmembers class ** {
  public static * parse(***);
}

but ProGuard complains about the syntax of the return type…

How to&Answers:

Your rule was almost correct, just use *** as return type, which will match any type:

-keepclassmembers class ** {
   public static *** parse(***);
}

Also -keepclassmembers is preferred over -keepclasseswithmembers as it will only keep the methods you specify and not the class itself (which is probably not needed in your case as you describe).

If you have more than one argument for the parse methods, you should use:

-keepclassmembers class ** {
   public static *** parse(...);
}

The ... will match any number of arguments of any type.

Answer:

-keepclasseswithmembers class * {
  public static *** parse(***);
}