Moshi KotlinJsonAdapterFactory cannot parse Json after enabled minify

You can also use the Moshi "codegen" annotation processor.

Add the dependency:

kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"

Then annotate your models with @JsonClass:

@JsonClass(generateAdapter = true)
data class MyModel(...)

You also need to annotate any enum classes used by models, but they don't need an adapter:

@JsonClass(generateAdapter = false)
enum class MyEnumClass { ... }

The previously mentioned Moshi Proguard rules from here and here are also required.


-keepclasseswithmembers class * {
    @com.squareup.moshi.* <methods>;
}
-keep @com.squareup.moshi.JsonQualifier interface *
-dontwarn org.jetbrains.annotations.**
-keep class kotlin.Metadata { *; }
-keepclassmembers class kotlin.Metadata {
    public <methods>;
}

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

-keepnames @kotlin.Metadata class (Change with Yourpackagename) com.myapp.packagename.model.**
-keep class (Change with Yourpackagename) com.myapp.packagnename.model.** { *; }
-keepclassmembers class (Change with Yourpackagename) com.myapp.packagename.model.** { *; }

Change com.myapp.packagnename to your packagename

OR

Replace moshi-kotlin with kotshi which plays fine with Proguard without special rules, dropping hundreds of kilobytes in the final apk.


The only solution worked for me is mentioned in the comment by @JeganBabu

i.e. Changing @Json(name="field_name") into @field:Json(name="field_name").

Probably the reason is the annotation is not applied to the converted Java code if field: was not added. Pretty odd.

For your reference: (Moshi in kotlin) @Json vs @field:Json