dagger-2

Dagger + Retrofit dynamic URL

假如想象 提交于 2019-11-26 21:05:16
问题 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,

Dagger 2.10 Android subcomponents and builders

我的梦境 提交于 2019-11-26 19:45:59
Using the new (in 2.10) dagger.android classes, I'm trying to inject things using a Subcomponent that depends on other Modules, and, therefore, has a Builder with setters for those modules. The documentation on https://google.github.io/dagger/android.html describes this, but is not clear on how to actually write and/or invoke those setters. Quoting from the above link: AndroidInjection.inject() gets a DispatchingAndroidInjector from the Application and passes your activity to inject(Activity). The DispatchingAndroidInjector looks up the AndroidInjector.Factory for your activity’s class (which

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

末鹿安然 提交于 2019-11-26 19:17:48
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 which I am adding to my answer as I learned more about Dagger1, and learn more about Dagger2. This question is more of a guide rather than a "question".] EpicPandaForce Guide for Dagger 2.x (Revised Edition 6) : The steps are the following: 1.) add Dagger to your build.gradle files: top level build.gradle : . // Top-level build file where you can

Singleton object becomes null after app is resumed

戏子无情 提交于 2019-11-26 19:09:29
For my Android project, I need global singleton Cache object to access data about a user through the app. A problem occurs when an app goes into the background and after some time of using other apps I try to open app variables in Cache objects are null. Everything is okay when I kill the app and open it again. I'm using dependency injection to access Cache object. Why doesn't app start again if that happened? Is there some annotation to keep cache variable even in low memory conditions? This is my Cache class class Cache { var categories : Array<BaseResponse.Category>? = null var user :

Dagger @Reusable scope vs @Singleton

筅森魡賤 提交于 2019-11-26 19:08:52
问题 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 ? 回答1: 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

Dagger 2 injecting Android Context

放肆的年华 提交于 2019-11-26 16:48:51
问题 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

Android lifecycle library ViewModel using dagger 2

倾然丶 夕夏残阳落幕 提交于 2019-11-26 15:18:28
问题 I have a ViewModel class just like the one defined in the Connecting ViewModel and repository section of Architecture guide. When I run my app I get a runtime exception. Does anyone know how to get around this? Should I not be injecting the ViewModel? Is there a way to tell the ViewModelProvider to use Dagger to create the model? public class DispatchActivityModel extends ViewModel { private final API api; @Inject public DispatchActivityModel(API api) { this.api = api; } } Caused by: java

Update to Kotlin 1.3.30 breaks build with Dagger 2.21

天涯浪子 提交于 2019-11-26 14:41:30
问题 Build error after update from Kotling 1.3.21 to 1.3.30: AppComponent.java:16: error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>, javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method. Reproduced on two different projects with similar dependencies on Kotlin, Dagger and Architecture components. I suspect it somehow related to the recent kapt updates in kotlin 1.3.30: https://blog.jetbrains

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

寵の児 提交于 2019-11-26 14:24:47
问题 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 void inject() methods do. But I don't understand what the other Foo foo() getter methods do. What is the purpose of these other methods? 回答1: Dagger is a way of wiring up graphs of objects and their dependencies. As an alternative to calling constructors directly,

Dagger not generating components for /test class

こ雲淡風輕ζ 提交于 2019-11-26 13:28:25
问题 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 @Component(modules = { FlingyModule.class }) public interface FlingyComponent } @Module public class FlingyModule In app/src/test: public class TestFlingyApplication extends Application { @Singleton @Component(modules = { TestFlingyModule.class }) public interface