square / moshi

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

Support R8 FullMode #1663

Open G00fY2 opened 1 year ago

G00fY2 commented 1 year ago

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

ZacSweers commented 1 year ago

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!

G00fY2 commented 1 year ago

Currently I just copy pasta this rule from other Android library projects, e.g. this PR which also contains a nice explanation: https://github.com/square/retrofit/pull/3579

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

ZacSweers commented 1 year ago

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.

DarkAbhi commented 1 year ago

Is this error due to moshi?

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:46)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:39)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy11.checkIfEmailExists(Unknown Source)
at com.iku.data.network.service.IkuApiService$DefaultImpls.checkIfEmailExists$default(IkuApiService.kt:123)
at com.iku.auth.data.network.repository.AuthRepositoryImpl$checkEmail$2.invokeSuspend(AuthRepositoryImpl.kt:24)
at com.iku.auth.data.network.repository.AuthRepositoryImpl$checkEmail$2.invoke(AuthRepositoryImpl.kt:0)
at com.iku.auth.data.network.repository.AuthRepositoryImpl$checkEmail$2.invoke(AuthRepositoryImpl.kt:0)
at com.iku.data.network.helper.NetworkHelperKt$safeApiCall$2.invokeSuspend(NetworkHelper.kt:27)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

I have retrofit and moshi, just upraded to AS flamingo with AGP 8 and Java 17. This error causes retrofit calls to not happen. Here's more details on stackoverflow - https://stackoverflow.com/questions/76118721/r8-full-mode-throws-class-cast-exception-agp-8-0

G00fY2 commented 1 year ago

@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

DarkAbhi commented 1 year ago

@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..

DarkAbhi commented 1 year ago

@G00fY2 @ZacSweers Would you have any idea about this issue? This is the only live project I have with Moshi, my other projects with GSON work fine so far after AGP 8 and Flamingo update. Thanks!