dagger-2

Dagger 2 injecting parameters of constructor

只谈情不闲聊 提交于 2019-11-28 04:08:29
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 is requested, Dagger will obtain the required parameters values and invoke this constructor. When I write a Module to provide a Thermosiphon like @Module public class ThermosiphonModule { @Provides @Singleton Thermosiphon provideThermosiphon() { return new Thermosiphon(???); } } the Thermosiphon constructor still requires a Heater as an argument, rendering the whole

Dagger 2: Injecting user inputted parameter into object

筅森魡賤 提交于 2019-11-28 04:06:39
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 validator) { } } I have a module that provides the Validator instance: @Provides @Singleton Validator provideValidator() { return Validator.getInstance(); } and an instance of the Util class: @Provides Util provideUtil(Validator validator) { return new Util(validator); } I have a component wired up that would give me an instance of Util: Util getUtil() so within my

How do you organise your Dagger 2 modules and components? [closed]

醉酒当歌 提交于 2019-11-28 03:40:35
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 MainActivity . Also, I've seen quite a few people defining components as inner classes, e.g. an ApplicationComponent that is defined inside the Application class. Do you think this is a good practice? EDIT: Let me start out with the fact that this is close to the truth here, but this is an antipattern as described by Martin Fowler's Data Domain

How to migrate missing inject from module with complete = false from Dagger 1 to Dagger 2

白昼怎懂夜的黑 提交于 2019-11-28 01:42:30
问题 I have a library project/module that is used by both Android apps and regular java apps. In Dagger 1 this project/module has property complete = false . Within there is an @Inject field that is not satisfied by any class implementation or @Provides method. The idea is to force the "top" module(s) which has complete = true to provide system specific implementation Just for the sake of example: In the library project I have ActLogin activity that have field @Inject @Named("app version")

Dagger 2 injecting Android Context

别来无恙 提交于 2019-11-27 22:42:28
I am using Dagger 2 and have it working however I now need access to the Android Application Context. Its not clear to me how to inject and get access to the context. I have tried to do this as follows: @Module public class MainActivityModule { private final Context context; MainActivityModule(Context context) { this.context = context; } @Provides @Singleton Context provideContext() { return context; } However this results in the following exception: java.lang.RuntimeException: Unable to create application : java.lang.IllegalStateException: mainActivityModule must be set If I inspect the

Dagger + Retrofit dynamic URL

杀马特。学长 韩版系。学妹 提交于 2019-11-27 22:27:48
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 baseUrl with my data (maybe in Interceptor?) just before call? CODE Singletons @Provides @Singleton Retrofit provideRetrofit(SharedPreferences prefs) { String apiUrl = "https://%1s%2s"; apiUrl = String.format(apiUrl, prefs.getString(ACCOUNT_SUBDOMAIN, null), prefs.getString(ACCOUNT_DOMAIN, null)); OkHttpClient httpClient = new OkHttpClient.Builder()

Dagger @Reusable scope vs @Singleton

风格不统一 提交于 2019-11-27 17:50:32
From the User's Guide : Sometimes you want to limit the number of times an @Inject-constructed class is instantiated or a @Provides method is called, but you don’t need to guarantee that the exact same instance is used during the lifetime of any particular component or subcomponent. Why would I use that instead of @Singleton ? Use @Singleton if you rely on singleton behavior and guarantees. Use @Reusable if an object would only be a @Singleton for performance reasons. @Reusable bindings have much more in common with unscoped bindings than @Singleton bindings: You're telling Dagger that you'd

Dagger: IllegalArgumentException: No injector factory bound for Class

你。 提交于 2019-11-27 17:05:08
问题 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<Class<? extends ViewModel>, Provider<ViewModel>> creators; @Inject public ProductViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) { this.creators = creators; } @SuppressWarnings("unchecked") @Override public <T extends ViewModel> T create(Class

Refreshing Dagger 2 instance from Android Application class

。_饼干妹妹 提交于 2019-11-27 16:11:44
问题 I have a set of @Singleton and @Provides method in my module class for the purpose of creating Singleton instance throughout the application. Everything works fine except few bottle neck scenarios like as follows: STEP 1. I am creating a Retrofit instance from OKHttpClient with Auth token in it to make a authenticated api calls each time (Auth token retrieval and insertion is handled through SharedPreferences ). But the problem starts at the time of relaunching the activity after when i

Unresolved reference DaggerApplicationComponent

ぐ巨炮叔叔 提交于 2019-11-27 14:21:46
问题 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 var locationManager : LocationManager override fun onCreate() { super.onCreate() graph = DaggerApplicationComponent.builder().appModule(AppModule(this)).build() graph.inject(this) } } and here is my AppComponent class @Singleton @Component(modules =