dagger-2

Dagger 2 constructor injection in kotlin with Named arguments

依然范特西╮ 提交于 2019-12-03 01:18:17
I have this dependency: @Singleton class SpiceMix @Inject constructor(@field:[Named("oregano")] private val oregano: Spice, @field:[Named("sage")] private val sage: Spice, @field:[Named("rosemary")] private val rosemary: Spice) And a module to fulfill its dependencies: @Module class SpiceModule { @Provides @Named("oregano") @Singleton fun provideOregano(): Spice = Oregano() @Provides @Named("sage") @Singleton fun provideSage(): Spice = Sage() @Provides @Named("rosemary") @Singleton fun provideRosemary(): Spice = Rosemary() The SpiceMix is then injected in various locations of my app. However,

Dagger and Kotlin. Dagger doesn't generate component classes

蹲街弑〆低调 提交于 2019-12-03 00:58:55
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 { this.application = app; } @Provides fun provideApplication(): Application? { return application; } @Provides fun provideResources(): Resources? { return application.resources; } } @Singleton @Component(modules = arrayOf(AppModule::class)) interface AppComponent: AppComponentBase { public class Initializer { private constructor(){} companion object { fun Init(app:

Dagger 2 - what is the purpose of a @Singleton annotation class

橙三吉。 提交于 2019-12-03 00:55:59
From the dagger 2 Documentation I noticed that you can have a @Singleton annotated class. What is the purpose of marking a class as @Singleton as I have tried to do this in my code but a singleton object is NOT produced. I'm not clear on what use marking my class with this annotation serves. From the documentation please focus on the following statement: The @Singleton annotation on an injectable class also serves as documentation. It reminds potential maintainers that this class may be shared by multiple threads.* @Singleton class CoffeeMaker { ... } UPDATE: After reviewing froger_mcs answer

What is actual usage of “HasFragmentInjector” in Dagger 2

不羁的心 提交于 2019-12-02 20:44:10
I have implemented dagger2 v2.2 previously but as now they have added dagger.android part also. so I am creating sample project with that. I am aware about old methodology of @Provide and @Modules and @Components etc annotations but from Dagger 2.8+ they have added this android-support library also which have some new Injections like @ActivityKey , @ContributesAndroidInjector , @Subcomponent.Builder etc. So my question is what benefits it brings to the table. Does it resolve problems like Inject method with base class can work for all child class ? or Any other benefits ? 2nd question -

Dagger2: Unable to inject dependencies in WorkManager

烈酒焚心 提交于 2019-12-02 19:25:14
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 debugger doesn't even enter the Worker class. For eg I tried doing this: @Component(modules = {Module.class})

Dagger 2, sometimes on compiling I get “cannot find symbol class DaggerApplicationComponent”

三世轮回 提交于 2019-12-02 18:42:00
Recent after update Android Studio (2.0.7) (maybe this is the cause) sometimes when building i get that error. Idea is that usually compilation goes well but sometimes I get dagger error. Is possible that is problem in Dagger configuration? Error itself: Executing tasks: [:app:assembleDebug] Configuration on demand is an incubating feature. Incremental java compilation is an incubating feature. :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72311Library UP-TO-DATE :app

Different @Singleton & static @Provides in dagger2

假如想象 提交于 2019-12-02 18:15:26
May I know the different between @Singleton VS static Provides in dagger2? @Provides static User currentUser(AuthManager authManager) { return authManager.currentUser(); } @Provides @Singleton User currentUser(AuthManager authManager) { return authManager.currentUser(); } Jeff Bowman These are very different attributes, and you can have one or the other independently. All of these are valid: @Provides User currentUser(...) {} @Provides static User currentUser(...) {} @Provides @Singleton User currentUser(...) {} @Provides @Singleton static User currentUser(...) {} To set the stage, a @Provides

Dagger 2 injection in non Activity Java class

ぃ、小莉子 提交于 2019-12-02 16:44:44
I am trying to use Dagger2 for DI, it works perfectly fine for Activity/Fragment related classes where there is a onCreate lifecycle event. Now I have a plain Java class which I want to be injected. Any ideas as to how to go about it would be appreciated. The code I have looks like this : BasicMoviesUsecaseComponent.java - @PerActivity @Component(dependencies = AppComponent.class, modules = BasicMoviesUsecasesModule.class) public interface BasicMoviesUsecasesComponent { void inject(PaymentsManager paymentsManager); } DatabaseModule.java - @Module public class DatabaseModule { @Provides

Why @ContributesAndroidInjector doesn't provide Android Framework type

左心房为你撑大大i 提交于 2019-12-02 15:59:22
问题 I have simplified my application to get the root of the problem and here is the simplified version. I'm implementing Dagger 2 using following configuration: AppComponent @Component(modules = [ AndroidSupportInjectionModule::class, ActivityBindingModule::class ]) interface AppComponent: AndroidInjector<MyApp> { @Component.Builder interface Builder{ @BindsInstance fun application(application: Application): Builder fun build(): AppComponent } } ActivityBindingModule @Module abstract class

Dagger AndroidInjector cannot be provided without an @Provides-annotated method

Deadly 提交于 2019-12-02 14:39:11
问题 I've done my Android projects MainActivity by means of the MVP pattern. So in my MainPresenter I want to inject a dynamic String, which then shall populate a TextView for instance: class MyMainPresenter @Inject constructor(@StringForTextView dynamicString : String ) whereas the StringForTextView annotation qualifier is defined as: import javax.inject.Qualifier @Qualifier @MustBeDocumented @Retention(AnnotationRetention.RUNTIME) annotation class StringForTextView then I also have my Interface: