0

Recently ran into an issue with library, which uses moshi to create runtime TypeAdapter for serialization/deserialization. The library seems to work fine with R8 disabled, but after enabling it I ran into an error message:

Fatal Exception: java.lang.IllegalArgumentException: Cannot serialize Kotlin type org.walletconnect.impls.WCSessionStore$State. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapterFactory from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.
for class org.walletconnect.impls.WCSessionStore$State
for java.util.Map<java.lang.String, org.walletconnect.impls.WCSessionStore$State>

The solution seems to be defining keepclass R8 rule and it works.

But, prior to adding R8 rule, what bothers me is that if I decompile the minified binary I can see that the class in question (WCSessionStore$State) is not minified at all and I can clearly see that it's referenced when constructing the adapter:

public final class C12848a implements WCSessionStore {


    public C12848a(File file, C10727r rVar) {
        C11124p.m43616g(file, "storageFile");
        C11124p.m43616g(rVar, "moshi");
        this.f32438c = file;
        this.f32436a = rVar.mo25906d(C10747u.m42144j(Map.class, String.class, WCSessionStore.State.class));
.
.
.
public interface WCSessionStore {

  public State(Session$Config session$Config, Session$PeerData session$PeerData, Session$PeerData session$PeerData2, Long l, String str, List list, Long l2) {}
.
.
.

Also worth noting would be, that I'm running android.enableR8.fullMode=true

Is this expected behavior? Is the R8 rule the only way to get around this?

Lubos Mudrak
  • 651
  • 1
  • 8
  • 26
  • In general when using reflection (which serialization usually does) keep rules are needed as the R8 static analysis cannot determine the parts of the program which is accessed through reflection. The issue you run into happens https://github.com/square/moshi/blob/c5e98e5be8d6e84152d5f28b927ae077c4292d50/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.kt#L160, so it seems like there is confusion between Java and Kotlin types. If you have a reproduction you can share we might be able to update https://r8.googlesource.com/r8/+/a05c72b29a140ee6e3350bb20518a2dee081868f/compatibility-faq.md. – sgjesse Aug 04 '22 at 07:58
  • hmm, the class in question should have `currentKey: String, approvedAccounts: List, chainId: Long` - I'm not seeing those names here. Also, as I understand the message, it's not about the names being actually mangled or not. It's about Moshi being unable to tell if they're intact. – Agent_L Aug 04 '22 at 11:49

0 Answers0