dagger2 Inject field NullPointerException - android

I used dagger2 in my project, but the injection field is always null. Here is the code.
sorry, my english is poor.
Thanks in advance.
Module
#Module
public class RetrofitModule {
#Provides
#Singleton
Retrofit provideRetrofit() {
return new Retrofit.Builder().build();
}
}
Component
#Component(modules = RetrofitModule.class)
public interface RetrofitComponent {
void inject(Activity activity);
}
And in MainActivity, I write this
DaggerRetrofitComponent.builder().build().inject(this);
But the Retrofit is always null. How can I solve it?

You can not inject this way to your Activity class!
change your component like this and specify the exact name of your Activity:
#Component(modules = RetrofitModule.class)
public interface RetrofitComponent {
void inject(MainActivity activity);
}
and then perhaps also you have to change your module like this or anything else that fit your need:
#Module
public class RetrofitModule {
#Provides
Retrofit provideRetrofit() {
return new Retrofit.Builder().baseUrl("http://google.com").build();
}
}
By the way, make sure you have written #Inject before Retrofit declaration in your activity:
#Inject
Retrofit retrofit;
note that: if you want to have singleton provide in your module, the
whole component cannot remain unstopped and it must be annotated
#Singleton.
I hope it helps :)

Related

dagger2 constructor injection how to provide dependency without module

I've read that constructor injections don't require a module.
So I have this questions.
If I have this constructor injection:
private Model realmModel;
#Inject
public MainActivityPresenter(Model realmModel) {
this.realmModel = realmModel;
}
and this component:
#Singleton
#Component(modules = {AppModule.class})
public interface AppComponent {
Model realmModel();
void inject(MainActivity activity);
}
if in my MainActivity I do it:
((MyApp)getApplication()).createAppComponent().inject(this);
how could I pass the realmModel parameter to the presenter constructor injection?
EDIT: this is the model:
Presenter presenter;
#Inject
public RealmModel(Presenter presenter) {
this.presenter = presenter;
}
Thanks
Three ways to solve this problem
1) Give a module which does the provide of the Relam Model
#Provides
#Singleton
public Model providesRealmModel() {
return new Model();
}
2) Make your RelamModel class also constructor injected
class Model {
#Inject
public Model() {}
}
The Trick in construction injection is all its dependencies should also be constuctor injeceted then it would work fine.
(From experience your model would need application context. look at the 3 option for ways to implement external Dependencies
3) Provide Model as external dependency.
#Module
public class ModelModule {
private Model relamModel;
public ModelModule(Model relamModel) {
this.relamModel = relamModel
}
}
#Component(module={ModelModule.class})
public interface ApplicationComponent {
}
Take a look at the series of videos from twisted eqautions these were my first video tutorial on dagger2. I found it really helpful. Hope it helps you too
https://www.youtube.com/watch?v=Qwk7ESmaCq0
You have two choices:
use a module with a provide method
annotate the constructor of Model with #Inject, doing that when you pass realmModel in the presenter contructor, the model
constructor will be called.
I prefer to use modules, but that's just my opinion.

Objects cannot be provided without an #Provides-annotated method

I've started to setup Dagger 2 and faced a strange issue that looks like a bug to me.
I have 1 main component and 1 subcomponent which I 'plus' in parent component.
I use:
compile "com.google.dagger:dagger:2.4"
apt "com.google.dagger:dagger-compiler:2.4"
annotationProcessor "com.google.dagger:dagger-compiler:2.4"
Application component is prety easy. It just 'plus' the subcomponent:
#Singleton #Component(modules = { AristocratsAppModule.class })
public interface AristocratsAppComponent {
StreamComponent plus(StreamModule module);
}
Application module also quite basic. It provides application level dependancies:
#Module public class AristocratsAppModule {
private static AristocratsApp app;
public AristocratsAppModule(AristocratsApp app) {
AristocratsAppModule.app = app;
}
#Provides #Singleton AristocratsApp providesApplication() {
return app;
}
#Provides #Singleton GsonXml providesGsonXml() {
return ...
}
#Provides #Singleton #Named("aristocrats") Retrofit providesAristocratsRetrofit() {
return ...
}
#Provides #Singleton #Named("last_fm") Retrofit providesLastFmRetrofit() {
return ...
}
}
Subcomponent has two StreamServices (Retrofit services) and simple inject:
#PerController #Subcomponent(modules = { StreamModule.class }) public interface StreamComponent {
void inject(StreamsController streamsController);
AristocratsStreamService providesAristocratsStreamService();
LastFmStreamService providesLastFmStreamService();
}
Module of subcomponent provides presenter injection and two different injections of Retrofit services:
#Module public class StreamModule {
private StreamsController mView;
public StreamModule(StreamsController view) {
mView = view;
}
#Provides #PerController StreamMvp.Presenter providesPresenter(StreamCase streamCase,
StreamModelMapper streamMapper) {
return new StreamPresenter(mView, streamCase, streamMapper);
}
#Provides #PerController AristocratsStreamService providesAristocratsStreamService(
#Named("aristocrats") Retrofit retrofit) {
return retrofit.create(AristocratsStreamService.class);
}
#Provides #PerController LastFmStreamService providesLastFmStreamService(
#Named("last_fm") Retrofit retrofit) {
return retrofit.create(LastFmStreamService.class);
}
}
Application injection that I call in onCreate() of my app class:
mAppComponent = DaggerAristocratsAppComponent.builder()
.aristocratsAppModule(new AristocratsAppModule(this))
.build();
View injection that I call in my controller:
getAppComponent().plus(new StreamModule(this)).inject(this);
Exception that I'm getting during build:
Error:(13, 8) error: [com.qsoft.streams.presentation.di.StreamComponent.inject(com.qsoft.streams.presentation.StreamsController)] com.qsoft.streams.data.network.AristocratsStreamService cannot be provided without an #Provides-annotated method.
com.qsoft.streams.data.network.AristocratsStreamService is injected at
com.qsoft.streams.data.network.StreamApi.<init>(…, streamServiceAristocrats, …)
com.qsoft.streams.data.network.StreamApi is injected at
com.qsoft.streams.data.repository.StreamRepository.<init>(streamApi, …)
com.qsoft.streams.data.repository.StreamRepository is injected at
com.qsoft.streams.domain.interactors.StreamCase.<init>(streamRepository)
com.qsoft.streams.domain.interactors.StreamCase is injected at
com.qsoft.streams.presentation.di.StreamModule.providesPresenter(streamCase, …)
com.qsoft.streams.presentation.StreamMvp.Presenter is injected at
com.qsoft.streams.presentation.StreamsController.mPresenter
com.qsoft.streams.presentation.StreamsController is injected at
com.qsoft.streams.presentation.di.StreamComponent.inject(streamsController)
A binding with matching key exists in component: com.qsoft.streams.presentation.di.StreamComponent
Error complaints about missing #Provides-annotated method but they are inside my child module. Seems that Dagger2 just does not see them. I was thinking that the problem could be in the missing declaration of the AristocratsStreamService in subcomponent, so I even added it there but nothing changed.
This situation looks very strange and I would like to hear some inputs from more experienced Dagger2 developers.
Thank you in advance!
I have noticed that you did not set subcomponents part in AristrocratsModule.
This:
#Module public class AristocratsAppModule {
into this:
#Module(subcomponents = {StreamComponent.class})
public class AristocratsAppModule {
Try to make the following changes. In your StreamComponent create a Builder.
#Subcomponent.Builder
interface Builder {
Builder streamModule(StreamModule module);
StreamComponent build();
}
Eventually you will be injecting everything via StreamComponent, and it will inject everything in AristocratsAppComponent as well.
public StreamComponent getStreamComponent(Controller controller) {
return getAppComponent().streamBuilder().streamModule(new StreamModule(controller)).build();
}
Plus thing can be removed after this.

Dagger 2 How to create a Module for Base Activity Components and a separate Module for all MVP components

Hello I am new to Dagger2.
Goal. Take my Networking DI and MVP DI. MVP as in the presenter for an an activity that extends base activity. I want to combine all this into one super module and place this into my base activity. Over time add more presenters.
I do not want 30+ inject statements in my baseActivity.
I am following this example but it is too simple compared to what I am trying to do.
I think the issue is with injecting the API at the base activity. For some reason Dagger is looking for Api in my MVP class.. So that would be a dependency graph issue?
Having spent more time on this.. The issue stems from Mvp's interface of baseActivity or any sub activity that extends baseActivity. That means when it goes to inject, it sees the #inject Api call, and cannot find it. It will work if I add Api to this module, but thats upside down of what I want. I want Component / Module for Application level items. I then want a component / module that has all my different MVP component in one module.. It's like Dagger starts looking for dependencies in the leaf of a tree and getting upset when it doesn't see whats in the root. I need it to go the other way. Be satisfied that I injected the dependency in the Root activity.
Base Activity...
#inject
public ApiClient mClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mManager = new SharedPreferencesManager(this);
DaggerInjector.get().inject(this);
}
DaggerInjector
public class DaggerInjector {
private static AppComponent appComponent = DaggerAppComponent.builder().appModule(new AppModule()).build();
public static AppComponent get() {
return appComponent;
}
}
#Component(modules = {AppModule.class, ApiModule.class, MvpModule.class})
#Singleton
public interface AppComponent {
void inject(BaseActivity activity);
}
Api
#Singleton
#Component(modules = {ApiModule.class})
public interface ApiComponent {
void inject( BaseActivity activity);
}
#Module
public class ApiModule {
#Provides
#Singleton
public ApiClient getApiClient(){
return new ApiClient();
}
}
Mvp
#Singleton
#Component(modules = {MvpModule.class})
public interface MvpComponent {
void inject(BaseActivity activity);
}
#Module
public class MvpModule {
#Provides
#Singleton
public MvpPresenter getMvpPresenter(){ return new MvpPresenter();}
}
Error:(16, 10) error: ApiClient cannot be provided without an #Inject constructor or from an #Provides- or #Produces-annotated method. This type supports members injection but cannot be implicitly provided.
ApiClient is injected at
...BaseActivity.ApiClient
...BaseActivity is injected at
MvpComponent.inject(activity)
I found out my problem. I needed to use a subcomponent.
#Singleton
#Subcomponent(modules = {MvpModule.class})
public interface MvpComponent {
void inject(BaseActivity activity);
}
#Module
public class MvpModule {
#Provides
#Singleton
public MvpPresenter getMvpPresenter(){ return new MvpPresenter();}
}
see
Dagger- Should we create each component and module for each Activity/ Fragment
Dagger2 activity scope, how many modules/components do i need?

How inject the Activity to object that is being injected to the Activity?

I have this module:
#Module
public class UserProfileModule {
#Provides
#Singleton
UserProfileController providesUserProfileController() {
return new UserProfileController();
}
}
and this component:
#Component(modules = {UserProfileModule.class})
#Singleton
public interface AppComponent {
void inject(UserProfileActivity activity);
}
So far, in My UserProfileActivity I can #Injectan UserProfileController. But now, I need to inject the UserProfileActivity to the controller. I mean, inject each other.
I could do it by calling a UserProfileController setter in UserProfileActivity: setActivity(this);, but it would be nice if can be automatic.
How can achieve that?
Thanks.
For starters: add it to the constructor. Then declare that dependency.
#Provides
#Singleton
UserProfileController providesUserProfileController(UserProfileActivity activity) {
return new UserProfileController(activity);
}
After doing so dagger will complain about not being able to provide UserProfileActivity unless you already do so. If you don't, add another module, or just provide the dependency from that same module. The actual implementation follows, first we need to fix your code.
#Singleton is a dependency on top of the hierarchy. You can't—or at least should not—have an activity dependency for a #Singleton annotated object, since this will probably cause bad smells and/or memory leaks. Introduce a custom scope #PerActivity to use for dependencies within your activities lifespan.
#Scope
#Retention(RUNTIME)
public #interface PerActivity {}
This will allow for correct scoping of the object. Please also refer to some tutorials about dagger, since this is a really important issue and covering everything in a single answer would be too much. e.g. Tasting dagger 2 on android
The following uses the latter approach of the aforementioned 2 options by expanding your module:
#Module
public class UserProfileModule {
private final UserProfileActivity mActivity;
public UserProfileModule(UserProfileActivity activity) {
mActivity = activity;
}
#Provides
#PerActivity
UserProfileActivity provideActivity() {
return mActivity;
}
#Provides // as before
#PerActivity
UserProfileController providesUserProfileController(UserProfileActivity activity) {
return new UserProfileController(activity);
}
}
If you now use your component Builder you can create a new instance of your module with the activity as an argument. The dependency will then correctly be supplied.

Dagger 2 - modules from different components

I am not quite sure how to solve this with dagger 2.
Lets assume we have ApplicationModule that provides us ApplicationContext
then we have ApplicationComponent that uses just this one module.
Then on top of it we have ActivityModule and ActivityComponent that has dependency on ApplicationComponent.
ActivityComponent is build just like
ApplicationComponent component = ((MyApplication) getApplication()).getComponent();
mComponent = Dagger_ActivityComponent.builder()
.applicationComponent(component)
.activityModule(new ActivityModule(this))
.build();
And then I inject my activity:
mComponent.inject(this);
Now I am able to use everything that is declared inside my ActivityModule, however it is not possible for me to access ApplicationModule.
So the question is how could that be achieved? So that when I build component that depends on another component I can still access module from the first one?
EDIT
I think I have found solutions, after rewatching Devoxx talk by Jake again, I have had to miss that out, whatever I want to use from another components module I have to provide in that component, for example I want to use Context from ApplicationModule then inside ApplicationComponent I have to state Context provideContext(); and it is going to be available. Pretty cool :)
You have already answered your question, but the answer is to specify the provision methods in your "superscoped" component (ApplicationComponent).
For example,
#Module
public class ApplicationModule {
#Provides
#Singleton
public Something something() {
return new Something.Builder().configure().build();
// if Something can be made with constructor,
// use #Singleton on the class and #Inject on the constructor
// and then the module is not needed
}
}
#Singleton
#Component(modules={ApplicationModule.class})
public interface ApplicationComponent {
Something something(); //PROVISION METHOD. YOU NEED THIS.
}
#Scope
#Retention(RetentionPolicy.RUNTIME)
public #interface ActivityScope {
}
#ActivityScope
public class OtherThing {
private final Something something;
#Inject
public OtherThing(Something something) {
this.something = something;
}
}
#Component(dependencies = {ApplicationComponent.class})
#ActivityScope
public interface ActivityComponent extends ApplicationComponent { //inherit provision methods
OtherThing otherThing();
}

Categories

Resources