Hilt Injection not working with BroadcastReceiver

Update: According to the issue the problem should be fixed in version 2.29.1 of Dagger Hilt. So, just use version 2.29.1-alpha or above. Don't forget to update hilt-android-gradle-plugin version as well.


Original answer: There's a GitHub issue and a workaround. It seems that injection doesn't work because it actually happens inside onReceive() method of the generated parent class. The problem is that you cannot call the super method since it's abstract. But you can create a simple wrapper class that fixes the problem:

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

@AndroidEntryPoint
class MyBroadcastReceiver : HiltBroadcastReceiver() {
  @Inject lateinit var testHiltInjection: TestHiltInjection

  override fun onReceive(context: Context?, intent: Intent?) {
    super.onReceive(context, intent) // <-- it's the trick

    ...
  }
}

With the stable version of Hilt. you can easily inject any variable like this

@AndroidEntryPoint
public class SMSReceiver extends BroadcastReceiver {
    @Inject
    public DataManager dataManager;

    @Override
    public void onReceive(final Context context, Intent intent) {
    }
}

I think hilt currently doesn't support BroadcastReceiver. In https://dagger.dev/hilt/android-entry-point says

Note: Hilt currently only supports activities that extend ComponentActivity and fragments that extend androidx library Fragment, not the (now deprecated) Fragment in the Android platform.

How it works internally: Hilt creates abstract class for @AndroidEntryPoint annotated component(Activity, Fragment, BroadcastReceiver etc) and your BroadcastReceiver extends generated class as base class in bytecode transformation. In base class onReceive method object is injected. But in generated bytecode class super class onReceive method is not called. That's why your object is not injected. To test add below code before testHiltInjection() in HiltBroadcastReceiver class. By the way it's still in Alpha mode.

((HiltBroadcastReceiver_GeneratedInjector) BroadcastReceiverComponentManager.generatedComponent(context)).injectHiltBroadcastReceiver(UnsafeCasts.<HiltBroadcastReceiver>unsafeCast(this));

Update: Now issue has been fixed in version 2.29.1-alpha. Also do not forget to upgrade hilt-android-gradle-plugin version to 2.29.1-alpha or latest. More info on releases .

Fix #1918: Support BroadcastReceiver with @AndroidEntryPoint transform. (ede018b)