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

R8 - ClassCastException because of removed EntryPoint HiltWrapper_ classes #3386

Closed
abd3lraouf opened this issue May 10, 2022 · 5 comments
Closed

Comments

@abd3lraouf
Copy link

R8 is failing on a release build and working normally on debug.

Caused by: java.lang.ClassCastException: Cannot cast somepackage.DaggerDemoApplication_HiltComponents_SingletonC to somepackage.r.f
...
at dagger.hilt.android.EntryPointAccessors.fromApplication(EntryPointAccessors.java:35)
at somepackage.someclass.onCreate(someclass.java:1)
EntryPointAccessors.fromApplication(
      context.applicationContext,
      CustomEntryPoint::class.java
    )

and After Decompiling the application found out that DemoApplication_HiltComponents$SingletonC doesn't implement the HiltWrapper_CustomEntryPoint class it's removed completely from the class signature. And it does appear on debug builds.

@abd3lraouf abd3lraouf changed the title R8 - ClassCastException Hilt removing custom component R8 - ClassCastException Hilt removing EntryPoint HiltWrapper_ classes May 10, 2022
@abd3lraouf abd3lraouf changed the title R8 - ClassCastException Hilt removing EntryPoint HiltWrapper_ classes R8 - ClassCastException because of removed EntryPoint HiltWrapper_ classes May 10, 2022
@danysantiago
Copy link
Member

This sounds like an R8 optimization issue, similar to #2291 (comment).

Can you tell us more about your proguard / r8 configuration. It would be helpful too if you shared some of reports as mentioned here: https://developer.android.com/studio/build/shrink-code#troubleshoot

@danysantiago
Copy link
Member

Noticed this was reported not so long ago too: #2226.

They had a non-public EntryPoint which I think you also have. Maybe we should report this to the R8 team, as it might be an interface merging issue.

@abd3lraouf
Copy link
Author

abd3lraouf commented May 11, 2022

Thank you @danysantiago for your response, really appreciate your help.

Actually was working on a side project trying to reproduce the issue for you and nearly found it.

The main issue is that hilt cannot work with libraries in release mode, meaning I cannot generate a release aar build (minification enabled) as it needs an application that will be provided later by library users.

I'm just curious if there is something else we can do about it.

@abd3lraouf
Copy link
Author

@danysantiago I've created the application to reproduce the issue

https://github.com/abd3lraouf/DaggerReleaseIssue

@danysantiago
Copy link
Member

Thanks for the repro!

The issue is not with Hilt itself but just with the nature of minification, by shrinking a library and not the final APK you need to add rules to keep your public APIs since from R8's perspective, many classes, including the SDK APIs are not used and will get removed. I see the project already has a rule for classes in your package but with code generating libraries like Dagger and Hilt you need to also consider the generated classes and specifically keep those too.

Hilt for example, generates various metadata classes in a special package called hilt_aggregated_deps, which is used to pass information across compilations and get aggregated into the 'root' where Dagger components are generated. So in order to create a consumable SDK that uses Hilt and is minified, you'll need to add a rules for the generated classes: -keep class hilt_aggregated_deps.** { *; }.

Note that this is a general trade-off when minifying or obfuscating a library, you need to verify the produced library has its public API as desired, including generated code, where as if R8 is run in the app, then it can figure out what is being used from the app and remove unnecessary code, including code from consumed libraries.

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

2 participants