dagger-2

How to provide Context with Dagger2

半世苍凉 提交于 2019-12-04 04:46:23
问题 I am learning Android and I am following some guides for Retrofit2 with RxJava and Dagger2. Now I want to handle no internet connection case. I've found this answer, which seems to be elegant, but I do not understand how to apply it. I've got some NetworkModule , with OkHttpClient provider. I assume I need to create OkHttpClient.Builder with interceptor. So it should look something like this: ` @Provides @Singleton OkHttpClient provideOkHttpClient(Cache cache) { ConnectivityInterceptor ci =

Using Dagger2 with Lombok

倖福魔咒の 提交于 2019-12-04 04:28:29
Has anyone used Lombok 1.16 with Dagger2? My current code looks like: @AllArgsConstructor(onConstructor = @__(@Inject)) public class JuiceMaker { private final Apple apple; The error is: JuiceMaker cannot be provided without an @Inject constructor or from an @Provides-annotated method. Without the Lombok annotation this actually works, so: public class JuiceMaker { private final Apple apple; @Inject public JuiceMaker(Apple apple){ this.apple = apple } } works This is a copy paste version of my answer here : This is not a direct answer to the question, which seems to be solved, but acts as

dagger2 error “android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method”

梦想与她 提交于 2019-12-04 03:28:07
问题 I'm trying to implement dagger2 in my project, but I'm faced with an error " android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method ". Here's my code: App.java package com.poppmedia.wallpaperautomaton; import android.app.Application; import com.poppmedia.wallpaperautomaton.di.DaggerAppComponent; import dagger.android.AndroidInjector; import dagger.android.DaggerApplication; /** * The Android {@link Application}. */ public class App

Dagger 2 how to perform constructor injection

删除回忆录丶 提交于 2019-12-04 02:59:50
问题 I have a class public class DialogUtils { private Context context; @Inject public DialogUtils(Context context) { this.context = context; } } In my activity class i have did but i'm getting null pointer exception on dialogUtils instance. public class LoginActivity extends Activity{ @Inject DialogUtils dialogUtils; } I know how to inject dependency via module and component but not sure how to with construction injection. Any help is much appreciated. 回答1: If you don't retain the activity-level

How to resolve a circular dependency while still using Dagger2?

好久不见. 提交于 2019-12-04 02:08:51
I have two classes, Foo<T> and Bar , which depend on each other, as well as various other classes. I am using Dagger-2 for dependency injection, but if I naively add the circular dependency, Dagger hits a stack overflow at runtime. What's a good way to refactor the classes to fix this, while still using Dagger to inject all the other dependencies, and with minimal duplication and changes to existing calls? The easy way out is to use Lazy<T> on one side. Lazy<Foo> foo; @Inject Bar(Lazy<Foo> foo) { this.foo = foo; } // use foo.get(); when needed After an excessive amount of thought and talks

Dagger 2 Singleton Component Depend On Singleton

僤鯓⒐⒋嵵緔 提交于 2019-12-04 01:56:31
I've got a strange problem here, and I'm not quite sure why what I'm doing isn't allowed. I've got the following modules: @Module public final class AppModule { private Context mContext; @Provides @Singleton @AppContext public Context provideContext() { return mContext; } } @Module public final class NetModule { @Provides @Singleton public OkHttpClient provideOkHttp() { return new OkHttpClient.Builder().build(); } } For various reasons, I don't want to have these two modules in the same component (basically due to my project structure). So I tried to create the following components: @Singleton

Beside testing, why do we need Dagger 2?

徘徊边缘 提交于 2019-12-04 00:12:59
At this point, my understanding of Dependency Injection (DI) is only from this article . I'm interested to try, but I just need to clarify some things: Many refer DI as a tool to reduce boilerplate code. But according to that tutorial, the setup of Dagger 2 tends to create even more classes for configuration (module and component). I have not tried it, but by the looks of it, it isn't reducing the code, it is just splitting them so the main class can look tidier. Am I wrong on this one? Despite Dagger 2's claim that DI isn't just for testing, many are regarding its usage mainly for testing,

Component (unscoped) may not reference scoped bindings

血红的双手。 提交于 2019-12-03 22:25:21
I've recently started using dagger-2 with kotlin. Unfortunately I have ecnountered a problem with sumbcomponants and I have trouble understanding why I get this gradle error: ...NetComponent (unscoped) may not reference scoped bindings: @dagger.Subcomponent(modules = {com.example.kremik.cryptodroid.di.module.NetModule.class}) @Singleton @Provides com.example.kremik.cryptodroid.data.remote.CMCService com.example.kremik.cryptodroid.di.module.NetModule.providesCMCService(retrofit2.Retrofit) @Singleton @Provides retrofit2.Retrofit com.example.kremik.cryptodroid.di.module.NetModule.providesRetrofit

Cannot find symbol class “Generated” for Dagger 2

帅比萌擦擦* 提交于 2019-12-03 22:17:56
I just started doing dependency injection using Dagger 2 . When I spun up my modules , components and tried to build my application, gradle threw the error Error:(4, 24) error: cannot find symbol class Generated I dug into it and found that the error is in one of the classes Dagger generates to do DI . The particular class that's missing was javax.annotation.Generated and the line throwing the error is the line that anntotates a Dagger generated class as @Generated("dagger.internal.codegen.ComponentProcessor") This question helped in finding the solution which is to add the javax package as a

Dagger2: Unable to inject dependencies in WorkManager

…衆ロ難τιáo~ 提交于 2019-12-03 18:42:07
问题 So from what I read, Dagger doesn't have support for inject in Worker yet. But there are some workarounds as people suggest. I have tried to do it a number of ways following examples online but none of them work for me. When I don't try to inject anything into the Worker class, the code works fine, only that I can't do what I want because I need access to some DAOs and Services. If I use @Inject on those dependencies, the dependencies are either null or the worker never starts i.e the