我有以下简单的模块:
@Module
public class ApplicationModule {
private CustomApplication customApplication;
public ApplicationModule(CustomApplication customApplication) {
this.customApplication = customApplication;
}
@Provides @Singleton CustomApplication provideCustomApplication() {
return this.customApplication;
}
@Provides @Singleton @ForApplication Context provideApplicationContext() {
return this.customApplication;
}
}
以及相应的简单组件:
@Singleton
@Component(
modules = ApplicationModule.class
)
public interface ApplicationComponent {
CustomApplication getCustomApplication();
Context getApplicationContext();
}
我在这里创建组件:
public class CustomApplication extends Application {
...
private ApplicationComponent component;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
@Override
public void onCreate() {
super.onCreate();
component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
它在编译时抛出此错误:Error:(22, 13) error: android.content.Context cannot be provided without an @Provides-annotated method
,但如您所见,它带有注释@Provides
。
这真的很奇怪,因为当我取消限定符注释时问题就消失了。
以防万一,这是我的@ForApplication
限定词:
@Qualifier @Retention(RUNTIME)
public @interface ForApplication {
}
这实际上是一个教科书 Dagger2 示例。我究竟做错了什么?