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.

Hilt: More meaningful error messages

See original GitHub issue

First of all, Hilt looks like a really promising project, keep up the good work!

One feature I would like to see is more meaningful error messages at run time and compile time. For instance, one error I always seem to get is:

Caused by: java.lang.ClassCastException: com.mypackage.DaggerMainApplication_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl cannot be cast to com.com.mypackage.MyActivity_GeneratedInjector

This runtime exception basically means that there is an error in the dependency graph meaning something can’t be injected, but it would be nice to know what exactly.

Even better, if this problem could be caught and displayed at compile time, then fantastic 😃

kind regards, goldy1992

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
ChanSekcommented, Jul 11, 2020

@Chang-Eric, is it expected to occur in below scenario?

:ui-component module - ‘com.android.library’ :profile module - ‘com.android.library’

implementation project(':ui-component')

:app module - ‘com.android.application’

implementation project(':ui-component')
implementation project(':profile')

In the above project, I have my Application class in :ui-component module.

@HiltAndroidApp
class CommonApp : Application()

And my Activity is in :profile module.

@AndroidEntryPoint
class ProfileActivity : BaseActivity(R.layout.activity_profile)

But when I run this app, I get below exception at runtime.

Process: com..., PID: 11338
    java.lang.RuntimeException: Unable to start activity ComponentInfo{.../....ProfileActivity}: java.lang.ClassCastException: ....DaggerCommonApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl cannot be cast to ...ProfileActivity_GeneratedInjector
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6123)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
     Caused by: java.lang.ClassCastException: ....DaggerCommonApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl cannot be cast to ...ProfileActivity_GeneratedInjector
        at ....Hilt_ProfileActivity.inject(Hilt_ProfileActivity.java:56)
        at ....Hilt_ProfileActivity.onCreate(Hilt_ProfileActivity.java:31)
        at ....ProfileActivity.onCreate(ProfileActivity.kt:25)
        at android.app.Activity.performCreate(Activity.java:6723)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6123) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

What could go wrong here? And why it’s a runtime error instead of compile time?

4reactions
martipellocommented, Jul 10, 2020

i just got an error and all it said was [Hilt] lol

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin + hilt error. [Hilt] and java.lang.reflect ... - Stack Overflow
Recently I started learning Kotlin and Hilt for my project. I watched some tutorials and afterwhile tried to write my own code.
Read more >
Debugging with Hilt / Dagger / Dependency Injection - Reddit
I've tried "Run with --info or --debug option to get more log output. ... You might actually get a useful error message that...
Read more >
Dependency injection with Hilt | Android Developers
More. Platform Android Studio Google Play Jetpack Kotlin Games. Language ... Transfer assets · Send and receive messages · Handle data layer events ......
Read more >
Migrating to Hilt - Dagger
You can find out when Hilt injects classes for each Android class here. These hopefully should be similar to where your code currently...
Read more >
MAD Skills series: Hilt under the hood | by Brad Corso - Medium
As this blog post is about Hilt, we won't go into further details about the Dagger generated code. However, if you're interested, ...
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