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
11
votes
3 answers

ViewModelProviders.get(...) in base class

I'm trying to cut some of Dagger's boilerplate by moving some of my ViewModel instantiation in an abstract base class but can't find quite a good way to do this. My intent is to instantiate all my ViewModels from my base fragment for them to be…
ben
  • 1,151
  • 10
  • 20
11
votes
3 answers

Dagger2 Qualifier not working with Kotlin?

I have a simple class as below class MainString(val msg: String) I want to inject with different argument to it, so I use the @Named Qualifier as per the guide shown in https://google.github.io/dagger/users-guide With that my AppModule…
Elye
  • 53,639
  • 54
  • 212
  • 474
11
votes
1 answer

Dagger 2 ContributesAndroidInjector provide activity to module

I'm trying to figure out a less-boilerplate-y way to implement an ActivityModule that is used in all of my app activities. This is my current setup: ActivityModule: @Module class ActivityModule(private val activity: Activity) { @Provides…
Valentin
  • 1,731
  • 2
  • 19
  • 29
11
votes
5 answers

Could not find method apt()

I'm trying to work with Dagger2, I am using Android studio 2.2.2 but I have an error with gradle: Error:(34, 0) Could not find method apt() for arguments [com.google.dagger:dagger-compiler:2.6] on object of type …
ediBersh
  • 1,135
  • 1
  • 12
  • 19
11
votes
1 answer

Espresso Test Failed: Wanted to match 1 intents. Actually matched 0 intents

I am trying to test that my SplashActivity correctly launches HomeActivity if the user is logged in. I have looked at related questions on StackOverflow, this seems to be a common issue, but I just cannot seem to get anything to work. I have watched…
madcow
  • 2,567
  • 14
  • 31
11
votes
2 answers

Dagger 2 reinit singleton

I am trying to store my user Object as a singleton with Dagger 2. @Provides @Named("me") @Singleton User provideUser(PrefsUtil prefsUtil, UserDao userDao) { int id = prefsUtil.getFromPrefs("me", 0); if (id == 0){ …
Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40
11
votes
2 answers

What is the purpose of the getter methods in Components in Dagger 2?

I am trying to understand Components in Dagger 2. Here is an example: @Component(modules = { MyModule.class }) public interface MyComponent { void inject(InjectionSite injectionSite); Foo foo(); Bar bar(); } I understand what the…
David Rawson
  • 20,912
  • 7
  • 88
  • 124
11
votes
0 answers

Dagger2 Error :component depends on scoped component in a non-hierarchical scope ordering

Can someone help me to resolve this issue, I'm trying to add dagger2 to my application getting this scope issue. Error:(14, 1) error: com.bmx.presentation.authentication.login.LoginComponent depends on scoped components in a non-hierarchical scope…
Sam
  • 6,215
  • 9
  • 71
  • 90
11
votes
2 answers

Optional injection Dagger 2

Is it possible to optionally inject a value in dagger 2? In particular, I want to do something like this. @Inject A(Optional b) { this.b = b; } If B is undefined in the modules, I would want dagger to give an Optional.empty(), If it is…
Cogman
  • 2,070
  • 19
  • 36
11
votes
4 answers

Android plain Junit with Dagger 2

I used to work in MVP and I usually test my presenters using a plain Junit (Not the Instrumentation !) , since Presenters only have the business logic and no references to Android internals whatsoever. Now by switching to Dagger 2 , I understood…
Ivelius
  • 4,953
  • 7
  • 30
  • 54
11
votes
2 answers

Presenter injection with Dagger 2

I just started using Dagger 2 and I found online thousands guides each one with a different implementation and I'm a bit confused now. So basically this is what I wrote at the moment: AppModule.java: @Module public class AppModule { Application…
user1341300
  • 375
  • 1
  • 5
  • 19
11
votes
2 answers

Dagger + Retrofit dynamic URL

PROBLEM I need to call API from domains entered by USER and I need to edit my Retrofit singleton before the call accordingly to the inserted data. Is there a way to "reset" my singleton, forcing it to recreate? or Is there a way to update my…
JakubW
  • 1,101
  • 1
  • 20
  • 37
11
votes
3 answers

Dagger 2 - Constructor injection - Scope

I'm using Dagger 2 with simple MVP pattern. I have a @PerApp and @PerActivity scopes. I'm injecting presenters dependencies using constructor injection, which makes those presenters "injectable" (I don't need to write provides methods in activity…
rafakob
  • 3,946
  • 3
  • 26
  • 36
11
votes
2 answers

Dagger2: Error when two components has same inject method signature

I have this component: @Singleton @Component(modules = OauthModule.class) public interface OauthComponent { void inject(LoginActivity a); } and the module: @Module public class OauthModule { @Provides @Singleton Oauth2Service…
Héctor
  • 24,444
  • 35
  • 132
  • 243
11
votes
2 answers

Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac

I have been testing out Dagger 2, and everything had been working, until I did a bit of refactoring. Now gradle is throwing an IllegalArgumentException, and I cannot figure out what I changed that is now causing the error. I haven't made any changes…
Bryan
  • 14,756
  • 10
  • 70
  • 125