Dagger ObjectGraph plus()包含根模块中模块的模块不断返回No no-args构造函数

时间:2014-08-19 19:13:57

标签: android dependency-injection include square dagger

我有一个使用Dagger的Android应用。整个应用程序的某些部分我想为几个共享共同范围的活动添加范围的ObjectGraphs。以下模块位于根ObjectGraph

@Module(
    injects = {
            MyApplication.class,
    },
    complete = false,
    library = true)
public class BasicContextManagerModule {

    private Context applicationContext;

    public BasicContextManagerModule(Context applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Provides
    Context getApplicationContext() {
        return applicationContext;
    }
}

然后我尝试通过existingObjectGraph.plus(new FileManagerModule())添加以下模块;

@Module(
    injects = {
            MyListActivity.class,
            MyFileDetailActivity.class,
            MyFileInfoActivity.class,
    },
    includes = BasicContextManagerModule.class
)
public class FileManagerModule {

    @Provides
    FileManager provideFileManager(Context context) {
        return new FileManager(context);
    }
}

但结果是

java.lang.UnsupportedOperationException: No no-args constructor com.myapp.core.modules.BasicContextManagerModule$$ModuleAdapter

有人可以帮助我理解为什么加号不会允许这样做?我从匕首文档中读到,扩展了对象图,你可以使用includes和addsTo Modules。但我还没有实现这个目标。

1 个答案:

答案 0 :(得分:5)

includes表示模块将存在于同一个子图中,如果您不传递实例,Dagger将实例化它。

addsTo表示引用的模块应该在图表中(实际上在父图表中),但Dagger不会为您提供。

你想要的是addsTo

相关问题