I tried a small Android project with the newly released Dagger 2.10 with the dagger.android package.
Only the MainApplication
class should be injected by Dagger, nothing else has any dependencies yet:
build.gradle
dependencies {
...
// Dagger
compile 'com.google.dagger:dagger:2.10'
compile 'com.google.dagger:dagger-android:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
// Fix for incompatible library versions when adding dagger-android and having espresso-core:2.2.2 in tests
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'
}
MainApplication.java
public class MainApplication extends Application implements HasDispatchingActivityInjector {
@Inject DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerMainApplicationComponent.create();
// DaggerMainApplicationComponent.create().inject(this); does not work - no inject(...) method!
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
MainApplicationComponent.java
@Component(modules = AndroidInjectionModule.class)
public interface MainApplicationComponent {}
Auto-generated DaggerMainApplicationComponent.java:
public final class DaggerMainApplicationComponent implements MainApplicationComponent {
private DaggerMainApplicationComponent(Builder builder) {
assert builder != null;
}
public static Builder builder() {
return new Builder();
}
public static MainApplicationComponent create() {
return new Builder().build();
}
public static final class Builder {
private Builder() {}
public MainApplicationComponent build() {
return new DaggerMainApplicationComponent(this);
}
}
}
I am new to Dagger and there are lots of resources, but many are describing DI with an outdated Dagger library. What is wrong in my implementation?
I also found an auto-generated class named MainApplication_MembersInjector
... do I have to use this myself?