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

dagger-android crash in DaggerApplication #202

Open
realdadfish opened this issue Sep 8, 2020 · 5 comments
Open

dagger-android crash in DaggerApplication #202

realdadfish opened this issue Sep 8, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@realdadfish
Copy link

realdadfish commented Sep 8, 2020

  • Have your main application class derive from dagger.android.DaggerApplication
  • Implement applicationInjector() to return the AndroidInjector
  • Partial reflection setup
  • Dagger: 2.28.3, Delect 0.3.0 (Dagger-Reflect-latest)
java.lang.RuntimeException: Unable to create application my.application.MyApplication: java.lang.IllegalStateException: The AndroidInjector returned from applicationInjector() did not inject the DaggerApplication
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6715)
      at android.app.ActivityThread.access$1300(ActivityThread.java:226)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1898)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      at android.os.Looper.loop(Looper.java:214)
      at android.app.ActivityThread.main(ActivityThread.java:7615)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.IllegalStateException: The AndroidInjector returned from applicationInjector() did not inject the DaggerApplication
      at dagger.android.DaggerApplication.injectIfNecessary(DaggerApplication.java:65)
      at dagger.android.DaggerApplication.onCreate(DaggerApplication.java:38)
      at my.application.MyApplication.onCreate(MyApplication.kt:46)
      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1190)
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6710)
      ... 8 more
@JakeWharton
Copy link
Owner

Presumably because we don't support injecting a MembersInjector.

@JakeWharton JakeWharton added the bug Something isn't working label Sep 8, 2020
@realdadfish
Copy link
Author

Issue is that AndroidInjector has a void inject(T instance); and therefor does not enforce a specific void inject(MyApplication instance) method in the application component (which derives from said interface). dagger-reflect now reads this method's argument as Object and can't find the factory that provides injectees for this type.

I refactored the code to not use DaggerApplication and replaced the usage of AndroidInjector by a specific inject(...) method and that made this issue go away.

@JakeWharton
Copy link
Owner

While that's true, this still should work in reflection since we don't actually need to resolve that T. The implementation injects a Map<Class<?>, AndroidInjector<?>> which the generated module binds into using your application class as the key.

So I'm not exactly sure why this doesn't work already today. But we should be able to make it work.

@JakeWharton JakeWharton reopened this Sep 9, 2020
@JakeWharton
Copy link
Owner

This is a problem with generic handling. We don't correctly project T to be your application subtype. There's a few other issues for this.

@catellie
Copy link

I had the same problem and found a workaround that is somewhat simpler:
In my case there is also an XxxInternalApplication which created the injector, so I just added:

@Override void inject(@NotNull XxxInternalApplication instance);

... to the InternalApplicationComponent and then in the XxxInternalApplication applicationInjector() method I added:

mInternalApplicationComponent.inject(this);

... before the return. Hope this helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants