如果没有@Provides注释的方法,则无法提供Dagger / MissingBinding

时间:2019-07-07 14:39:30

标签: android dagger-2 dagger

从dagger2 2.16版本构建到2.23.2版本时出现异常。 它已启动并在2.16中运行。我没有修改任何代码。升级到2.23.2后,它无法构建。 我不确定是什么问题,所以我要求所有人提供帮助。 谢谢。

模块

@Module
public class BaseModule {

    private ConfigBuilder configBuilder;

    public BaseModule(@Nullable ConfigBuilder configBuilder) {
        this.configBuilder = configBuilder;
    }

    @Singleton
    @Provides
    public Gson provideGson() {
        GsonBuilder builder = new GsonBuilder();
        if (configBuilder != null) {
            configBuilder.buildGson(builder);
        }
        return builder.create();
    }

    @Singleton
    @Provides
    public OkHttpClient provideOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (configBuilder != null) {
            configBuilder.buildOkHttp(builder);
        }
        builder.addInterceptor(new EncryptInterceptor());
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(loggingInterceptor);
        }
        return builder.build();
    }

    @Singleton
    @Provides
    public Retrofit provideRetrofit(OkHttpClient okHttpClient, Gson gson) {
        Retrofit.Builder builder = new Retrofit.Builder()
                .client(okHttpClient);
        if (configBuilder != null) {
            configBuilder.buildRetrofit(builder);
        }
        builder.addConverterFactory(GsonWrapperConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        return builder.build();
    }

    @Singleton
    @Provides
    public SharedPreferencesPlus provideSharedPreferencesPlus(Application application, Gson gson) {
        return SharedPreferencesPlus.createDefault(application, gson);
    }
@Module
interface ActivityModule {

    @ActivityScope
    @ContributesAndroidInjector
    MainActivity main();
@Module
public interface ViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(VMMain.class)
    ViewModel main(VMMain vm);

组件

@Singleton
@Component(modules = {BaseModule.class, AndroidSupportInjectionModule.class})
public interface BaseComponent {

    Application provideApplication();

    SharedPreferencesPlus provideSharedPreferencesPlus();

    Gson provideGson();

    OkHttpClient provideOkHttpClient();

    Retrofit provideRetrofit();

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        Builder AppModule(BaseModule baseModule);

        BaseComponent build();
    }
@ApplicationScope
@Component(modules = {
        ActivityModule.class,
        ViewModelModule.class,
        DataModule.class}, dependencies = BaseComponent.class)
public interface AppComponent
{
    void inject(AppContext application);
}

例外: [Dagger / MissingBinding]如果没有@Provides注释的方法,则无法提供java.util.Map >>。

java.util.Map >>被注入dagger.android.DispatchingAndroidInjector(…,jectorFactoriesWithStringKeys)

dagger.android.DispatchingAndroidInjector被注入 org.pp.va.video.app.AppContext.serviceInjector

org.pp.va.video.app.AppContext中注入 org.pp.va.video.di.AppComponent.inject(org.pp.va.video.app.AppContext)

也可以在以下位置提出要求: dagger.android.DispatchingAndroidInjector(…,jectorFactoriesWithStringKeys)

1 个答案:

答案 0 :(得分:0)

我发现从2.16到2.17开始出现问题。 由于在AppComponent中引入了AndroidSupportInjectionModule,我观察到了该错误。我曾经在BaseComponent中介绍AndroidSupportInjectionModule,然后在AppComponent依赖的基础上介绍BaseComponent。现在它将无法正常工作。我从BaseComponent中删除了对AndroidSupportInjectionModule的介绍,并在AppComponent中介绍了AndroidSupportInjectionModule,这解决了我的问题。

我当前的代码如下:

@ApplicationScope
@Component(modules = {
        ActivityModule.class,
        ViewModelModule.class,
        DataModule.class, AndroidSupportInjectionModule.class}, dependencies = BaseComponent.class)
public interface AppComponent
@Singleton
@Component(modules = {BaseModule.class})
public interface BaseComponent {