I am currently trying to integrate Dagger 2 into an Android application. My project setup is as follows:
- library
- app (depends on library)
In my library project I defined a class that I'll later inject into other classes that need it (Activities and regular classes) in the library as well as the app project.
@Singleton
public class MyManager{
@Inject
public MyManager(){
//Do some initializing
}
}
Now - for instance in my Fragments or Activities or regular classes I'd inject the above Singleton as follows:
public class SomeClass{
@Inject
MyManager myManager;
}
Or so I thought, because in practice myManager is always null. And apparently it's constructor is never called either, so I guess I must be missing something configuration-wise? Or maybe I misunderstood the documentation and it's not meant to work this way at all? The purpose of MyManager class is to be an application-wide accessible component-accumulating entity - that's why I went for the @Singleton.
UPDATE
To avoid confusion: I mentioned my having components somewhere in a comment I think - this refers to components in the sense of "component based design" and has nothing to do with dagger. The dagger-based code I have is all listed above - there is nothing else related to dagger in my code.
When I started adding @Component I had some compiler issues, because my dagger2 was not setup properly - check out this really helpful thread on how to setup dagger2 correctly: https://stackoverflow.com/a/29943394/1041533
UPDATE 2
Here is my updated code, based on G. Lombard's suggestions - I changed the code as follows - the original Singleton is in the library project:
@Singleton
public class MyManager{
@Inject
public MyManager(){
//Do some initializing
}
}
Also in the library project is the bootstrap class:
@Singleton
@Component
public interface Bootstrap {
void initialize(Activity activity);
}
Then I use the above Bootstrap class in my activity (in my concrete app, NOT in the library project! I do however also have Classes/Activities in the library that'll access Bootstrap to inject MyManager):
public class MyActivity extends Activity{
@Inject
MyManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//DONT DO THIS !!! AS EXPLAINED BY EpicPandaForce
DaggerBootstrap.create().initialize(this);
}
}
But even after this line:
DaggerBootstrap.create().initialize(this);
the manager instance is still null, i.e. not injected.
I just found this: https://stackoverflow.com/a/29326023/1041533
Which if I don't misread, implies I need to specify every single class in the Bootstrap class that will use @Inject to have stuff injected. Sadly - this is not an option, as I have more than 40 classes and activities for which I'd have to do that.
Meaning my Bootstrap interface apparently would have to look something like this:
@Singleton
@Component
public interface Bootstrap {
void initialize(ActivityA activity);
void initialize(ActivityB activity);
void initialize(ActivityC activity);
void initialize(ActivityD activity);
void initialize(ActivityE activity);
void initialize(ActivityF activity);
//and so on and so forth...
}
If the above is true, that would not be worth it for my use case. Plus: Seems there is no compile-time check, if I forgot to specify one of my 40+ classes here? It just wont work - i.e. crash the app at runtime.
void inject(YourClass yourClass);
method defined or otherwise it won't work. Back in the days of Dagger1, you had to provide this in aninjects={YourClass.class}
kind of array and it actually did throw a runtime exception if you messed up! It was pretty bad. We have about 53 classes defined in our ApplicationComponent for field injection and the provided classes (non-lifecycle) can just call inject in their constructors. Voila! – EpicPandaForceinject
, then it'll whine about how it needs to be added to the component. I feel like saying that "they forget to inject" is like saying "I marked the view fields with annotations but my view is null" because they forget to callButterKnife.bind(this);
aftersetContentView(R.layout.something);
... They'll figure out that it's kinda required to make it work. My workmate was completely new to dagger (and dependency injection), and he picked it up really quickly. – EpicPandaForceBootstrap
class is an actual dagger component and it is annotated as such. The name of the method does not matter, it can be called eitherinject
orinitialize
or whatever as long as its return type isvoid
and the parameter is whatever you want to inject. – EpicPandaForce