square / moshi

A modern JSON library for Kotlin and Java.
https://square.github.io/moshi/1.x/
Apache License 2.0
9.79k stars 761 forks source link

Json annotation and Proguard #411

Open ngarelimdex opened 6 years ago

ngarelimdex commented 6 years ago

I have made a simple app with just the following:

@Keep
class Simple(@Json(name = "dummy1")var dummy: Int)

....
val moshi = Moshi.Builder()
                // Add any other JsonAdapter factories.
                .add(KotlinJsonAdapterFactory())
                .build()
val simpleAdapter = moshi.adapter(Simple::class.java).lenient()
        val s = simpleAdapter.fromJson("{\"dummy1\":1}")
        Log.d("simpleAdapter", s?.dummy.toString())

And here are my proguard rules:

-dontwarn okio.**
-dontwarn javax.annotation.**

-keepclasseswithmembers class * {
    @com.squareup.moshi.* <methods>;
}
-keep @com.squareup.moshi.JsonQualifier interface *

-keepclassmembers class kotlin.Metadata {
    public <methods>;
}

-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.CallableDescriptor
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.ClassDescriptor
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.ClassifierDescriptorWithTypeParameters
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.annotations.AnnotationDescriptor
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.impl.PropertyDescriptorImpl
-dontwarn kotlin.reflect.jvm.internal.impl.load.java.JavaClassFinder
-dontwarn kotlin.reflect.jvm.internal.impl.resolve.OverridingUtil
-dontwarn kotlin.reflect.jvm.internal.impl.types.DescriptorSubstitutor
-dontwarn kotlin.reflect.jvm.internal.impl.types.DescriptorSubstitutor
-dontwarn kotlin.reflect.jvm.internal.impl.types.TypeConstructor
-keep class com.squareup.moshi.** { *; }
-keepattributes *Annotation*

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

I'm using:

compile 'com.squareup.moshi:moshi-kotlin:1.5.0'

and kotlin 1.1.51

The problem is that i get dummy = 0 when I use "dummy1" in the Json annotation. I get 1 if I use "dummy". In other words it doesn't work if the variable does not match json key.

NightlyNexus commented 6 years ago
-keepclasseswithmembers class * {
    @com.squareup.moshi.* <methods>;
}

should keep the @Json on the parameter, so I'm not sure.

Can you check in your dex file? Is there anything else in the build chain that might not be applying these ProGuard rules?

ngarelimdex commented 6 years ago

Do you mind if I send you the sample app? It would be easier this way.

NightlyNexus commented 6 years ago

Feel free to link to a sample app.

ngarelimdex commented 6 years ago

I have setup a repo here: https://gitlab.com/nicolas-garel/sampleMoshi

maidamemonimdex commented 6 years ago

Just wondering if there is any update on this issue?

premnirmal commented 6 years ago

I'm seeing a similar issue here: https://github.com/square/moshi/issues/454. Is there any update?

Bartosz-Kozajda commented 5 years ago

is there any update?

fgarcialainez commented 5 years ago

Still doesn't work even including the ProGuard rules of the README file. Please do you have a fix for this?

ZacSweers commented 5 years ago

@fgarcialainez can you link a minimal reproducible sample for the issue and more details? "doesn't work" doesn't give us enough context to help :)

fgarcialainez commented 5 years ago

Yes, sorry... Something as simple as this.

public enum MyEnum { @Json(name = "option_1") OPTION1, @Json(name = "option_2") OPTION2 }

On parsing this enum included in any Java class the application is crashing.

Thanks and Regards,

ZacSweers commented 5 years ago

Please link a sample project with a case we can run to reproduce. Since there's an implication of proguard rules impacting this it's best to see a full integration

fgarcialainez commented 5 years ago

Hi,

I don't have a sample project that I can link, but I think that this use case is really simple to reproduce.

Regards,

AndrewReitz commented 5 years ago

@fgarcialainez If it's really simple to reproduce, could you please do so in a project and upload it to GitHub? Thanks!

oalee commented 4 years ago

i had this problem with moshi version 1.8.0 while using kotlin. simple fix was using @field:Json(name = "start-over") val startOver: Boolean instead of @Json(name = "start-over") val startOver: Boolean