Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Java annotations to guide Android's Proguard?

When using Proguard with Android, methods that are only invoked via reflection (e.g., callbacks defined in onClick XML attributes) are erroneously stripped out.

One solution for this issue is to add each affected class and method to your proguard.cfg.

How can I use Java annotations to achieve the same effect?

I feel that would make the code self-documenting and it would avoid code and proguard.cfg drifting out of sync. However, Android's Proguard doesn't seem to ship with the annotations.jar mentioned in its documentation.

like image 422
Luís Oliveira Avatar asked Dec 16 '10 08:12

Luís Oliveira


People also ask

How do you keep a class in ProGuard?

-keepclassmembernames. This is the most permissive keep directive; it lets ProGuard do almost all of its work. Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.

Does ProGuard remove unused classes?

Specifies not to shrink the input. By default, ProGuard shrinks the code: it removes all unused classes and class members. It only keeps the ones listed by the various -keep options, and the ones on which they depend, directly or indirectly.


2 Answers

You can retrieve annotations.jar and annotations.pro from an official ProGuard release. You should then be able to use annotations as discussed here

All the necessary options can go in proguard.cfg.

like image 171
Eric Lafortune Avatar answered Sep 24 '22 06:09

Eric Lafortune


I ran in to this problem recently. Here is what you need to do:

To fix the onClick events add this to Proguard settings

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

To keep annotation add

-keepattributes ** or -keepattributes *Annotation*

More information is available here http://www.simpligility.com/2010/12/hints-for-using-proguard-on-your-android-app/ I use maven-android-plugin to compile android app and this article pretty much sums up what I do for the android to get it working. Hope this helps!

like image 25
Amir Raminfar Avatar answered Sep 22 '22 06:09

Amir Raminfar