Moshi: Custom adapter's annotations with Proguard

Created on 30 Sep 2015  路  5Comments  路  Source: square/moshi

Is there a configuration to make moshi work with Proguard? specifically the @FromJson and ToJson annotation?

I figured if I put this:

-keep class com.squareup.moshi.** { *; }
-keepattributes *Annotation*

it might work but I get Expected at least one @ToJson or @FromJson method on com.example.app.mypackage. Since it works perfectly when proguard is off, I'm guessing my configuration is wrong. Thanks in advance :)

Most helpful comment

-keepclassmembers class ** {
    @com.squareup.moshi.FromJson *;
    @com.squareup.moshi.ToJson *;
}

All 5 comments

Not tested, but in my experience, you can try -keepclassmembers with your methods.

this seems to work
-keep public class com.example.www.MyAdapter {
*;
}

expanding on tibbi's comment, what needs to be kept here are the (application specific) classes that Moshi is going to be serializing/deserializing to JSON.

i.e. if you have a JsonAdapter<Foo>, Foo (and anything referenced by Foo) need to be kept in proguard.

i additionally added:

-keep class com.squareup.moshi.** { *; }
-keep interface com.squareup.moshi.** { *; }
-dontwarn com.squareup.moshi.**
-keepclassmembers class ** {
    @com.squareup.moshi.FromJson *;
    @com.squareup.moshi.ToJson *;
}

The ProGuard configuration is documented now. https://github.com/square/moshi#proguard
Feel free to open a new issue or send a PR if something there isn't up to date!

Was this page helpful?
0 / 5 - 0 ratings