Skip to content

Support R8 FullMode #1663

Open
Open

Description

@G00fY2

Currently Moshi expects JsonAdapters to have the ParameterizedType. But the R8 fullMode (which is enabled by default since AGP8) strips signatures from non-kept items. Therefore the check in parametersAreJsonAdapters (also in the Kotlin implementation) fails because of this assumption and you run into a IllegalArgumentException at runtime.

I was able to fix this by adding the following proguard rule:
-keep,allowobfuscation,allowshrinking class com.squareup.moshi.JsonAdapter

I would expect this rule as part of the META-INF/proguard/moshi.pro

Activity

  1. ZacSweers commented on Apr 20, 2023

    @ZacSweers
    Collaborator

    Wanna send a PR? I'd want to see a full description of the implications of that rule and its attributes, but open to it!

  2. G00fY2 commented on Apr 20, 2023

    @G00fY2
    Author

    Currently I just copy pasta this rule from other Android library projects, e.g. this PR which also contains a nice explanation: lysine-dev/retrofit#3579

    I think this is also true for Moshi. But I am not really an expert on this topic.

  3. ZacSweers commented on Apr 20, 2023

    @ZacSweers
    Collaborator

    Well, PR welcome but would need an explanation of the side effects. We're unlikely to accept a PR with broad proguard/R8 implications without that kind of detail.

  4. DarkAbhi commented on Apr 27, 2023

    @DarkAbhi
  5. G00fY2 commented on Apr 27, 2023

    @G00fY2
    Author

    @DarkAbhi Nope, your stacktrace seems not to be related to Moshi, since it would run into this error: https://github.com/square/moshi/blob/1.14.0/moshi/src/main/java/com/squareup/moshi/AdapterMethodsFactory.java#L226

  6. DarkAbhi commented on Apr 27, 2023

    @DarkAbhi

    @DarkAbhi Nope, your stacktrace seems not to be related to Moshi, since it would run into this error: https://github.com/square/moshi/blob/1.14.0/moshi/src/main/java/com/squareup/moshi/AdapterMethodsFactory.java#L226

    Right, thanks. I'm going to be running around a lot figuring this out..

  7. DarkAbhi commented on Apr 27, 2023

    @DarkAbhi
  8. mvanhorn commented on Oct 14, 2026

    @mvanhorn

    R8 full mode can strip generic signatures from types unmatched by keep rules. Without JsonAdapter's signature, reflection exposes a raw delegate parameter, so Moshi's parameter validation fails and delegate binding cannot recover the type argument.

    The embedded R8 rules now match JsonAdapter explicitly to retain its generic signature.

    #2139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions