okhttp3.Cache cannot be provided without an @Inject constructor or from an @Provides-annotated method
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)
android dependency-injection dagger-2 android-mvp
add a comment |
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)
android dependency-injection dagger-2 android-mvp
add a comment |
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)
android dependency-injection dagger-2 android-mvp
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)
android dependency-injection dagger-2 android-mvp
android dependency-injection dagger-2 android-mvp
edited Nov 16 '18 at 6:30
Raghunandan
119k19193236
119k19193236
asked Nov 16 '18 at 6:20
Diya BhatDiya Bhat
11416
11416
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332467%2fokhttp3-cache-cannot-be-provided-without-an-inject-constructor-or-from-an-prov%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
add a comment |
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
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
add a comment |
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
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
answered Nov 16 '18 at 6:35
RaghunandanRaghunandan
119k19193236
119k19193236
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
add a comment |
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
Thanks, Its working now.
– Diya Bhat
Nov 16 '18 at 8:59
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
@Raghunandan can i have your email id?
– Pie
Dec 7 '18 at 17:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332467%2fokhttp3-cache-cannot-be-provided-without-an-inject-constructor-or-from-an-prov%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown