In proguard, how to preserve a set of classes' method names?

ab11 picture ab11 · Oct 24, 2011 · Viewed 32k times · Source

I am using proguard to obfuscate my android application. The android application contains some native code, which makes callbacks to fully qualified java methods. I need to not obfuscate these classes and the names of their methods. The below properly keeps the class names, but not the method names.

-keep public class com.me.dontobf.*
-keepnames public class com.me.dontobf.*

Answer

Eric Lafortune picture Eric Lafortune · Oct 24, 2011

For native methods: ProGuard manual > Examples > Processing native methods

# note that <methods> means any method
-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}

In this case, for callback methods: ProGuard manual > Examples > Processing callback methods

-keep class mypackage.MyCallbackClass {
    void myCallbackMethod(java.lang.String);
}

Or e.g., if all public methods may be callback methods:

-keep class mypackage.MyCallbackClass {
    public <methods>;
}

You probably also need to keep any program classes that occur in the method descriptors.