I know the github Android app using RoboGuice, and field injection needs the module to bind.
But I can not found the module that is supposed to bind the field "accountDataManager" in the com.github.mobile.ui.user.HomeActivity. And it is not the only one like this.
It just uses an #Inject attribute, but no module can be found.
Can anybody tell me how it works?
For using #InjectView you need to use ButterKnife view injection library. Check this link for more ButterKnife
Related
It seems to be impossible but I can't find it written explicitly. Is there a clear reason for that ?
Thanks
That's not possible. A component can either inject everything at once or the compilation will fail with a cannot be provided error, listing what it's missing.
After all you can't partially call a constructor (if using constructor injection) and also partially injecting fields would be rather indeterministic about which objects were injected when, how, or with which scope. If both components could supply a dependency, which should provide it? Do you create and inject the object twice? What if another object depends on it in the other component? It would create more confusion than any good it could do and provide a source for a lot of errors and unexpected behavior.
Only one component can be used to inject inside a given class. You can have several subcomponents dependencies installed on it though.
I was trying to add Dagger 2 to my android app.
As far as I understand, Dagger will construct my object(which I am trying to inject) as long as its' dependencies are provided(in a Module) or they are injected using some form of injection(constructor/method).
I would like to know if there's a distinction between when a dependency should be provided in a Module(say Application Module) vs when its' injected using a constructor injection, and if there is any rule of when I should do which?
Both are the same. Constructor injection basically eliminates the need to write a provider method. As a rule of thumb, I mostly use it for classes with a no-args constructor for easy injection, like Util classes.
There's no difference really. As long as Dagger knows how to construct an instance, that's all that matters.
The reason there are two ways to do it is that you don't always have the ability to use constructor injection, for instance if the class is part of a library that you are using but which you don't have the source (and so you can't add #Inject on one of the constructors).
I want to use dagger library to android library project. I also fallows How to use dagger in a android library project but does not get proper implementation. Anyone has demo or any Ideas about it ? I also want these library classes extends in project for some changes.
I don't know what the library is, but I would assume most of the code doesn't contain classes with uncontrolled lifecycle (activities, services, fragments, etc.).
If it is true then just use constructors to pass your dependencies.
If it is not then, you have to make a decision who is going to hold knowledge about dependencies graph. A usual solution that context is castable to objects graph knowledge, or it know how to get it. Usually, classes with uncontrolled lifecycle have access to the context
I'm going through androidannotations cookbook and I can't find way to inject dependencies to Bean via constructor.
Is it possible at all with this library? I have everything else up and running, but I just can't fix this single problem :/
AndroidAnnotations does not provide this feature currently. Setter injection maybe added in the next release.
Use field injection, if possible.
While reading the docs for dagger 2 I cannot find an easy way to provide a dependency when building an app for testing. The only clue I've found is this:
Dagger 2 doesn't support overrides. Modules that
override for simple testing fakes can create
a subclass of the module to emulate that behaviour.
Modules that use overrides and rely on dependency injection
should be decomposed so that the overridden modules are instead
represented as a choice between two modules.
I don't understand how I would set up such a configuration on Android, anyone can explain?
This is currently impossible with Dagger 2 (as of v2.0.0) without some workarounds. You can read about it here.
I've proposed one workaround but this requires changes to the production code.
In short:
provide additional setter for #Component (e.g. in Android setter in Application class)
test component must extend the production component
For more information please check both links. Hope this issue will be addressed in future versions of Dagger 2.