Proguard and reflection in Android Proguard and reflection in Android android android

Proguard and reflection in Android


SOLVED

For others that are having this problem you need to add the following to proguard.cnf

-keep public class * extends com.yoursite.android.yourappname.YourClassName-keepclassmembers class * extends com.yoursite.android.yourappname.YourClassName{ public <init>(android.content.Context);}

The first keep tells proguard to not obfuscate class names that extend YourClassName

The second one says to keep the constructor name (<init> means constructor) un-obfuscated that has a single argument of Context and extends YourClassName

In addition, for android developers that are using the onClick attribute in you XML layouts file you will also need to add the name of the function in your proguard.cnf file.

-keepclassmembers class * { public void myClickHandler(android.view.View);}

This says keep all methods named myClickHandler with a single argument View in all classes. You could further constrain this by using the extends keyword like above.

hope this helps.


For the on click fix you don't have to list each method name. You can do:

-keepclassmembers class * {   public void *(android.view.View);}

which find all methods that have a View as parameter.


Because Compilation process will remove the method which is not used ,so the constructor method will be remove even if you use reflection to call during the compilation process.

You could see the method which is be removed during compilation process in the the usage.txt in your project.

So you should keep the constructor method in proguard file.

-keepclassmembers class * extends com.yoursite.android.yourappname.YourClassName{ public <init>(android.content.Context);}