-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
After enable R8 full mode getting ParameterizedType error #3751
Comments
I'm facing a very similar issue. When i fire up the request with proguard enabled in R8 fullmode this exception raises: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType I believe that probably is a bug with R8 fullmode enabled. I'm using retrofit 2.9.0 + okHttp3 4.2.0 + square-gson-converter 2.6.1 + kotlin coroutines. |
New proguard rules added but not published yet, you can add these rules to your proguard-rules files. retrofit/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro Lines 34 to 41 in 6cd6f7d
EDIT: Now you can just simplify use these retrofit/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro Lines 38 to 48 in 2f12836
|
Now that Android Studio Flamingo with AGP 8.0 enabled R8 full mode by default, I'm facing the same issue. Could you please release a new version of Retrofit with the updated consumer rules? |
You can just copy-paste the rules #3751 (comment) into your project. |
I know, but not having to maintain R8 rules for third-party libraries is much nicer 😉 |
@Goooler
@GET("issues?format=$FORMAT&field_list=${Fields.Issues}")
@JvmSuppressWildcards
suspend fun issues(
@Query("api_key") apiKey: String,
@Query("offset") offset: Int,
@Query("limit") limit: Int,
@Query("sort", encoded = true) sort: ComicVineSort?,
@Query("filter[]", encoded = true) filter: List<ComicVineFilter>?,
): ApiResponse<IssuesResponse> |
Things about Sandwich will be fixed in skydoves/sandwich#103. |
Hi java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType |
The R8 FAQ states the following:
Does anyone know why the rules in retrofit only contain keep,allowobfuscation,allowshrinking but missing allowoptimization and allowaccessmodification? The ProGuard documentation states that allowaccessmodification should probably not be used for libraries:
But what about the allowoptimization? |
Did you able to solve it? |
@chiragthummar I just copied these rules into my project but I would still prefer a new Retrofit release 🙏🏼 |
Thanks you for your answer @svenjacobs |
Same, have you found the issue? |
ChatGPT gave me this as the answer The java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType error can occur with Retrofit and Moshi when using R8 full mode because R8 removes the generic type information during the code optimization process. Here's how you can fix this issue: Add the following Proguard/R8 rule to your proguard-rules.pro file to keep the generic type information:
If you're using Moshi, add the following Proguard/R8 rule to your proguard-rules.pro file to keep the classes that are used for JSON serialization and deserialization:
Replace com.example.MyClass with the name of your class. If you're using Kotlin, add the following Proguard/R8 rule to your proguard-rules.pro file to keep the Kotlin metadata:
Replace com.example.MyClass with the name of your class. If you're still experiencing the issue, you can try using the TypeToken class from the Gson library instead of the ParameterizedType class. To use TypeToken, replace the following code:
with:
Note that you'll need to add the following dependency to your build.gradle file:
Finally, if you're using a custom Moshi JsonAdapter, you can try adding the @JsonClass(generateAdapter = true) annotation to your data class to generate the adapter at compile-time. This can help to avoid issues with R8. For example:
These steps should help you to resolve the java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType error when using Retrofit and Moshi with R8 full mode. |
If any of you are using a custom sealed class to represent your API calls state, make sure you include that, as done in the sandwich.pro file in this PR. Your final rules can look like:
|
@eury-fsl will try this and get back. |
My Working Solutions
|
Yup, when using https://github.com/arrow-kt/arrow-integrations/tree/main/arrow-integrations-retrofit-adapter arrow adapter which turns the response into and arrow.core.Either<> I had to add exactly what was said above + |
Okay this doesn't work I'm convinced I've got something to deal with Moshi. This is my issue if anyone has a clue, thanks Github issue - #3751 (comment) |
@DarkAbhi Can you show me your rules and code for one of your APIs (the interface). |
@eury-fsl here, this is my proguard
My ApiService
I have tried to add various rules to try out, this is what it is for now - https://pastebin.com/zJPcJFYT |
@DarkAbhi Do you have a custom CallAdapter set to retrofit? |
@eury-fsl No.
|
For anyone using jackson googling for this error:
`java.lang.IllegalArgumentException: Internal error: TypeReference
constructed without actual type information`
Read this issue.
…On Fri, Jul 7, 2023 at 12:50 PM Jake Wharton ***@***.***> wrote:
All reflection-based serialization libraries will exhibit problems that
require rules to correct.
—
Reply to this email directly, view it on GitHub
<#3751 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJ5WJNWABBYCL33CWJ66M3XPA45HANCNFSM5274ZGRQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Combining officialy commited proguard rules with @digrec's solution worked for me.
|
So fix for me was to use this rule from retrofit2.pro
And remove Now I have the rule as follow and it works:
Project is multimodule with Hilt as DI and Moshi as Json convertor. Example of our interface is like this:
|
If you have tried all the solutions above and still haven't fixed the issue yet, make sure you have defined this line in every
I resolved this issue by adding the rule from retrofit2.pro to
Edited:
|
I'm sorry to ask again but is there a reason why no new version of Retrofit has been released in the past three years? Is this project still maintained? |
Thank you everyone proguard rules were effective |
I had an issue and I've applied all of the rules in addition to my custom |
For ones who need this support, you can try out my fork, use |
Just a small note: |
Closing since the fix was merged. Will be included in the next release. |
(#3751 (comment)) |
1. fix proguard error, see square/retrofit#3751 (comment) 2. sign release apk
Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (greylist, reflection, allowed)
16:09:29.013 W java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
16:09:29.014 W at retrofit2.HttpServiceMethod.parseAnnotations(SourceFile:46)
16:09:29.014 W at retrofit2.ServiceMethod.parseAnnotations(SourceFile:39)
16:09:29.014 W at retrofit2.Retrofit.loadServiceMethod(SourceFile:202)
16:09:29.014 W at retrofit2.Retrofit$1.invoke(SourceFile:160)
16:09:29.014 W at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
16:09:29.014 W at $Proxy6.generateAnonymousAuthTokenAsyn(Unknown Source)
The text was updated successfully, but these errors were encountered: