moshiにProGuardを適用してハマるの巻
moshiはsquare謹製のAndroid/Java向けJSONライブラリ。
Githubに
Moshi uses the same streaming and binding mechanisms as Gson.
とあるように、定番ライブラリであるGsonと同じ思想で作られています。違いは、重装備なGsonに対してシンプルで軽量なmoshi、という感じ。
今回はJSONをObjectにマッピングできればOKという要件だったので、ものは試しとmoshiを使ってみました。square謹製ということでこれから流行るかもしれないですし。
で、AndroidでJSONを扱うときの頭痛の種がProGuard。ご多分に漏れずmoshiでもハマったので備忘録を残しておきます。
公式のドキュメントに言及が見当たらないので、issueを検索して出てきたCustom adapter’s annotations with Proguard · Issue #93 · square/moshi · GitHubを参考に以下を追加します。
-keep class com.squareup.moshi.** { *; }
-keep interface com.squareup.moshi.** { *; }
-dontwarn com.squareup.moshi.**
## Custom Type Adapterを使う場合に必要(今回は要らなかった)
-keepclassmembers class ** {
@com.squareup.moshi.FromJson *;
@com.squareup.moshi.ToJson *;
}
マッピング対象となるクラス群も保護してあげる必要アリですね。com.example.entity
以下にまとめて置いてある場合はこんな感じ。
-keep class com.example.entity.** { *; }
これでバッチリ、と思いきや上手くいきません。
マッピング対象のクラス内でList
-keepattributes Signature
ということで、改めてまとめるとこんな感じになります。
## moshi
-keep class com.squareup.moshi.** { *; }
-keep interface com.squareup.moshi.** { *; }
-dontwarn com.squareup.moshi.**
## Custom Type Adapterを使う場合に必要
-keepclassmembers class ** {
@com.squareup.moshi.FromJson *;
@com.squareup.moshi.ToJson *;
}
## Generics
-keepattributes Signature
## マッピング対象のクラス
-keep class com.example.entity.** { *; }
この辺、公式ドキュメントにまとめてくれないかなあ...
※参考URL:JSONIC & google-gson とProguardでドツボる。 - Androidとたわむれる日々。
関連(するかもしれない)記事
おススメ