1

I am using Android Dagger2 but I am getting the error below. My AppModule class is:

@Module
public class AppModule {

RetrofitExample retrofitExample;

AppModule(RetrofitExample retrofitExample) {

    this.retrofitExample = retrofitExample;

}

@Singleton
@Provides
RetrofitExample provideApplication() {
    return retrofitExample;
}
}

My API module class

@Module
class ApiModule {

String BaseUrl;

private  MainActivity  mainActivity;

ApiModule(String baseUrl) {
    this.BaseUrl = baseUrl;
}


public ApiModule(MainActivity downloadFileView) {
    this.mainActivity = downloadFileView;
}
@Provides
@Singleton
Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return gsonBuilder.create();
}

@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache) {
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.cache(cache);
    return client.build();
}

@Singleton
@Provides
Retrofit providesRetrofit(Gson gson, OkHttpClient okHttpClient) {
    return new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(BaseUrl)
            .client(okHttpClient)
            .build();
}

}

My API component class.

void inject(MainActivity activity);

Here is my application class

private static RetrofitExample mInstance;

private ApiComponent mApiComponent;

@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;

    mApiComponent = DaggerApiComponent.builder()
            .appModule(new AppModule(this))
            .apiModule(new ApiModule("https://simplifiedcoding.net/demos/"))
            .build();
}

public static synchronized RetrofitExample getInstance() { return mInstance;    }
ApiComponent getApiComponent()
{
    return mApiComponent;
}

I am getting the following error

okhttp3.Cache cannot be provided without an @Inject constructor or from an 
@Provides-annotated method.
okhttp3.Cache is injected at
net.simplifiedlearning.retrofitexample.ApiModule.provideOkhttpClient(cache)
okhttp3.OkHttpClient is injected at
net.simplifiedlearning.retrofitexample.ApiModule.providesRetrofit(…, 
okHttpClient)
retrofit2.Retrofit is injected at
net.simplifiedlearning.retrofitexample.MainActivity.retrofit
net.simplifiedlearning.retrofitexample.MainActivity is injected at
net.simplifiedlearning.retrofitexample.ApiComponent.inject(activity)
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Diya Bhat
  • 235
  • 3
  • 12

1 Answers1

5
okhttp3.Cache cannot be provided without an @Inject constructor or from an 
@Provides-annotated method.

You need

@Provides
@Singleton
Cache provideOkHttpCache(Application application) { 
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    Cache cache = new Cache(application.getCacheDir(), cacheSize);
    return cache;
}

in your ApiModule. Check NetModule which is similar to yours at https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @Raghunandan can i have your email id? – Pie Dec 07 '18 at 17:14
  • Can you please look into this problem I could not solve this https://stackoverflow.com/questions/56513672/errorunable-to-resolve-dependency-for-appdebugandroidtest-compileclasspath @Raghunandan – Pie Jun 09 '19 at 14:32