Felix.choi
Felix.choi

Reputation: 11

JsonAnySetter not called if I turn on proguard

I have a question about JsonAnySetter. As described in the subject, my JsonAnySetter handler is not called when I turn on proguard. It's OK if I turn off proguard. The following is my test code and proguard setting. Surely JsonProperty annotation is working well. it's not deleted by my proguard configuration.

public class TestJson
{
  public static class Item1
  {
  @JsonAnySetter
  public void handleUnknown(String key, Object val)
  {
    System.out.println(String.format("unknown : %s - %s", key, val.toString()));
  }
  @JsonProperty("uid")
  public long uid_ = 0;
  public static void test()
  {
    ObjectManager m = new ObjectMapper();
    m.setVisibilityChecker(....None...)

    String j1 = "{\"uid\":5, \"pos\":5, \"kk\":888, \"attr\":5}";
    Item1 item = (Item1) m.readValue(j1, Item1.class);
  }
}

Below lines is my proguard configuration I used.

-dontobfuscate
-dontoptimize
-printusage
-dontwarn
-keepattributes *Annotation*,EnclosingMethod,Signature
-keepnames class com.fasterxml.jackson.** { *; }
-keepclassmembers enum * {
  public static **[] values();
  public static ** valueOf(java.lang.String);
}

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
  native <methods>;
}
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmember class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class * {
  @fully.qualified.package.AnnotationType *;
}
-keep public class cca.news.TestJson.** { *; }

Is there anybody who can solve this problem I have? I need both of proguard and Jackson's JsonAnySetter handler.

Upvotes: 1

Views: 698

Answers (1)

Eric Lafortune
Eric Lafortune

Reputation: 45678

Jackson accesses the field uid and the method handleUnknown by reflection. ProGuard can't know this by analyzing the code, so you need to preserve them:

-keepclassmembers class cca.news.TestJson$Test {
    public void handleUnknown(java.lang.String, java.lang.Object);
    public long uid_;
}

Note the $ for the inner class -- your line with . won't match, although you can indeed use all kinds of wildcards and patterns.

You also need to preserve the annotations:

-keep @interface com.fasterxml.jackson.**

Note the -keep option -- your line with -keepnames won't be sufficient, although you could indeed just keep all classes.

Upvotes: 2

Related Questions