我正在尝试使用 Dagger2 设置一个新项目,我以前使用过 Dagger2,但现在我正在尝试自己从头开始设置它。我正在从我参与的 Kotlin 项目中获取示例,但无法像现在在 Kotlin 中一样为 Java 设置它(或者我可能遗漏了一些东西)。
它只是一个组件、一个模块和一个应用程序。
组件
@Singleton
@Component(modules = {MainAppModule.class})
public interface AppComponent extends AndroidInjector<App> {
@Component.Builder
abstract class Builder implements AndroidInjector.Factory<App> {
public AppComponent create(App application) {
seedApplication(application);
return build();
}
@BindsInstance
abstract void seedApplication(App application);
abstract AppComponent build();
}
}
模块
@Module
abstract class MainAppModule {
@Binds
abstract public Application bindApplication(App application);
@ContributesAndroidInjector
abstract public MainActivity contributeActivityInjector();
}
*申请*
public class App extends DaggerApplication {
@Override
public AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().create(this);
}
}
此时我没有任何可以用@Inject
调用的类
我只是在构建时遇到错误:
error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.content.BroadcastReceiver>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.content.BroadcastReceiver>>> cannot be provided without an @Provides-annotated method.
public interface AppComponent extends AndroidInjector<App> {
^
当然不能在没有@Provides-annotated 方法的情况下提供。
似乎是问题所在,但我就是不知道如何解决。它在我的 kotlin 项目上运行良好,由其他人设置。
最佳答案
看来你不见了AndroidInjectionModule (或 AndroidSupportInjectionModule,如果您使用支持 fragment )安装在您的 AppComponent
上。
应该是这样的:
@Component(modules = {AndroidInjectionModule.class, MainAppModule.class})
关于java - Dagger2 "cannot be provided without an @Provides-annotated method."问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51113770/