Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hilt: Injection not working in BroadcastReceiver #4379

Open
BurningDroid opened this issue Aug 6, 2024 · 9 comments
Open

Hilt: Injection not working in BroadcastReceiver #4379

BurningDroid opened this issue Aug 6, 2024 · 9 comments

Comments

@BurningDroid
Copy link

BurningDroid commented Aug 6, 2024

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
Copy link

bcorso commented Aug 7, 2024

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
Copy link
Author

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
Copy link

bcorso commented Aug 8, 2024

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
Copy link

Same here, I also have this issue

@CodingWithTashi
Copy link

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
Copy link

Same here, I have the same issue in 2.49.

@vincent-paing
Copy link

I'm on 2.52 and can confirm the crash.

@bcorso
Copy link

bcorso commented Oct 29, 2024

@vincent-paing can you try the suggestion in #4379 (comment).

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

@furkanayaz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants