Questions tagged [dagger-2]

Dagger 2 is a dependency injection framework for Java and Android. It implements the full stack with generated code

Dagger 2 is a compile-time dependency injection framework for and .

It uses code generation and compile-time validation taking the approach started with Dagger 1 to it's ultimate conclusion. Dagger 2 eliminates all reflection and improves code clarity by removing the traditional ObjectGraph/Injector in favor of user-specified @Component interfaces. Dagger 2 implements the full stack with generated code.

Useful Links

3161 questions
26
votes
2 answers

Simplest way to create a Singleton w/ Dagger 2?

Do I need to setup Modules, Providers and Interfaces (Components)? Seems like quite a bit of overhead just to be able to inject a singleton. Can someone provide a simple singleton example using Dagger 2? (also show how one can set properties of the…
My House
  • 759
  • 2
  • 6
  • 18
26
votes
5 answers

Cannot find symbol class "Generated" for Dagger 2

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…
x-treme
  • 1,606
  • 2
  • 21
  • 39
26
votes
2 answers

Scopes in Dagger 2

I probably missed something, but I thought Scopes like @Singleton are used to define "scoped lifecycles". I use Dagger 2 in an Android app (but I don't think the problem is android related at all). I have 1 Module: @Module public class MailModule…
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
24
votes
3 answers

Hilt using in android library

I would like to try Hilt DI in the android library. It is a dependency on another project, with its own submodule. The very first problem I've encountered is the requirement of marking Application with @HiltAndroidApp. Now I do not have anything…
kolboc
  • 805
  • 1
  • 8
  • 22
24
votes
4 answers

Dagger2: Unable to inject dependencies in WorkManager

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…
varunkr
  • 5,364
  • 11
  • 50
  • 99
24
votes
2 answers

Difference between @Singleton & static @Provides in dagger2

May I know the difference between @Singleton VS static Provides in dagger2? @Provides static User currentUser(AuthManager authManager) { return authManager.currentUser(); } @Provides @Singleton User currentUser(AuthManager authManager) { …
Shawn Thye
  • 386
  • 3
  • 11
24
votes
3 answers

Can someone provide a good explanation of Dagger 2?

I'm really having a hard time understanding the Dagger 2 Dependency injection system. I understand the use of the @Inject annotation to tell Dagger we need to provide an instance of some type to here. But, I don't understand the various roles of the…
user47376
  • 2,263
  • 3
  • 21
  • 26
24
votes
2 answers

Java dependency injection: Dagger 1 vs Dagger 2, which is better?

What are the advantages of Dagger 2 over Dagger 1? So far I found (just) 2: Dagger 2 allows you to use code obfuscation with proguard Dagger 2 is faster (which is not much of an advantage when using it for android application but it is sure an…
Ognyan
  • 13,452
  • 5
  • 64
  • 82
24
votes
4 answers

Using Kotlin with Dagger

What's the best way to use Dagger to inject dependencies into classes, especially zero-arg constructor classes like Activities, with Dagger? Will Dagger 2 possibly bring improvements to the situation? Thanks in advance.
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
23
votes
3 answers

Dagger 2 cannot access Retrofit

I'm trying to provide an instance of Retrofit to my Repository using Dagger 2 (with Android module). Buy I'm facing the error: Error:cannot access Retrofit Other instances like Picasso was injected with success, I just have problems with…
fe_araujo_
  • 1,859
  • 1
  • 14
  • 15
23
votes
2 answers

CustomView dependency injection with dagger 2 (within activity scope)

My question is similar to this. So for instance, I have a LiveData implementation: public class CustomLiveData extends LiveData { @Inject public CustomLiveData(@ActivityContext Context context) { //.... } } that I…
sinek
  • 2,458
  • 3
  • 33
  • 55
23
votes
3 answers

How to resolve a circular dependency while still using Dagger2?

I have two classes, Foo 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…
23
votes
1 answer

Dagger 2: Provide same instance between multiple Component with same Scope on different library modules

I have a Core Android Library where I'm defining a CoreComponent ad using the @Singleton scope to inject instances of classes provided by a CoreModule. @Singleton @Component(modules = {CoreModule.class}) public interface CoreComponent { void…
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
23
votes
1 answer

How to create an object using constructor injection?

How would I create an instance of Dog with a component which provides Cat. public final class Dog { private final Cat mCat; public final static String TAG = "Dog"; @Inject public Dog(Cat cat) { mCat = cat; Log.e(TAG,…
Paradiesstaub
  • 2,590
  • 2
  • 18
  • 28
23
votes
2 answers

Dagger 2 and android Studio: working but can't see generated classes

I'm trying to use Dagger 2 in an Android Studio Project. I've used the CoffeeMaker example. I've managed to make the app build and working however: - I don't success in seeing the generated code. - If I debug, I can't see it neither. - Moreover…
Mateu
  • 2,636
  • 6
  • 36
  • 54