nasch nasch - 1 year ago 93
Android Question

Android Dagger 2 compile errors

I'm getting compile errors when attempting to create a second Dagger 2 Component that injects something into the same classes as the first. Is that not allowed? I haven't found any documentation that says it isn't.

Module 1:

@Module
public class NavModule {

Context context;

public NavModule(Context context){
this.context = context;
}

@Provides @Named("nav")
public List<NAV_ACTIONS> provideNavActions() {
// Do some stuff
}
}


Component 1:

@Component(modules = {NavModule.class})
public interface NavComponent {

void inject(MainActivity activity);
void inject(AbstractHomeFragment fragment);
}


Module 2:

@Module
public class OtherModule {

Context context;

public OtherModule(Context context){
this.context = context;
}

@Provides
public Object provideSomething(){
return null;
}
}


At this point everything still compiles fine. When I add Component 2, it breaks:

@Component(modules = {OtherModule.class})
public interface OtherComponent {

void inject(MainActivity activity);
void inject(AbstractHomeFragment fragment);
}


Here are the errors:

Error:(14, 10) error: @javax.inject.Named("nav") java.util.List<NAV_ACTIONS> cannot be provided without an @Provides- or @Produces-annotated method.
@javax.inject.Named("nav") java.util.List<NAV_ACTIONS> is injected at
com.company.common.MainActivity.navActions
com.company.common.MainActivity is injected at
com.company.common.dependency.OtherComponent.inject(activity)

Error:(15, 10) error: @javax.inject.Named("home") java.util.List<com.company.common.NAV_ACTIONS> cannot be provided without an @Provides- or @Produces-annotated method.
@javax.inject.Named("home") java.util.List<com.company.common.NAV_ACTIONS> is injected at
com.company.common.views.AbstractHomeFragment.homeActions
com.company.common.views.AbstractHomeFragment is injected at
com.company.common.dependency.OtherComponent.inject(fragment)

E:\Development\Repositories\PropertyForce\PropertyForce_Android\app\src\main\java\com\company\common\MainActivity.java
Error:(50, 47) error: cannot find symbol class DaggerNavComponent
Error:(50, 47) error: cannot find symbol class DaggerNavComponent

E:\Development\Repositories\PropertyForce\PropertyForce_Android\app\src\main\java\com\company\common\views\AbstractHomeFragment.java
Error:(29, 47) error: cannot find symbol class DaggerNavComponent
Error:(29, 47) error: cannot find symbol class DaggerNavComponent


Here's the Dagger related stuff from
MainActivity
. The editor shows no errors.

import com.company.common.dependency.DaggerNavComponent;
import com.company.common.dependency.NavComponent;
import com.company.common.dependency.NavModule;
// ...
@Inject @Named("nav") List<NAV_ACTIONS> navActions;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

NavComponent navComponent = DaggerNavComponent.builder().navModule(new NavModule(this)).build();
navComponent.inject(this);
// ...


AbstractHomeFragment
is doing very similar things.

Answer Source

You need to add NavModule and OtherModule together in the injected modules list in single component which you have declared in your Application:

@Component(modules = {OtherModule.class, NavModule.class})
public interface OtherComponent {

    void inject(MainActivity activity);
    void inject(AbstractHomeFragment fragment);
}

Hope this helps :)