Monday, June 13, 2011

Button onclick & proguard

I have a simple program that has a single Button on an Android layout file, and this Button has an onClick attribute that is designing which method it should be triggered when clicked. This worked great while on development, but once I exported and installed a signed package I got a problem: NoSuchMethodException being thrown after a click.

After looking through the source code with no clue on where the problem would be, I remembered I had Proguard enabled. Proguard modifies method names to short names like: a, b, c, d and so on. So it is obvious that when clicking on a button all you will get is a NoSuchMethodException because the method itself has been renamed to something else.

To avoid this problem, I've added this to my proguard.cfg:

-keepclassmembers class package.name.YourActivity {
  public void handleXxxYyyButton(android.view.View);
}


This problem is tricky because on development mode it works perfectly as Proguard is no involved. It will only throw an exception when you actually deploy your exported application. That said, I recommend everyone to test applications with the exported signed package before releasing them to the market.

2 comments:

Mohan said...

Thank you. We are also into similar problem and seems to be no issue with the code. We will try this.

joão said...

Mohan:
Great to hear that!! =) I hope it helps you in solving the problem.