开发者

android onClick events and pro guard

i'm trying to obfuscate my app before uploading to the market. i've setup pro guard and i allready handled the Serialzie issue (prog guard maul - 开发者_开发技巧serialize) however i still have a problem (MethodNotFoundException) when trying to press one of the buttons in my home screen. they are simple LinearLayout with background that in the xml have the android:onClick="doOnClick" attribute. in my HomeScreenActivity i have a method named :

public void doOnClick(View v){...}

that should be called whenever a button is pressed. the code works GREAT when not obfuscated, but once obfuscated android class loader in unable to find my method. Before i move in to use only code and not xml callbacks (maybe advisable) i would like to know if there is a nice way around it. Trying to prevent method obfuscation did not work for me (trying to prevent obfuscation of methods that extends Activity and are of the form public void on(android.view.View); or public void On(android.view.View).

If anyone have done it i'll appreciate the hint :-)


It's really simple, this should work.

-keepclasseswithmembers class MyActivity {
   public void doOnClick(android.view.View);
}


Add this to your proguards file.

-keep class com.android.toto.ClassName {
    public void doOnClick(android.view.View);
}


the -keep or keepclasswithmemebers do not apply since i already have -keep public class * extends android.app.Activity which will include ALL Activities.

I managed to find the problem in which the methods had been totally removed by proguard optimization and replaced with direct inline code. This happens because there are no specific calls to those functions in code; note that the XML calls to the code aren't taken into account.

adding the following to the proguard configuration solves the problem:

-keepclassmembers class * extends android.app.Activity {
   public void *On*Click(android.view.View);
   public void *on*Click(android.view.View);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜