google / dagger

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

Hilt: Injection not working in BroadcastReceiver #4379

Open BurningDroid opened 3 months ago

BurningDroid commented 3 months ago

I have an app that has been running for 3 years. I have implemented and am using BroadcastReceiver.

I recently updated the hilt version from 2.45.0 to 2.51.0. After the update, Firebase Crashlytics started showing an explosion of new crashes. These are completely new crashes that were never there before.

@AndroidEntryPoint
class AlarmReceiver : BroadcastReceiver() {

    @Inject
    lateinit var notificationManager: NotificationManagerCompat
    @Inject
    lateinit var getUsers: GetUsers

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

        getUsers.get() // *** CRASH HERE ***

        // do something
    }
}

The crash is:

Fatal Exception: kotlin.UninitializedPropertyAccessException
lateinit property getUsers has not been initialized

Nothing has changed in the hilt settings. Only the version has changed, and the AlarmReceiver was working fine before. What could be the bug in 2.51.0?

bcorso commented 3 months ago

lateinit property getPortfolios has not been initialized

What is this property? Is this something in your code (I don't see it in the AlarmReceiver you posted)? Is it an @Inject property?

BurningDroid commented 3 months ago

lateinit property getPortfolios has not been initialized

What is this property? Is this something in your code (I don't see it in the AlarmReceiver you posted)? Is it an @Inject property?

Oh sorry, I updated the error log

bcorso commented 3 months ago

Hmm, this suggests that maybe the bytecode injection isn't working properly.

Does it work if you extend the hilt class manually (see changes below)?

-@AndroidEntryPoint
+@AndroidEntryPoint(BroadcastReceiver::class)
-class AlarmReceiver : BroadcastReceiver() { ... }
+class AlarmReceiver : Hilt_BroadcastReceiver() { ... }
tdnghia98 commented 2 months ago

Same here, I also have this issue

CodingWithTashi commented 2 months ago

I had the same issue but in my case, I was using

 // Dagger Hilt
    implementation 'com.google.dagger:hilt-android:2.52'
    kapt 'com.google.dagger:hilt-compiler:2.52'

Issue was due to class path version in project level build.gradle file classpath 'com.google.dagger:hilt-android-gradle-plugin:44' update to classpath 'com.google.dagger:hilt-android-gradle-plugin:2.52' make sure all are in same version i.e 2.52

orangeboyChen commented 1 month ago

Same here, I have the same issue in 2.49.

vincent-paing commented 1 month ago

I'm on 2.52 and can confirm the crash.

bcorso commented 4 weeks ago

@vincent-paing can you try the suggestion in https://github.com/google/dagger/issues/4379#issuecomment-2274801756.

If that works that will also help us narrow down if the issue is related to the plugin.

furkanayaz commented 2 weeks ago

You can update hilt and hilt compiler version to 2.51. I updated to this version and worked for me.

Misaka19327 commented 1 day ago

I have tried both 2.52 and 2.51 to inject in BroadcastReceiver, and can confirm the crash. (Inject with @ApplicationContext)