13

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.

finiteloop
  • 4,444
  • 8
  • 41
  • 64

3 Answers3

22

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.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Hi Diego, could you please take a look at my question [here](http://stackoverflow.com/q/37314760/3287204)? Thank you ... – Yash Sampat May 23 '16 at 12:40
3

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

-keepclasseswithmembers class * {
    void listener_*(...);
}
ggurov
  • 1,496
  • 2
  • 15
  • 21
  • Good suggestion, although you should then use -keepclasseswithmembers instead of -keepclasseswithmembernames, to protect the methods from being renamed *and* from being removed. – Eric Lafortune Sep 19 '11 at 21:17
  • You are right. I had -dontshrink in my config and with it it was the same, but generally -keepclasseswithmembers is better. I edited my answer in this way. – ggurov Sep 20 '11 at 08:58
0

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.

donfuxx
  • 11,277
  • 6
  • 44
  • 76