dagger-2

Set dynamic base url using Retrofit 2.0 and Dagger 2

可紊 提交于 2019-11-26 12:18:48
问题 I\'m trying to perform a login action using Retrofit 2.0 using Dagger 2 Here\'s how I set up Retrofit dependency @Provides @Singleton Retrofit provideRetrofit(Gson gson, OkHttpClient client) { Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson) .client(client) .baseUrl(application.getUrl()) .build(); return retrofit; } Here\'s the API interface. interface LoginAPI { @GET(relative_path) Call<Boolean> logMe(); } I have three different base urls

How do you override a module/dependency in a unit test with Dagger 2.0?

穿精又带淫゛_ 提交于 2019-11-26 11:56:38
问题 I have a simple Android activity with a single dependency. I inject the dependency into the activity\'s onCreate like this: Dagger_HelloComponent.builder() .helloModule(new HelloModule(this)) .build() .initialize(this); In my ActivityUnitTestCase I want to override the dependency with a Mockito mock. I assume I need to use a test-specific module which provides the mock, but I can\'t figure out how to add this module to the object graph. In Dagger 1.x this is apparently done with something

Why is my field `null` after injection? How do I inject my object?

≡放荡痞女 提交于 2019-11-26 11:36:17
问题 This is a Canonical Question because there are a lot of misconceptions about object initialization with Dagger 2. If your question was flagged as a duplicate please read this post carefully and make sure to understand the difference between constructor injection and field injection. I try to inject a Context into my presenter, but I get a NullPointerException when trying to use it. class MyPresenter { @Inject Context context; private MyView view; @Inject MyPresenter(MyView view) { this.view =

Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

时间秒杀一切 提交于 2019-11-26 09:07:31
问题 I am getting the following error while building the project. haven\'t used CoordinatorLayout in this project. just added as a dependency in build.gradle : I am using Android Studio 3.2 Canary 4. LogCat AGPBI: {\"kind\":\"error\",\"text\":\"Program type already present: android.support.design.widget.CoordinatorLayout$Behavior\",\"sources\":[{}],\"tool\":\"D8\"} :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution

Dagger 2 subcomponents vs component dependencies

独自空忆成欢 提交于 2019-11-26 08:39:59
问题 Dagger 1\'s plus() method is something I used quite often in previous applications, so I understand situations where you might want to have a subcomponent with full access to the parent graphs bindings. In what situation would it be beneficial to use a component dependency instead of a subcomponent dependency and why? 回答1: Component dependencies - Use this when you want to keep two components independent. Subcomponents - Use this when you want to keep two components coupled. I will use the

Dagger 2.10 Android subcomponents and builders

…衆ロ難τιáo~ 提交于 2019-11-26 07:25:19
问题 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

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

北城余情 提交于 2019-11-26 04:57:34
问题 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\".] 回答1: Guide for Dagger 2.x (Revised Edition 6) : The steps are the following: 1.) add

Singleton object becomes null after app is resumed

↘锁芯ラ 提交于 2019-11-26 02:38:39
问题 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?

How do I fix Dagger 2 error &#39;… cannot be provided […]&#39;?

ぐ巨炮叔叔 提交于 2019-11-25 23:00:00
问题 This is a Canonical Question because this is a common error with Dagger 2. If your question was flagged as a duplicate please read this post carefully and make sure to understand what this error means and why it occured. If this post does not work for you make sure to include where and how you provide the mentioned classes and include the full error message in your question like the one here. I tried to use a dependency with Dagger 2, but I receive the following error when I try to compile my