Android Dagger 2 编译错误

Android Dagger 2 compile errors

我在尝试创建第二个 Dagger 2 组件时遇到编译错误,该组件将某些内容注入到与第一个相同的 类 中。那是不允许的吗?我还没有找到任何说明它不是的文档。

模块 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
    }
}

组件 1:

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

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

模块 2:

@Module
public class OtherModule {

    Context context;

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

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

此时一切仍然可以正常编译。当我添加组件 2 时,它中断了:

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

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

错误如下:

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

这是 MainActivity 中与 Dagger 相关的内容。编辑器没有显示错误。

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 正在做非常相似的事情。

您需要在单个 component[=22= 中的注入模块列表中一起添加 NavModuleOtherModule ] 您已在 Application:

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

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

编辑 -- 您可以有多个组件,但您需要在 Application.class 文件中初始化它们。我建议在这里单独使用,因为你在同一个 类.

中注入了 2 个不同的模块

希望对您有所帮助:)