question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AndroidInjection doesn't allow to inject a super class

See original GitHub issue

I discovered this issue playing with AndroidAnnotations but it should be the same for every scenario where a Fragment extends another Fragment and the base one use AndroindInjection.inject.

The detailed scenario:

MyFragment use AndroidInjection to inject the presenter. Following the Subcomponent, module and Fragment:

@Subcomponent
public interface MyFragmentSubComponent extends AndroidInjector<MyFragment> {

  @Subcomponent.Builder
  abstract class Builder extends AndroidInjector.Builder<MyFragment> {
  }
}
@Module(subcomponents = { MyFragmentSubComponent.class })
abstract class FragmentModule {

  @Binds
  @IntoMap
  @FragmentKey(MyFragment.class)
  abstract AndroidInjector.Factory<? extends Fragment> bindMyFragmentInjectorFactory(
      MyFragmentComponent.Builder builder);
}
@EFragment(R.layout.screen_my_fragment)
public class RMyFragment extends BaseFragment implements RecipeBrowserPageView {
 @Inject
  MyPresenter presenter;

  @Override
  public void onAttach(Context context) {
    AndroidSupportInjection.inject(this);
    super.onAttach(context);
  }
}

``

AndroidAnnotations will generate the class MyFragment_ extending MyFragment. If I try to use MyFragment_ this error will be raised: Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class<com.package.MyFragment_>. Injector factories were bound for supertypes of com.package.MyFragment_: [com.package.MyFragment]. Did you mean to bind an injector factory for the subtype?

Because obviously the subcomponent is for the base MyFragment.

I’d like to try adding a class parameter to AndroidInjector.inject, and force to look for the correct injector based on the class parameter, and inject the instance parameter. It seems possible to you?

Could anyone suggest a different approach or a solution for this problem?

Thanks very much.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

25reactions
1amGr00tcommented, Mar 29, 2018

I struggle with the same problem. I want to write the Dagger Code only once for my BaseActivity, so all other activities inherits from this one and could easily be injected. But as I get the Dagger 2 concept right that means, I have to write a SubComponent, a Module and an injector factory for all my 10 Activities. In what way should that kind of DI makes developers live easier?!

4reactions
skhazcommented, May 15, 2017

Hi @LuigiPapino you said that your demo works well. I cloned your repository and had the same issue, do you mind to check if some changes was not pushed to your demo?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where to inject AndroidInjection in custom/util classes using ...
inject (this) line somewhere. but it does not have any onCreate or onAttach function. So how to get the dependency accessible in my...
Read more >
Dependency Injection with Dagger 2 - CodePath Cliffnotes
Android Studio by default will not allow you to navigate to generated Dagger 2 code as legitimate classes because they are not normally...
Read more >
Dependency injection with Hilt | Android Developers
These components can receive dependencies from their respective parent classes as described in Component hierarchy. To obtain dependencies from a component, use ...
Read more >
How To Inject ViewModel With Dagger & What Might Go Wrong
It doesn't allow us to pass any arguments to VM that aren't available from ... Add dependency and remove ViewModel superclass from our...
Read more >
Using Dagger 2 for dependency injection in Android - Tutorial
Therefore it is not allowed to use private fields for field injection. ... The base pattern for the generated class is that Dagger...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found