question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

R8 - ClassCastException because of removed EntryPoint HiltWrapper_ classes

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
danysantiagocommented, May 11, 2022

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.

0reactions
abd3lraoufcommented, May 11, 2022

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

https://github.com/abd3lraouf/DaggerReleaseIssue

Read more comments on GitHub >

github_iconTop Results From Across the Web

ClassCastException after obfuscation internal classes with ...
Now the true weird problem is that the ClassCastException disappears as soon as I remove the suspend modifier. I tried adding - ...
Read more >
R8 FAQ
So if instances of a given class are only created through deserialization from JSON, R8 will not see that class as instantiated leaving...
Read more >
ClassCastException after updating to R8 2.0.59 (Android ...
After updating the Android Gradle plugin from version 3.6.2 to 4.0.0-beta04 I get a ClassCastException in a release build:
Read more >
ProGuard Troubleshooting
Possibly, some class file didn't get recompiled properly, or some class file was left behind after its source file was removed. Try removing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found