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
189
votes
20 answers

How to get Context in Android MVVM ViewModel

I am trying to implement MVVM pattern in my android app. I have read that ViewModels should contain no android specific code (to make testing easier), however I need to use context for various things (getting resources from xml, initializing…
Vincent Williams
  • 2,836
  • 3
  • 17
  • 22
150
votes
4 answers

Dagger 2 subcomponents vs component dependencies

Dagger 1's plus() method is something I used quite often in previous applications, so I understand situations where you might want to have a subcomponent with full access to the parent graphs bindings. In what situation would it be beneficial to use…
Bradley Campbell
  • 9,298
  • 6
  • 37
  • 47
142
votes
1 answer

What determines the lifecycle of a component (object graph) in Dagger 2?

I'm trying to wrap my head around scopes in Dagger 2, specifically the lifecycle of scoped graphs. How do you create a component that will be cleaned up when you leave the scope. In the case of an Android application, using Dagger 1.x you generally…
Enrico
  • 10,377
  • 8
  • 44
  • 55
132
votes
22 answers

Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

I am getting the following error while building the project. haven't used CoordinatorLayout in this project. just added as a dependency in build.gradle : I am using Android Studio 3.2 Canary 4. LogCat AGPBI: {"kind":"error","text":"Program type…
Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27
105
votes
2 answers

How to set up DAGGER dependency injection from scratch in Android project?

How to use Dagger? How to configure Dagger to work in my Android project? I'd like to use Dagger in my Android project, but I find it confusing. EDIT: Dagger2 is also out since 2015 04 15, and it's even more confusing! [This question is a "stub" on…
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
91
votes
8 answers

Set dynamic base url using Retrofit 2.0 and Dagger 2

I'm trying to perform a login action using Retrofit 2.0 using Dagger 2 Here's how I set up Retrofit dependency @Provides @Singleton Retrofit provideRetrofit(Gson gson, OkHttpClient client) { Retrofit retrofit = new Retrofit.Builder() …
Renjith
  • 3,457
  • 5
  • 46
  • 67
88
votes
5 answers

Dagger- Should we create each component and module for each Activity/ Fragment

I've been working with dagger2 for a while. And I got confused wether to create an own component/module for each Activity/ Fragment. Please help me clarify this: For example, We have an app, and the app has about 50 screens. We will implement the…
Mr Mike
  • 899
  • 1
  • 7
  • 7
86
votes
1 answer

Why use/develop Guice, when You have Spring and Dagger?

To my knowledge, Dagger does generate code, while Guice and Spring rely on runtime processing, thus Dagger works faster, but requires more work on programmer side. Because of performance edge it's good for mobile (Android) development. However, when…
spam
  • 1,853
  • 2
  • 13
  • 33
85
votes
5 answers

Dagger 2.2 component builder module method deprecated

I started using dagger 2.2 and the module methods in the Component builder are deprecated. This is my Application component : @Component(modules = ApplicationModule.class) public interface ApplicationComponent { void inject(Application…
user1940676
  • 4,348
  • 9
  • 44
  • 73
79
votes
32 answers

Error:Execution failed for task ':app:kaptDebugKotlin'

I'm new to using Kotlin and trying to set it up with Dagger2, I've seen some few examples but none of them seem to work for me. I keep getting this Error:Execution failed for task ':app:kaptDebugKotlin'. Internal compiler error. See log for more…
leggo
  • 807
  • 1
  • 6
  • 9
76
votes
8 answers

Dagger not generating components for /test class

I am following the guide here: https://github.com/ecgreb/dagger-2-testing-demo I have the following setup in my app/src/main (the injection and @Provides code omitted): public class FlingyApplication extends Application { @Singleton …
jyanks
  • 2,356
  • 1
  • 19
  • 36
73
votes
19 answers

ERROR : error.NonExistentClass Kotlin In multi module Dagger project

I'm using Dagger 2 and Kotlin for Android development. My project is also a multi-module project. My settings.gradle file is like this: include :app include :lib I'm also maintaining the lib module. In the Dagger Files (for example in the…
72
votes
5 answers

Dagger 2: Injecting user inputted parameter into object

Say I have a class Util that takes in a object - an instance of class Validator. Since I want to avoid instantiating the Validator class within Util, I pass it in via a constructor: public class Util { @Inject public Util(Validator…
Jay Sidri
  • 6,271
  • 3
  • 43
  • 62
71
votes
4 answers

What is the use case for @Binds vs @Provides annotation in Dagger2

I am not certain on the purpose for Dagger2's @Bind annotation. From what i have read online im still not clear but here is an example: @Module public abstract class HomeModule { @Binds public abstract HomePresenter…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
71
votes
5 answers

Dagger: IllegalArgumentException: No injector factory bound for Class

I am new to Dagger 2. I have 2 Activities, I want to use injected ViewModel for both. Here is my ViewModuleFactory : @Singleton public class ProductViewModelFactory implements ViewModelProvider.Factory { private final Map
dev_android
  • 8,698
  • 22
  • 91
  • 148
1
2 3
99 100