Android Dagger 2编译错误

时间:2016-08-27 16:16:39

标签: android dagger-2

我在尝试创建第二个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正在做类似的事情。

1 个答案:

答案 0 :(得分:4)

您需要在应用中声明的单个组件中的注入模块列表中一起添加 NavModule OtherModule

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

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

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

希望这会有所帮助:)