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
50
votes
6 answers

Dagger 2 error: dependency "cannot be provided without an @Inject constructor" while it actually annotated with @Inject

I've started using Dagger 2 and faced strange issue that looks like a bug to me. I have 3 modules, that are composed into one subcomponent, which in turn extends/pluses higher level component. Subcomponent is pretty simple: just combination of…
dominus
  • 1,082
  • 1
  • 11
  • 14
50
votes
2 answers

Dagger2 Custom Scopes : How do custom-scopes (@ActivityScope) actually work?

I am reading the source code for Dagger2 Component Scopes Test on GitHub, and I've seen a "custom scope" defined for activities called @ActivityScope, but I've seen it in other projects including the 4-module CleanArchitecture that has its…
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
49
votes
5 answers

Dagger2 : How to use @Provides and @Binds in same module

I'm using the new Dagger2 (ver 2.11) and I'm using the new features like AndroidInjector, and ContributesAndroidInjector. I have an activity subcomponent, @Module abstract class ActivityBuilderModule { …
user4260260
49
votes
1 answer

Dagger 2 multibindings with Kotlin

I have the following snippet in my dagger 2 module @Singleton @Provides @ElementsIntoSet fun providesQueries(foo: Foo): Set{ val queries = LinkedHashSet() queries.add(foo) return queries } I try to inject into in this…
Borja
  • 1,318
  • 13
  • 18
49
votes
16 answers

Unresolved reference DaggerApplicationComponent

I'm trying to create my app component, but Dagger does not generate my app component. here is MyApplication class class MyApplication : Application() { companion object { @JvmStatic lateinit var graph: ApplicationComponent } @Inject lateinit…
user5543258
47
votes
5 answers

Dagger 2: @Component.Builder is missing setters for required modules or components: [appi.example.com.dagger.AppModule]`

I'm configuring the new Dagger Android module but I got this error Here's my Component: @AppScope @Component(modules = {AppModule.class, NetModule.class}) public interface AppComponent { @Component.Builder interface Builder { …
Leonardo Deleon
  • 2,577
  • 5
  • 15
  • 22
47
votes
10 answers

Dagger and Kotlin. Dagger doesn't generate component classes

I'm new with kotlin and Dagger. I have a little problem that I do not how to solve and I don't find a solution. So this is what I have: @Module class AppModule (app: Application) { private var application: Application; init { …
Leo
  • 605
  • 1
  • 6
  • 10
46
votes
4 answers

Dagger 2 - two provides method that provide same interface

lets say I have: public interface Shape {} public class Rectangle implements Shape { } public class Circle implements Shape { } and I have a ApplicationModule which needs to provides instances for both Rec and Circle: @Module public class…
Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
45
votes
1 answer

Dagger 2 lifecycle of a component, module and scope

I've read a lot of posts and tutorials about dagger…
s-hunter
  • 24,172
  • 16
  • 88
  • 130
44
votes
2 answers

Method injection using Dagger 2

I haven't managed to find a good explanation/example on method injection using Dagger 2. Could someone please help me understand? Example: @Inject public Dinner makeDinner(Pasta pasta, Sauce sauce) { mPan.add(pasta); mPan.add(sauce); …
Vas
  • 2,014
  • 3
  • 22
  • 38
43
votes
2 answers

Dagger 2 - Why is this a dependency cycle?

I'm trying to inject the application's Context into 2 other objects, an AuthManager and an ApiClient. Both of them depends on said context, and the ApiClient depends on the AuthManager. Why is this a dependency cycle, if Context doesn't have a…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
42
votes
1 answer

Component (unscoped) may not reference scoped bindings

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…
Kremik13
  • 445
  • 1
  • 4
  • 6
42
votes
2 answers

Dagger 2 injecting parameters of constructor

I saw the following example on the Dagger 2 website: class Thermosiphon implements Pump { private final Heater heater; @Inject Thermosiphon(Heater heater) { this.heater = heater; } ... } and the documentation: When a new instance…
fweigl
  • 21,278
  • 20
  • 114
  • 205
41
votes
6 answers

HasActivityInjector can not be resolved in android dagger 2

I was trying to implement HasActivityInjector in my android application but it's showing me HasActivityInjector cann't be resolved. Below dependencies and plugin I have used in my project- apply plugin: 'kotlin-kapt' and implementation…
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
41
votes
2 answers

How do you organise your Dagger 2 modules and components?

Do you have a specific package where you put all the Dagger related classes? Or do you put them next to the relevant class they inject, e.g. if you have an MainActivityModule and MainActivityComponent, you put them in the same package as your…
ivacf
  • 1,163
  • 3
  • 11
  • 15