Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup PROGUARD for Jackson JSON Processor?

I am using Jackson JSON Processor for my app.Included this using

compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2' compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.2'. 

And my proguard configuration is.

## BEGIN -- Google Play Services proguard.txt -keep class * extends java.util.ListResourceBundle {     protected java.lang.Object[][] getContents(); } # Keep SafeParcelable value, needed for reflection. This is required to support backwards # compatibility of some classes. -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {     public static final *** NULL; } # Keep the names of classes/members we need for client functionality. -keepnames @com.google.android.gms.common.annotation.KeepName class * -keepclassmembernames class * {     @com.google.android.gms.common.annotation.KeepName *; } # Needed for Parcelable/SafeParcelable Creators to not get stripped -keepnames class * implements android.os.Parcelable {     public static final ** CREATOR; } ## END -- Google Play Services proguard.txt  -keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault  -dontskipnonpubliclibraryclassmembers -dontskipnonpubliclibraryclasses -keepattributes *Annotation*. -keep class org.codehaus.jackson.**     -dontwarn twitter4j.** -dontwarn com.facebook.android.BuildConfig -dontwarn org.apache.commons.** -keep class org.apache.http.** { *; } -dontwarn org.apache.http.** -dontwarn com.nhaarman.listviewanimations.** 

However when i try to compile in debug mode i get the following error.

26207-26207/com.blah E/AndroidRuntime﹕ FATAL EXCEPTION: main     Process: com.blah, PID: 26207     java.lang.NoSuchFieldError: PUBLIC_ONLY             at java.lang.Class.getDeclaredAnnotation(Native Method)             at java.lang.Class.getAnnotation(Class.java:290)             at com.b.a.c.f.ah.<clinit>(Unknown Source)             at com.b.a.c.z.<clinit>(Unknown Source)             at com.blah.utils.c.<init>(Unknown Source)             at com.blah.main.a.a(Unknown Source)             at com.blah.main.b.a.a(Unknown Source)             at com.blah.main.b.a.onCreateView(Unknown Source) 

Working on it for a long time!Would appreciate your help!Thanks!

like image 300
williamj949 Avatar asked Dec 29 '14 10:12

williamj949


People also ask

How do you set ProGuard rules?

When you create a new project or module using Android Studio, the IDE creates a <module-dir>/proguard-rules.pro file for you to include your own rules. You can also include additional rules from other files by adding them to the proguardFiles property in your module's build.gradle file.

What is ProGuard configuration?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.

What is ProGuard optimization?

ProGuard optimizes Gson code by detecting which domain classes are serialized using the Gson library. It replaces the reflection-based implementation of GSON for reading and writing fields with injected and optimized code that accesses the fields of the domain classes directly when reading and writing JSON.


2 Answers

After much debugging finally found the answer my Proguard configuration is

-keepattributes *Annotation*,EnclosingMethod,Signature -keepnames class com.fasterxml.jackson.** { *; } -dontwarn com.fasterxml.jackson.databind.** -keep class org.codehaus.** { *; } -keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {     public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; } -keep public class your.class.** {     public void set*(***);     public *** get*(); } 

your class depicts the getter setter classes/class you are using to parse your response.

Also I added compile 'com.fasterxml.jackson.core:jackson-core:2.4.2' to my Gradle file which was missing previously. Now my Proguard works like a beast..;-)

like image 156
williamj949 Avatar answered Sep 22 '22 20:09

williamj949


Modified a litte from @goonerDroid work for me

-keepattributes *Annotation*,EnclosingMethod,Signature -keepnames class com.fasterxml.jackson.** { *; } -dontwarn com.fasterxml.jackson.databind.** -keep class org.codehaus.** { *; } -keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {     public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; } -keep public class your.class.** {     *; } 
like image 44
tanshiwei Avatar answered Sep 24 '22 20:09

tanshiwei