Val
Val

Reputation: 4366

Dagger 2 does not generate subcomponent implementation

I've started to setup Dagger 2 and faced a strange issue that looks like a bug to me.

I have 1 main component and 2 subcomponents which I 'plus' in parent component. I use different scopes for each subcomponent. The problem is that I can easily do fields injection for the 1st subcomponent but I can't do the same for the second. The injected fields stay nulls.

Main component:

@Singleton
@Component(modules = { WalletSaverAppModule.class })
public interface MyAppComponent {
  TrackingComponent plus(TrackingModule module);

  DashboardComponent plus(DashboardModule module);
}

1st subcomponent (works well):

@PerActivity @Subcomponent(modules = { DashboardModule.class })
public interface DashboardComponent {
  void inject(MainActivity activity);
}

2nd subcomponent (fields injection -> null):

@PerService @Subcomponent(modules = { TrackingModule.class })
public interface TrackingComponent {
  void inject(IntentService context);
}

How I do fields injection for the 2nd subcomponent:

public class TrackingService extends IntentService {
  @Inject CallCase mCallCase;
  @Inject CallModelMapper mCallModelMapper;
  ...

@Override protected void onHandleIntent(Intent intent) {
((MyApp) getApplication()).getAppComponent().plus(new TrackingModule(this)).inject(this);
// ---> here the both fields are null
...

Objects that I'm injecting:

@Singleton public class CallCase {
  private CallRepository mCallRepository;

  @Inject public CallCase(final CallRepository userRepository) {
    mCallRepository = userRepository;
  }

  public Observable<Call> execute() {
    ...
  }
}

@Singleton public class CallModelMapper {
  @Inject CallModelMapper() {
  }

  public CallModel transform(@NonNull final Call callEntity) {
    ...
  }
}

Both objects have @Singleton scope (as their constructor fields). Could it be a scope conflict?

--- UPDATE ---

I've checked the class generated by Dagger2 (DaggerMyAppComponent) that I'm using in MyApp to build application component. I found the difference between implementations of 1st and 2nd components.

1st:

private final class DashboardComponentImpl implements DashboardComponent {
    private final DashboardModule dashboardModule;

    private Provider<DashboardMvp.Presenter> providesPresenterProvider;

    private MembersInjector<MainActivity> mainActivityMembersInjector;

    private DashboardComponentImpl(DashboardModule dashboardModule) {
      this.dashboardModule = Preconditions.checkNotNull(dashboardModule);
      initialize();
    }

    private void initialize() {...}

    @Override
    public void inject(MainActivity activity) {...}
  }

2nd:

private final class TrackingComponentImpl implements TrackingComponent {
    private final TrackingModule trackingModule;

    private TrackingComponentImpl(TrackingModule trackingModule) {
      this.trackingModule = Preconditions.checkNotNull(trackingModule);
      // ---> look, missing call initialize() <---
    }

    @Override
    public void inject(IntentService context) {...}
  }

Why Dagger 2 takes different the subcomponents that implemented in the same way? Only one difference I can see is the scope. I would appreciate any inputs about this problem.

Thanks in advance!

Upvotes: 0

Views: 1019

Answers (1)

Serhii Bosovskyi
Serhii Bosovskyi

Reputation: 46

In TrackingComponent why are you inject to IntentService. Maybe changing to TrackingService will help

void inject(TrackingService context);

Upvotes: 3

Related Questions