-
Notifications
You must be signed in to change notification settings - Fork 545
Proguard configuration for RoboSpice projects
Some comments let us think that not all Android Coders know that Proguard is a standard Android SDK tool. You can use it to shrink, shorten, obfuscate your Android apps. The APK size will be reduced, the classes will load faster into a Dalvik VM.
This page is dedicated to configuring Proguard to build apps based on RoboSpice in release mode.
# For RoboSpice
#Results classes that only extend a generic should be preserved as they will be pruned by Proguard
#as they are "empty", others are kept
-keep class <your REST POJOs package>.**
#RoboSpice requests should be preserved in most cases
-keepclassmembers class <your RoboSpice requests package>.** {
public void set*(***);
public *** get*();
public *** is*();
}
#Warnings to be removed. Otherwise maven plugin stops, but not dangerous
-dontwarn android.support.**
-dontwarn com.sun.xml.internal.**
-dontwarn com.sun.istack.internal.**
-dontwarn org.codehaus.jackson.**
-dontwarn org.springframework.**
-dontwarn java.awt.**
-dontwarn javax.security.**
-dontwarn java.beans.**
-dontwarn javax.xml.**
-dontwarn java.util.**
-dontwarn org.w3c.dom.**
-dontwarn com.google.common.**
-dontwarn com.octo.android.robospice.persistence.**
#if you use public fields instead of setter/getters for your REST POJOs, add this
-keepclassmembers class <you REST POJOs package>.** {
public <fields>;
}
### Jackson SERIALIZER SETTINGS
-keepclassmembers,allowobfuscation class * {
@org.codehaus.jackson.annotate.* <fields>;
@org.codehaus.jackson.annotate.* <init>(...);
}
## Gson SERIALIZER SETTINGS
# See https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# Gson specific classes
#-keep class sun.misc.Unsafe { *; }
### Simple XML SERIALIZER SETTINGS
-keepclassmembers,allowobfuscation class * {
@org.simpleframework.xml.* <fields>;
@org.simpleframework.xml.* <init>(...);
}
Snippet from Google Http Java Client setup wiki page:
# Needed by google-api-client to keep generic types and @Key annotations accessed via reflection
-keepclassmembers class * {
@com.google.api.client.util.Key <fields>;
}
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
# Needed by Guava
-dontwarn sun.misc.Unsafe
# See https://groups.google.com/forum/#!topic/guava-discuss/YCZzeCiIVoI
-dontwarn com.google.common.collect.MinMaxPriorityQueue
OrmLite uses reflection, and as the Proguard Manual explains, using Proguard can lead to problems where reflection is involved if not configured properly. Since there is a trade-off between avoiding risk and gaining the benefits of using Proguard, your optimal solution may be heavily dependent on your particular situation. This issue has been discussed in various online forums, and reviewing those discussions may be the best way for you to reach a decision regarding your own circumstances.
Something like this might prevent errors while resulting in a significantly larger apk file:
-keep class com.j256.** {
*;
}
A more efficient result might be obtained by limiting the application of such options to only the classes or methods that are causing you difficulty. Here are references to some of what has been said on this issue:
- Proguard Discussion, January 2011
- OrmLite Users, January 2011
- OrmLite Users, May 2011
- OrmLite Android Users, December 2011
- StackOverflow, March 2012
@z0lope0z reports that this proguard configuration is needed when using retrofit module and proguard:
-keep class com.octo.android.robospice.retrofit.** { *; }
see https://github.com/stephanenicolas/robospice/issues/336.
Thanks to Vincent Lemeunier for helping us building this page.