Search code examples
androiddependency-injectionincludesquaredagger

Dagger ObjectGraph plus() Modules that include modules in the root module keep returning No no-args constructor


I have an Android app that uses Dagger. There are certain sections of the entire app that I want to add scoped ObjectGraphs for several activities that share a common scope. The following module is in the root 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;
    }
}

Then I try to add the following Module through 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);
    }
}

But the result is

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

Can someone help me understand why the plus won't allow this? I read from the dagger documentation that plus extends the object graph and you can have includes and addsTo Modules. But I haven't been able to achieve this.


Solution

  • includes means the module will live in the same subgraph, and Dagger will instantiate it if you don't pass an instance.

    addsTo means the referenced module is expected to be in the graph (actually in a parent graph) but Dagger won't provide it for you.

    What you want is addsTo.