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 {
  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.


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...

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


Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. vShare iOS 11

2009 DREAM said...

I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. Jailbreak iOS 11.3