dagger-2

Building an Android Instant App with Application Component from Dagger

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 09:52:27
I'm currently experimenting with InstantApps and would like to include dagger into my project. I'm facing an issue setting up an application AppComponent. My application component includes all the feature dagger modules of my app. I basically have: One Base app module containing my Application class Multiple features with each a dagger Module per Activity, all with Base as a dependency. One app module and instant module both importing all the features and the base app module. I'm trying to figure out the setup before adding the Instant App module. From InstantApps documentation and project

Dependencies from two components in one activity

痴心易碎 提交于 2019-12-03 07:47:00
I'm playing with Dagger-2 to figure how to integrate it in our existing application and I'm facing something which I can't understand or I'm doing wrong. My situation : 3 API without any annotated constructor ( each one in its own file ) public class DbApi { public void doSomething(String username, String password) { } } public class RestApi { public void doSomething(String username, String password) { } } public class SocketApi { public void doSomething(String username, String password) { } } 3 Modules ( each one in its own file ) @Module public class DbModule { @Provides @Singleton public

How can I build a Dagger-based Android library without forcing consuming applications to use Dagger?

天大地大妈咪最大 提交于 2019-12-03 07:14:11
问题 I'm working on an Android library that is basically a client for some REST services I've written. I have several storage classes, network queues, parsers, and so on, and like many such classes, they have dependencies on Context or on things like SharedPreferences that are constructed from Context . These objects are all hidden behind a facade class, so consumers of my library don't see them or interact with them directly. For my own sanity, I would like to use Dagger 2 for dependency

What is actual usage of “HasFragmentInjector” in Dagger 2

泄露秘密 提交于 2019-12-03 07:10:13
问题 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

Presenter injection with Dagger 2

只愿长相守 提交于 2019-12-03 06:12:17
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 mApplication; public AppModule(Application application) { mApplication = application; } @Provides @Singleton Application providesApplication() { return mApplication; } } DataModule.java: @Module public class DataModule { private static final String BASE_URL = "http://beta.fridgewizard.com:9001/api/"; @Provides @Singleton NetworkService

Different @Singleton & static @Provides in dagger2

爱⌒轻易说出口 提交于 2019-12-03 04:56:04
问题 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(); } 回答1: 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

Dagger 2 injection in non Activity Java class

人走茶凉 提交于 2019-12-03 03:14:04
问题 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

How to inject dependencies into any kind of object with Dagger2?

*爱你&永不变心* 提交于 2019-12-03 02:31:52
According to http://konmik.github.io/snorkeling-with-dagger-2.html i could just add inject(Anything anything) into AppComponent.java, but this doesn't work for me, in the articles example: @Singleton @Component(modules = AppModule.class) public interface AppComponent { void inject(MainActivity activity); void inject(MainFragment fragment); void inject(MainToolbarView view); } If I try to inject dependencies into my fragment the injected members remain null. What obvious error am I missing here? Relevant sources: ApplicationComponent.java @Singleton @Component( modules = ApplicationModule.class

How to create an object using constructor injection?

偶尔善良 提交于 2019-12-03 02:30:59
How would I create an instance of Dog with a component which provides Cat. public final class Dog { private final Cat mCat; public final static String TAG = "Dog"; @Inject public Dog(Cat cat) { mCat = cat; Log.e(TAG, "Dog class created"); } } After experimenting with Dagger 2 for a while I have no idea how to use constructor injection – a hint would be nice, thanks. Edit: Whats wrong with the question? After using Dagger 2, following several tutorials and reading the official documentation I have no clue how to use the constructor injection feature, that's why I ask here. Instead of injecting

Dagger 2: When to use constructor injections and when to use field injections?

℡╲_俬逩灬. 提交于 2019-12-03 01:24:07
I was kind of lazy and used to use almost entirely field injections. I was just providing empty constructor, put my @Inject fields I everything was looking nice and simple. However field injection have its trade-offs so I've devised some simple rules that help me to decide when to used field and when to use constructor injections. I will appreciate any feedback if there is mistake in my logic or if you have additional considerations to add. First some clarification in order to be on the same page: Constructor injection: @Inject public SomeClass(@Named("app version") String appVersion, AppPrefs