-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Dagger Application injection NPE #748
Comments
Take a look at https://google.github.io/dagger/api/latest/dagger/android/DaggerApplication.html, which handles this scenario where sometimes a contentprovider.oncreate() is called before the application.oncreate() |
I can't see the link (404) for me. |
Also, getInstance() is only called on onReceiver() of broadcast or on startCommand() of service |
Ron probably got his markdown mixed up and meant DaggerApplication |
Thanks, so if I undestrood correctly, injecting my objects on a DaggerApplication class will prevent this injection before application.oncreate() right? |
I'm actually trying to do something about this,
When I notice that AndroidInjector only has, broadcast, service, activity, fragment or contentprovider. So, how should I proceed when overriding the method applicationInjector on application?
Since I'm only injecting this to access it from everywhere, activities, services and broadcasts. |
I'm stucked at the same point, i'm pretty new to Dagger 2 and there is no documentation on how to use the new |
As a side note, this NPE on injected instance happens a lot on services from startCommand() and on samsung devices such as Galaxy S8, S7 and S6
|
Github isn't the best forum for this - please use StackOverflow with the |
@luispereira Did you find any solution for your problem? Same here :/ |
Actually I found a work around for this which implies something like this on your application class:
Basically, if by any instance, the dagger app component builder returns null, it will force to initialise again. I know this is far from a perfect solution, but it works. PS: I had multiple reports of different projects which the application was started after services/brodcasts and even activities on Samsung devices. I'm still investigating the issue, but I seen that Samsung starts a content provider for every application, maybe triggering this kind of behaviour. Although, with the above solution this kind of NPE no longer happened |
Thank you very much, I will try it :) |
I am facing this issue as well and I have no idea how to work around it.
This happens only on Android 7.0, mainly on Samsung devices, some Huawai and Motorola devices and a few Xperia devices. I am unable to reproduce it since I do not have any of the affected devices at hand. It seems this is caused by the application not (yet) being set for the activity, meaning activity's onCreate is called before the activity is actually attached to the application. Here the affected Dagger code: dagger/java/dagger/android/support/DaggerAppCompatActivity.java Lines 42 to 45 in e8d7cd4
dagger/java/dagger/android/AndroidInjection.java Lines 43 to 52 in e8d7cd4
Anyone any suggestions? |
Oh this is probably the same problem as what Mortar fixes with
|
@Zhuinden is this what you're referring too: square/mortar@bf74e3e? Is there any reason to not just call |
No
…On Tue, Oct 17, 2017, 12:12 PM Ron Shapiro ***@***.***> wrote:
@Zhuinden <https://github.com/zhuinden> is this what you're referring
too: ***@***.***
<square/mortar@bf74e3e>
?
Is there any reason to not just call (Application)
activity.getApplicationContext() here then and not worry about the
null-check?
/cc @rjrjr <https://github.com/rjrjr> @loganj <https://github.com/loganj>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#748 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAEEEWPEtAeiU80PqYMRPwQN_rnusoFcks5stNHzgaJpZM4NjrJU>
.
|
Hi everyone, I rolled out an update a few hours ago with the change @Zhuinden proposed based on Dagger's DaggerAppCompatActivity and the AndroidInjection class:
Still getting the RuntimeException, now thrown by my code above. So looks like that either there is an application but the wrong one or even the application context is null. Cannot imagine how any of both scenarios would work actually... |
I tested using See also https://stackoverflow.com/questions/46784685/runtimeexception-with-dagger-2-on-android-7-0-and-samsung-devices where I posted some code where I tried to get more understanding of this error without any luck. |
See also #891 |
I solved the issue for myself by not using anymore the DaggerApplication but an Application independent solution described here: https://stackoverflow.com/a/46925736/8788319 Not sure if this is an option for Dagger 2 but I did not find another way to resolve the crashes under Android 7.0 otherwise. |
Hope you had added application class name in the Android Manifest file. Without it, the app will work but the dagger component will be null. |
I'm still pretty dumbfounded at what is happening here. I wish I had a better suggestion at what we should do. |
One interesting thing to point out is that Android doesn't always run any of the I've tested to see if Services and BroadcastReceivers still ran, and they do. This was causing multiple issues for me with Fabric, Firebase, and Dagger. I figured I'd share my findings here as I referenced this issue to try to fix my Dagger problem. I'm in the process of disabling this auto backup (setting |
@pablobaxter Any update? |
@camsteffen Thank you for reminding me! Also sorry... I never did update my result on this thread as promised. So disabling the backup worked for me. I am no longer seeing my Dagger crash, which in my case was a I filed a ticket about this with Firebase (firebase/firebase-android-sdk#1456), as this was the main library starting Services and BroadcastReceivers while the app was in this restricted mode. However, this is most likely an Android OS issue. It looks like whenever the app crashes in this restricted mode (in my case due to Firebase starting up), the next launch app (user initiated or otherwise) restarts the app in this restricted mode, which caused a crash, and so on. Hope this helps! |
@pablobaxter Thanks for the update! It looks like your firebase bug report is attracting some more needed attention so I will follow that, even though I don't use firebase myself. I wonder if disabling backup would prevent this issue in all cases. |
Closing this as I don't think there is much that can be done on the Dagger side. This feels like an Android bug that you can ever be called without the |
As an FYI, I did file a bug with the Android team regarding the backup agent. https://issuetracker.google.com/u/1/issues/160946170 |
@camsteffen if I understand the SO solution, it is just moving the component to a static variable? This would work for the production app, but keeping your DI graph in static state tends to cause problems for things like tests. |
That's never bothered anyone using Koin, which works exactly the same way. |
I'm trying to inject some objects on application, in order to access them whenever I want.
So, on my application I'm doing something like this:
However, according to google crash log of my production app, the application has a NullPointException when trying to access anything from getInstance(). On my application this happens on a service which is only called via a receiver. Additionally, this also happens on another receiver.
Here is my ApplicationComponent:
I tried many times to replicate, but so far without success, despite the google crash saying otherwise.
Am I doing something wrong here?
As far as I know, for services/receivers to be initialised in android, the application must first be initialised, so getInstance() should not return null right?
The text was updated successfully, but these errors were encountered: