Moshi: Proguard + moshi-kotlin: Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.

Created on 11 Feb 2020  Â·  7Comments  Â·  Source: square/moshi

I'm running into this issue where I can't use Proguard with the recommended proguard rules (https://github.com/square/moshi/blob/master/kotlin/reflect/src/main/resources/META-INF/proguard/moshi-kotlin.pro).

The only way I've got it to work is by keeping everything in the kotlin namespace instead of just the meta data.

-keep class kotlin.** { *; }

Seems to be related to this issue somehow? https://github.com/square/moshi/issues/345.

Moshi & Moshi Kotlin versions are 1.9.2.

Most helpful comment

Those are not appropriate rules. You might as well just turn off ProGuard instead.

On Tue, Feb 25, 2020, at 1:40 AM, xiejinpeng wrote:

same problem , moshi-kotlin reflection adapter crashed after upgrade to Moshi 1.9.2.
fixed after adding this:

-keep class kotlin.** { *; }

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/square/moshi/issues/1095?email_source=notifications&email_token=AAAQIEJOUZJ74HJQB3IEPU3RES4PVA5CNFSM4KS72SHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM2YEUY#issuecomment-590709331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAQIEITPM4LBKK4IDQ7WSTRES4PVANCNFSM4KS72SHA.

All 7 comments

same problem , moshi-kotlin reflection adapter crashed after upgrade to Moshi 1.9.2.
fixed after adding this:

-keep class kotlin.** { *; }

Those are not appropriate rules. You might as well just turn off ProGuard instead.

On Tue, Feb 25, 2020, at 1:40 AM, xiejinpeng wrote:

same problem , moshi-kotlin reflection adapter crashed after upgrade to Moshi 1.9.2.
fixed after adding this:

-keep class kotlin.** { *; }

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/square/moshi/issues/1095?email_source=notifications&email_token=AAAQIEJOUZJ74HJQB3IEPU3RES4PVA5CNFSM4KS72SHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM2YEUY#issuecomment-590709331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAQIEITPM4LBKK4IDQ7WSTRES4PVANCNFSM4KS72SHA.

Those are not appropriate rules. You might as well just turn off ProGuard instead.

On Tue, Feb 25, 2020, at 1:40 AM, xiejinpeng wrote: same problem , moshi-kotlin reflection adapter crashed after upgrade to Moshi 1.9.2. fixed after adding this: `-keep class kotlin.** { *; }

But I have no idea why the rules in README not work , is this a bug of Moshi or my fault?

We are using moshi-kotlin-codegen with version 1.8.0.
Using proguard rules described in docs works properly while building release application.

Unfortunately after update to version 1.9.2 and using proguard rules from documentation raises the issue. It could not find constructor used by generated adapter so every time there is an error while parsing

Exception:
image

image

What can we do about it?

Check out the rules created here:
https://github.com/square/moshi/tree/master/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/ProguardRules.kt

Specifically this:

-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
-keepclassmembers @com.squareup.moshi.JsonClass @kotlin.Metadata class * {
    synthetic <init>(...);
}

Are these rules being generated and respected?

On Fri, Mar 13, 2020, 4:17 AM Michał notifications@github.com wrote:

We are using moshi-kotlin-codegen with version 1.8.0.
Using proguard rules described in docs works properly while building
release application.

Unfortunately after update to version 1.9.2 and using proguard rules from
documentation raises the issue. It could not find constructor used by
generated adapter so every time there is an error while parsing

Exception:
[image: image]
https://user-images.githubusercontent.com/9006071/76607040-93ff7d80-6513-11ea-8294-9d84ac869dc5.png

[image: image]
https://user-images.githubusercontent.com/9006071/76607099-ada0c500-6513-11ea-945a-f734c5181f49.png

What can we do about it?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/1095#issuecomment-598625255, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJ4S3U2OFACAIXFXQ2S6MLRHH22DANCNFSM4KS72SHA
.

This is a pretty big issue that can easily break apps. Luckily, I re-tested the release version of my app after upgrading moshi, because I ran into the same. Now the option is to either downgrade moshi or to disable proguard. To see that this is open since 3 months makes me kinda sad.
👎

There are two issues being conflated here.

@michal-luszczuk your issue appears to be separate. You should file a new issue with a stacktrace and repro steps. in 1.9.x, all these constructors are kept by the default rules. On master, they are generated (but this isn't released yet).

For the original issue, this is a dupe of #345 and should be folded into there IMO as it has a resolution.

Was this page helpful?
0 / 5 - 0 ratings