Skip to content

Instantly share code, notes, and snippets.

@NightlyNexus
Last active August 6, 2017 16:49
Show Gist options
  • Save NightlyNexus/82c8138816c6b4db9afd7785f3396f47 to your computer and use it in GitHub Desktop.
Save NightlyNexus/82c8138816c6b4db9afd7785f3396f47 to your computer and use it in GitHub Desktop.
Does this Dagger subcomponent need an empty module in the parent component?
import dagger.BindsInstance;
import dagger.Component;
import dagger.Subcomponent;
import javax.inject.Inject;
final class App {
private final Sub.Builder subBuilder;
private final AppDependency appDependency;
@Inject App(AppDependency appDependency, Sub.Builder subBuilder) {
this.appDependency = appDependency;
this.subBuilder = subBuilder;
}
@Component(modules = AppComponent.Module.class) interface AppComponent {
App getApp();
@Component.Builder interface Builder {
@BindsInstance Builder appDependency(AppDependency appDependency);
AppComponent build();
}
// TODO: How can we elide this module whose only job is to declare the subcomponent?
@dagger.Module(subcomponents = Sub.class) abstract class Module {
}
}
@Subcomponent interface Sub {
SubDependency getSubDependency();
@Subcomponent.Builder interface Builder {
@BindsInstance Builder subDependency(SubDependency subDependency);
Sub build();
}
}
void run() {
Sub sub = subBuilder.subDependency(new SubDependency()).build();
System.out.println(appDependency);
System.out.println(sub.getSubDependency());
}
public static void main(String[] args) {
AppComponent appComponent = DaggerApp_AppComponent.builder().appDependency(new AppDependency())
.build();
App app = appComponent.getApp();
app.run();
}
static final class AppDependency {
@Override public String toString() {
return "AppDependency{}";
}
}
static final class SubDependency {
@Override public String toString() {
return "SubDependency{}";
}
}
}
@JonathanMerritt
Copy link

Not sure if it will help for you, due to my experience level.

But have you looked into ContributesAndroidInjector?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment