I'm new to dagger and my constructor/method injection doesn't seem to work.
Doesn't work meaning the injected fields are still requested.
This is my code:
#Module
public class AppContextModule {
private final Context appContext;
public AppContextModule(Context appContext) {
this.appContext = appContext;
}
#Singleton
#Provides
public Context getAppContext() {
return appContext;
}
}
#Singleton
#Component(modules = {AppContextModule.class})
public interface MyComponent {
void inject(ActivitiesLifeCycleListener obj);
void inject(WebViewManager obj);
Context context();
}
public final class MyClass {
private final WeakReference<Context> mAppContext;
#Inject
public MyClass(Context context) {
this.mAppContext = context
}
}
public class MyActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyClass my = new MyClass(); // The IDE requests context!
}
}
"MyClass" still requires "Context" although I used the "#Inject" annotation above the constructor.
What am I missing?
Do I even need to expose my Context?
Thanks.
Well...you declare your constructor
public MyClass(Context context) {
this.mAppContext = context
}
And then call it without any parameters
MyClass my = new MyClass(); // The IDE requests context!
Why do you think this should work? This is plain java, no dagger involved, and you are trying to create some new object without supplying the arguments needed.
Dagger is no magic that changes how java works. To use constructor injection you have to actually use Dagger to inject your fields.
public class MyActivity extends BaseActivity {
#Inject
MyClass mMyClass; // mark field for injection
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// todo ... create component
component.inject(this); // inject fields
}
}
Given that you provide a Context somewhere (you'll get a Context must be provided... compile error if it is not) dagger will then inject your activity with MyClass by calling inject—no magic. The inject method will assign the object to your field.
Constructor injection only means that you don't have to use a module and include a method to provide the object. It does not just magically create objects out of thin air or change the way constructors work.
Related
I have Application component with two modules - applicationModule and activityModule.
In my MvpApp.java class i create component with modules:
public class MvpApp extends Application {
#Override
public void onCreate() {
super.onCreate();
mApplicationComponent = DaggerApplicationComponent.builder()
.activityModule(new ActivityModule())
.applicationModule(new ApplicationModule(this))
.build();
}
}
My ApplicationModule has constructor with Application and i send it. But my `Activity module has constructor:
public ActivityModule(AppCompatActivity activity) {
this.mActivity = activity;
}
And how can i send my MainActivity instance into this constructor?
When i try
mApplicationComponent = DaggerApplicationComponent.builder()
.activityModule(new ActivityModule(new MainActivity()))
.applicationModule(new ApplicationModule(this))
.build();
I get an error.
First of all, it is not recommended to provide context to the dagger graph via module constructor. You should use the #BindsInstance annotation in your dagger component builder.
Also, you can not create a module with the constructor param of activity. You probably want to inject some object into your MainActivity. In this case, you need to create a method in your dagger component with a similar declaration and call it in your activity before calling
ApplicationComponent
public interface ApplicationComponent {
// your previous code
void inject(MainActivity target);
}
MainActivity
#Inject SomeClass someClass; // <- some class you want to inject from graph
#Override
protected void onCreate(Bundle savedInstanceState) {
((MvpApp) getApplicationContext()).mApplicationComponent.inject(this);
super.onCreate(savedInstanceState);
// your code
}
This seems very basic question for Dagger2 users . I have recently started exploring it with RetroFit. I have followed some tutorials and came up with the code below(some of it).
#Singleton
#Component(modules = {AppModule.class, ApiModule.class})
public interface ApiComponent {
void inject(MainActivity context);
}
public class MyApplication extends Application {
private ApiComponent mApiComponent;
#Override
public void onCreate() {
super.onCreate();
mApiComponent = DaggerApiComponent.builder()
.appModule(new AppModule(this))
.apiModule(new ApiModule("https://rect.otp/demos/"))
.build();
}
public ApiComponent getNetComponent() {
return mApiComponent;
}
}
And MainActivity.java
public class MainActivity extends AppCompatActivity {
#Inject
Retrofit retrofit;
ActivityMainBinding mainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
((MyApplication) getApplication()).getNetComponent().inject(this);
ApiCall api = retrofit.create(ApiCall.class);
}
}
Questions
1. When i change void inject(MainActivity context); to void inject(Context context); i am getting a NullPointerException on retrofit in MainActivity.Why?
When use void inject(MainActivity context); its working fine. Why ?
If i need to inject RetroFit in Multiple classes what should be the approach. Creating inject() for each class is not seems the solution.
I am a newbie to dependency Injections. So Can i have some guidence on it . What will be the proper approach to use it in multiple classes.
When you declare void inject(Context context) Dagger will generate code to inject Context. Since Context does not declare any #Inject annotated fields it will end up injecting nothing. This is why your retrofit is null after the injection.
When you declare void inject(MainActivity context) it will generate code to inject MainActivity that will also set your retrofit, thus it will be initialized.
Dagger will inject parent fields, but not childrens. The class that you declare is the one that the code will be generated for.
Your default way to inject objects should be Constructor Injection where you don't have to manually declare methods or inject the objects. e.g. see this answer for reference.
after search on web for learning about this feature most topics or post was using dependency injection for Retrofit or other android useful libraries, but i have some custom class which i want to use that with DI and i can't done it, for example i have simple custom class for using SharePreference and i'm using with that as an Singleton class
in my code i can't assign correct Dagger to component on SpApplication class to use that on activities or fragments
public class SP {
private SharedPreferences preferences;
private Context context;
public SP(Context context) {
this.context = context;
}
private SharedPreferences getPrefs() {
return preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
public String getString(SharedPrefsTypes propertyName) {
return getPrefs().getString(propertyName.toString(), "");
}
public int getInt(SharedPrefsTypes propertyName) {
return getPrefs().getInt(propertyName.toString(), 0);
}
...
public enum SharedPrefsTypes {
Login
}
}
now i'm trying to use DI for that:
AppModules class:
#Module
public class AppModules {
private Context context;
public AppModules(Context context) {
this.context = context;
}
#Provides
#Singleton
SP provideSharePreferences() {
SP sharePreference = new SP(context);
return sharePreference;
}
}
ApplicationComponent class:
#Component(modules = AppModules.class)
public interface ApplicationComponent {
void inject(ActivityMain activity);
}
SpApplication class:
public class SpApplication extends Application {
private static SpApplication self;
private ApplicationComponent component;
#Override
public void onCreate() {
super.onCreate();
self = this;
component = DaggerApplicationComponent.builder().build();
}
public static SpApplication get(Context context) {
return (SpApplication) context.getApplicationContext();
}
public static SpApplication getInstance() {
return self;
}
public ApplicationComponent getComponent() {
return component;
}
}
and my ActivityMain class:
public class ActivityMain extends AppCompatActivity {
#Inject
SP sharePreference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SpApplication) getApplication()).getComponent().inject(this);
sharePreference.setInt(SP.SharedPrefsTypes.Login, 0);
}
}
I get this error:
android.app.Application cannot be cast to com.pishguy.yendir.SpApplication
Thanks in advance
I guess you are trying to inject into ActivityMain, but since you did not provide its source, let me show you how you could inject into SpApplication. Then just copy the relevant parts into your Activity.
Few things I think you need to change.
Module:
Your AppModules class is generally OK, but I just suggest you to change the way you make use of Context - don't use it as a field, but inject it as any other service. It looks like this:
#Module
public class AppModules {
private Context context;
public AppModules(Context context) {
this.context = context;
}
#Provides // this can be non-scoped because anyway the same instance is always returned
Context provideContext() {
return this.context;
}
#Provides
#Singleton
SP provideSharePreferences(Context context) {
return new SP(context); // use method-local Context
}
}
Component:
If component injects scoped services (#Singleton is a scope), the component itself must be scoped
If you want to inject into SpApplication class, then declare it as an injection client in the component
Taking into account these two points, ApplicationComponent should look like this:
#Singleton // injects #Singleton scoped services
#Component(modules = AppModules.class)
public interface ApplicationComponent {
void inject(SpApplication application); // SpApplication is DI client (injection target)
}
Client:
I'd change few things in your SpApplication class:
The way you instantiate ApplicationComponent is incorrect
This is not a bug, but you don't really need get(Context) and getInstance() methods
In addition, since I'm showing how you inject into SpApplication, I will add the injection logic as well (which you should copy to your actual clients).
So, SpApplication (which is DI client) should look similar to this:
public class SpApplication extends Application {
#Inject SP sp; // the dependency that should be injected
private ApplicationComponent component;
#Override
public void onCreate() {
super.onCreate();
getComponent().inject(this); // this is when the actual injection takes place
}
public ApplicationComponent getComponent() {
if (component == null) {
// this is the way Dagger components should be instantiated
component = DaggerApplicationComponent.builder()
.appModules(new AppModules(this))
.build();
}
return component;
}
}
If you perform the above changes, I tend to believe that you'll be all right.
BTW, I recently completed a blog post about Dagger 2 scopes. You might want to check it if you are going to be serious about dependency injection.
I have 2 modules: AndroidModule for providing Context-related objects and PhotosModule providing PhotoManager.
I want to use PhotoManager in PhotosFragment. PhotoManager depends on DbManager, which is provided by AndroidModule.
How do I connect all the dots?
The code compiles, but a NPE is thrown on this line:
mDbManager.readDatabase();
meaning that the injection of DbManager into PhotoManager did not occur, even though I set the
injects = PhotoManager.class
in AndroidModule.
Also, mDbManager is not a private field, so I don't think I have to use injection on PhotoManager's constructor; field injection should be sufficient.
What's the reason and how do I satisfy the missing dependency? I assume there may be more than one problem with my code. Note: I just started with Dagger, please keep that in mind.
Application:
public class TestApp extends Application {
private ObjectGraph mObjectGraph;
#Override
public void onCreate() {
super.onCreate();
mObjectGraph = ObjectGraph.create(getModules().toArray());
}
protected List<Object> getModules() {
return Arrays.asList(new AndroidModule(this), new PhotosModule());
}
public void inject(Object object) {
mObjectGraph.inject(object);
}
}
AndroidModule:
#Module(library = true, injects = PhotoManager.class)
public class AndroidModule {
private final TestApp mApplication;
public AndroidModule(TestApp application) {
mApplication = application;
}
#Provides
#Singleton
#ForApplication
Context provideApplicationContext() {
return mApplication;
}
#Provides
#Singleton
DbManager provideDbManager() {
return new DbManager(mApplication);
}
}
PhotosModule:
#Module(injects = PhotosFragment.class)
public class PhotosModule {
#Provides
#Singleton
PhotoManager providePhotoManager() {
return new PhotoManager();
}
}
PhotoManager:
#Singleton
public class PhotoManager {
#Inject
DbManager mDbManager;
public void doSomething() {
mDbManager.readDatabase();
}
}
PhotosFragment:
public class PhotosFragment extends Fragment {
#Inject
PhotoManager mPhotoManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((TestApp) getActivity().getApplication()).inject(this);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
///...
mPhotoManager.doSomething();
}
}
DbManager:
#Singleton
public class DbManager {
public DbManager(Context context) {
//...
}
public void readDatabase() {
//...
}
}
Here's the core of the problem: in PhotosModule, you have the provider method:
PhotoManager providePhotoManager() {
return new PhotoManager();
}
You are newing up the PhotoManager yourself, so Dagger doesn't have an opportunity to inject its dependency (the DbManager).
I think it's because you're misunderstanding the meaning of the injects property on the #Module(injects=...) annotation of the AndroidModule. You have:
#Module(library = true, injects = PhotoManager.class)
But that injects isn't needed, because you're never calling objectGraph.inject on the PhotoManager. Instead, you're only injecting the PhotoFragment, which is correct...
So one way to fix the NPE problem:
remove the injects=PhotoManager.class from AndroidModule.java:
#Module(library = true)
change PhotosModule to include AndroidModule:
#Module(injects = PhotosFragment.class, includes = AndroidModule.class)
I avoid field injection using the #Inject annotation (only use it for the top-level objects, i.e. where you do the objectgraph.inject, such as activity and fragment level), and use constructor injection instead for all the other dependencies. So I'd change PhotoManager to inject the DbManager like this:
private final DbManager mDbManager;
public PhotoManager(DbManager dbManager) {
mDbManager = dbManager;
}
I've put up the code here on GitHub.
I am trying to inject my android context from one module into another. This is my code so far:
UserProfileModule.java
#Module(
library = true
)
public class UserProfileModule {
#Inject Context _context;
#Provides
public AccountUtils.UserProfile provideUserProfile() {
return AccountUtils.getUserProfile(_context);
}
}
RootModule.java
#Module(
injects = {
PizzaApplication.class,
UserProfileModule.class,
MainActivity.class
},
includes = {
UserProfileModule.class
},
library = true
)
public class RootModule {
private final Context _context;
public RootModule(Context context) {
_context = context;
}
#Provides
#Singleton
public Context provideApplicationContext() {
return _context;
}
}
Anytime it tries to get the user profile it fails saying the object is null.]
EDIT:
PizzaApplication.java
public class PizzaApplication extends Application {
private ObjectGraph objectGraph;
#Override
public void onCreate() {
super.onCreate();
injectDependencies();
}
private void injectDependencies() {
objectGraph = ObjectGraph.create(new RootModule(this));
objectGraph.inject(this);
}
public void inject(Object object) {
objectGraph.inject(object);
}
}
MainActivity.java
public class MainActivity extends BaseActivity {
#InjectView(R.id.toolbar) public Toolbar _toolbar;
#InjectView(R.id.drawer) public DrawerFrameLayout _drawer;
#Inject public AccountUtils.UserProfile _profile;
#Inject public Context _context;
// private NavigationDrawerFragment navigationDrawerFragment;
#Override
protected void onCreate(Bundle saveInstanceState) {
tl;dr (Short version):
For a minimal working example of your code, see my code on GitHub
You don't need to / should't inject the Context from one module into another.
The order/direction of your module includes is reversed, the UserProfileModule should include the RootModule.
More details and comments on your code:
You don't need to inject something into a module, a module only provides dependencies. In your case simply making use of the module includes gives the functionality you want.
Remove the library = true from UserProfileModule, because you only need this when the module's providers aren't all used directly by the classes specified in the injects list.
As Niek Haarman said, you need to pass both RootModule and UserProfileModule instances to ObjectGraph.create in your PizzaApplication's onCreate.
You're doing inject(this) in PizzaApplication but it's got no dependencies, so the inject isn't necessary. Based on the sample code you've provided, this makes me think you're assuming that injecting on the Application-level will also inject Activity dependencies...? You need to do the inject on your Activity too.
You don't show if you're doing an inject in your Activity's onCreate -- that's most likely what's missing.
You're injecting Context into the Activity but that's not necessary since you can just use getApplicationContext() in the Activity.
Here's the working code:
RootModule:
#Module(
injects = {MainActivity.class},
library = true,
complete = false
)
public class RootModule {
private final Context _context;
public RootModule(Context context) {
_context = context;
}
#Provides
#Singleton
public Context provideApplicationContext() {
return _context;
}
}
UserProfileModule:
#Module(includes = {RootModule.class})
public class UserProfileModule {
#Provides
public AccountUtils.UserProfile provideUserProfile(Context context) {
return AccountUtils.getUserProfile(context);
}
}
MainActivity:
public class MainActivity extends BaseActivity {
#Inject
public AccountUtils.UserProfile _profile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((PizzaApplication) getApplication()).inject(this);
_profile.message();
}
PizzaApplication:
public class PizzaApplication extends Application {
private ObjectGraph objectGraph;
#Override
public void onCreate() {
super.onCreate();
objectGraph = ObjectGraph.create(new RootModule(this), new UserProfileModule());
}
public void inject(Object object) {
objectGraph.inject(object);
}
}
You have
objectGraph = ObjectGraph.create(new RootModule(this));
But, includes = { UserProfileModule.class } is no magic. You have to create it yourself:
objectGraph = ObjectGraph.create(new RootModule(this), new UserProfileModule() );
Furthermore, the #Inject annotation on the Context doesn't work. You're better off modifying your provider method:
#Provides
public AccountUtils.UserProfile provideUserProfile(Context context) {
return AccountUtils.getUserProfile(context);
}
I cannot guarantee this will solve all your problems, but it will help you in the right direction.