Skip to content

ClassCastException thrown from unused Retrofit services with R8  #4134

Open

Description

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.

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions