How can I tell ProGuard to keep my function that is used for onClick?

五迷三道 提交于 2019-12-31 21:08:24

问题


I am using the android:onClick atribute in some of my .xml layout files for my android application, but ProGuard is removing these methods from my code when it runs because nothing in my code is ever calling them.

Rather then specifying each function individually, I would like to name them something like listener_functionName, and use wildcards, like -keep listener_* (I know this is incorrect, but hopefully it illustrates my goal).

If this is possible that would be great, but if not I still need to know how to specify these functions in the proguard.cfg file. Any help is appreciated.


回答1:


According to proguard documentation:

Fields and methods may also be specified using regular expressions. Names can contain the following wildcards: ? matches any single character in a method name. * matches any part of a method name.

so, you will be find specifying

-keep class com.example.MyClass {
  public void listener_*(android.view.View);
}

in your proguard flags.




回答2:


You can do it once for all your classes in this way:

-keepclasseswithmembers class * {
    void listener_*(...);
}



回答3:


An a bit more greedy approach that should keep all "onClick" methods:

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

==> so basically every public method that has an Android View as the only Paramater should survive ProGuard then.



来源:https://stackoverflow.com/questions/6084861/how-can-i-tell-proguard-to-keep-my-function-that-is-used-for-onclick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!