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: Injection not working in BroadcastReceiver

See original GitHub issue

The issue is described on Stackoverflow https://stackoverflow.com/questions/62335727/hilt-injection-not-working-with-broadcastreceiver

I am not the poster, but I did encounter the same issue.

This workaround prevented the crash:

  override fun onReceive(context: Context, intent: Intent?) {
    val injector = BroadcastReceiverComponentManager.generatedComponent(context) as AppBroadcastReceiver_GeneratedInjector
    injector.injectAppBroadcastReceiver(UnsafeCasts.unsafeCast(this))

Issue Analytics

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

github_iconTop GitHub Comments

19reactions
Thomas-Voscommented, Jun 15, 2020

Another workaround is to use a class like this:

abstract class DaggerBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {}
}

Now extend this class instead of BroadcastReceiver and you can call super.onReceive().

4reactions
NitroG42commented, Sep 24, 2020

Fix : So basically, I had

    override fun onReceive(context: Context, intent: Intent) {

        GlobalScope.launch {
            //Doing stuff
        }
    }

I moved the code in another method

    override fun onReceive(context: Context, intent: Intent) {

        myMethod()
    }

fun myMethod() {
        GlobalScope.launch {
            //Doing stuff
        }
}

and voilà!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hilt Injection not working with BroadcastReceiver
How it works internally: Hilt creates abstract class for @AndroidEntryPoint annotated component(Activity, Fragment, BroadcastReceiver etc) and ...
Read more >
Dependency injection with Hilt | Android Developers
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
Read more >
Migration to Dagger Hilt, what the guides are missing - Medium
BroadcastReceiver not being injected 🤦​​ Injection happens in the parent's onReceive() method, however the method is abstract and we can't call ...
Read more >
A Standard Way to Implement Dependency Injection in Android
BroadcastReceiver. Inject dependencies into non-Android classes. Even though Hilt supports most of the Android classes, at some point, you'll need ...
Read more >
The practical guide – Part 4: Dependency injection with Hilt
How to fix this? One thing you can think of is by creating some class where you handle all the dependencies there (Like...
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