Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Proguard - how to keep onClick handlers only referenced from XML layouts

In my Android app I ofter don't create a View's on-click handler in code, but rely on the ability to specify it in the XML layout file, like this:

   <Button
        ....
        android:onClick="onSearchClicked"
       ...../>

And then have the method in the Activity like this:

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

Meaning there is no obvious reference to this method in my own code.

When running Proguard for a production release it seems to remove this method and the on-click fails.

What can I add to my proguard config file to avoid this that won't oblige me to rename all these methods?

  • An annotation I could add to the method and have proguard take notice of?
  • Somehow specify these types of methods referenced from xml?
  • I suppose I can add a false reference in the code, but would like to avoid that if I can as I won't always remember to put it in!

I have looked through the proguard examples for Android and can't see anything for this particular need.

like image 223
Andrew Mackenzie Avatar asked Jun 02 '11 15:06

Andrew Mackenzie


2 Answers

This seems to be the best answer, as it is 100% robust to naming of such methods:

# This will avoid all the onClick listeners referenced from XML Layouts from being removed
-keepclassmembers class * extends android.app.Activity { 
       public void *(android.view.View); 
}

Hope it helps.

like image 117
Andrew Mackenzie Avatar answered Nov 12 '22 04:11

Andrew Mackenzie


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

but double check it from proguard doc : http://proguard.sourceforge.net/index.html#/manual/refcard.html

like image 1
Snicolas Avatar answered Nov 12 '22 03:11

Snicolas