G
G
gikengik2021-04-15 21:02:39
Java
gikengik, 2021-04-15 21:02:39

How to fix problem with Dagger2?

I use the Dagger2 library, when I try to rebuild, the compiler gives the error
"error: Members injection methods may only return the injected type or void." (in the getRetrofit method of the NetworkComponent interface)
It seems that in the ModuleNetwork module I do the Retrofit class injected, but the error does not disappear.
What should I do?

@Module
public class NetworkModule {
    private static final String BASE_URL = "https://api.coingecko.com/";
    @Provides
     public static Gson provideGson() {
        return new GsonBuilder()
                .setLenient()
                .create();
    }
    @Provides
    public Retrofit provideRetrofit(Gson gson) {
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
}


@Component (modules = NetworkModule.class)
public interface NetworkComponent {
    Retrofit getRetrofit(Gson gson);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-04-15
@gikengik

You slightly misunderstand the meaning of the methods in the component. These are provision methods, or getters, they only return something, but do not accept. Get the gson out of there, and everything will be fixed. But wangyu, that's not what you want to do. You obviously want to inject this retrofit somewhere, and not just get it from the component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question