Skip to content
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

ClassCastException thrown from unused Retrofit services with R8 #4134

Open
grandstaish opened this issue Apr 30, 2024 · 4 comments
Open

ClassCastException thrown from unused Retrofit services with R8 #4134

grandstaish opened this issue Apr 30, 2024 · 4 comments

Comments

@grandstaish
Copy link

grandstaish commented Apr 30, 2024

Bit of an edge case here—but if all of the functions in a Retrofit interface are unused, R8 will strip them. This causes the following Proguard rule to miss:

-if interface * { @retrofit2.http.* <methods>; }

Then once you try to instantiate the stripped service class, you get a fairly nondescript ClassCastException from Retrofit. E.g.:

Caused by: java.lang.ClassCastException
  at androidx.appcompat.app.ToolbarActionBar$$ExternalSyntheticThrowCCEIfNotNull0.m(:0)

Replacing the referenced rule with the following one fixes the issue for us:

-if interface *
-keepclasseswithmembers,allowobfuscation interface <1> {
  @retrofit2.http.* <methods>;
}

(Source: here)

Note: I barely know how to proguard, so this definitely requires some scrutiny. Raising issue here for visibility.

@aroyarexs
Copy link

I think we have a problem which relates to yours: https://stackoverflow.com/questions/78518260/r8-shrinker-removes-retrofit-interfaces
The difference is that we are using the methods of the interface. The methods are not unused.

But instead of using our instance that we created using Retrofit.create(Class<T> service), it somehow uses/references our demo implementation which implements our retrofit interface ... 🤷‍♂️
Adding @Keep rules to the interfaces seems to fix the issue like suggested in your source.

@Soltus
Copy link

Soltus commented May 25, 2024

Debug mode works fine, but once released mode causes very stubborn java.lang. ClassCastException

@glovebx
Copy link

glovebx commented Jun 14, 2024

Debug mode works fine, but once released mode causes very stubborn java.lang. ClassCastException

Add @keep annotation to the interface solved my problem

@Soltus
Copy link

Soltus commented Jun 14, 2024

我的问题得到了解决,方法是与服务端交互的JSON对象类不混淆,例如:
My problem is solved by not confusing JSON object classes that interact with the server level, for example:

-keep class sc.windom.sofill.dataClass.** { *; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants