我对Dagger 2库来说还很陌生,我已经在线检查了一些指南,并且试图创建一个简单的应用程序以查看是否正确。问题是绑定类不是由我的代码生成的,我也不知道为什么。
我的gradle文件中有这个文件:
implementation 'com.google.dagger:dagger:2.20'
annotationProcessor 'com.google.dagger:dagger-compiler:2.20'
implementation 'com.google.dagger:dagger-android-support:2.20'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
androidTestAnnotationProcessor 'com.google.dagger:dagger-compiler:2.20'
androidTestAnnotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
这是我的AppComponent类:
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
ActivityBuildersModule.class,
AppModule.class,
NetworkModule.class})
interface AppComponent extends AndroidInjector<App> {
@Component.Builder
abstract class Builder extends AndroidInjector.Builder<App> {}
}
我有这个ActivityBuildersInterface:
@Module
public interface ActivityBuildersModule {
@ContributesAndroidInjector(modules = {FragmentBuildersModule.class})
MainActivity contributeMainActivity();
}
AppMudule看起来像这样:
@Module(includes = ViewModelModule.class)
public class AppModule {
@Provides
@Singleton
Api provideApi(Retrofit retrofit) {
return retrofit.create(Api.class);
}
@Provides
@Singleton
OutboundFlightsRepository provideOutboundFlightsRepository(Api api) {
return new OutboundFlightsRepository(api);
}
@Provides
@Singleton
BaseScheduler provideSchedulerProvider() {
return new SchedulerProvider();
}
}
FragmentBuildersModule:
@Module
public interface FragmentBuildersModule {
@ContributesAndroidInjector
OutboundFlightsFragment contributeOutboundFlightsFragment();
@ContributesAndroidInjector
InboundFlightsFragment contributeInboundFlightsFragment();
}
这是我的ViewModelModule:
@Module
public interface ViewModelModule {
@Binds
ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(OutboundFlightsViewModel.class)
ViewModel bindOutboundFlightsViewModel(OutboundFlightsViewModel viewModel);
}
我的ViewModelFactory是这样的:
@Singleton
public class ViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
@Inject
ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
@SuppressWarnings("unchecked")
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
Provider<? extends ViewModel> creator = creators.get(modelClass);
if (creator == null) {
for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) {
if (modelClass.isAssignableFrom(entry.getKey())) {
creator = entry.getValue();
break;
}
}
}
if (creator == null) {
throw new IllegalArgumentException("unknown model class " + modelClass);
}
try {
return (T) creator.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
我的App类:
public class App extends DaggerApplication {
@Override
protected AndroidInjector<? extends App> applicationInjector() {
return DaggerAppComponent.builder().create(this);
}
}
即使我具有上面的所有代码,当我尝试访问绑定类时,它们也不存在。我试图重新清理,重建,导入项目,但没有任何效果。我想念什么吗?