google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.44k stars 2.02k forks source link

R8 - ClassCastException because of removed EntryPoint HiltWrapper_ classes #3386

Closed abd3lraouf closed 2 years ago

abd3lraouf commented 2 years ago

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.

danysantiago commented 2 years ago

This sounds like an R8 optimization issue, similar to https://github.com/google/dagger/issues/2291#issuecomment-772096011.

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 commented 2 years ago

Noticed this was reported not so long ago too: https://github.com/google/dagger/issues/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 commented 2 years ago

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 commented 2 years ago

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

https://github.com/abd3lraouf/DaggerReleaseIssue

danysantiago commented 2 years ago

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.