Sorry for my english, now i begin learn dagger2 and i cant understand why i have error:
Error:(9, 10) error:
test.dagger.dagger.modules.MainActivityPresenterModule cannot be
provided without an #Inject constructor or from an #Provides- or
#Produces-annotated method.
test.dagger.dagger.modules.MainActivityPresenterModule is injected at
test.dagger.view.activitys.MainActivity.mainActivityPresenterModule
test.dagger.view.activitys.MainActivity is injected at
test.dagger.dagger.components.AppComponent.injectMainActivity(mainActivity)
App
public class App extends Application {
private static AppComponent component;
#Override
public void onCreate() {
super.onCreate();
component = DaggerAppComponent.create();
}
public static AppComponent getComponent() {
return component;
}
}
AppComponent
#Component(modules = {MainActivityPresenterModule.class})
public interface AppComponent {
void injectMainActivity(MainActivity mainActivity);
}
MainActivityPresenterModule
#Module
public class MainActivityPresenterModule {
#Provides
MainActivityPresenter provideActivityPresenter(NetworkUtils networkUtils) {
return new MainActivityPresenter(networkUtils);
}
#Provides
NetworkUtils provideNetworkUtils(){
return new NetworkUtils();
}
}
NetworkUtils
public class NetworkUtils {
public boolean isConnection() {
return true;
}
}
MainActivityPresenter
public class MainActivityPresenter {
NetworkUtils networkUtils;
public MainActivityPresenter(NetworkUtils networkUtils) {
this.networkUtils = networkUtils;
}
public void getUser(){
if(networkUtils.isConnection()) {
Log.e("getUser", "getUser");
} else {
Log.e("no internet", "no internet connection");
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
#Inject
MainActivityPresenterModule mainActivityPresenterModule;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App.getComponent().injectMainActivity(MainActivity.this);
}
}
You can inject only the things that are provided in the classes annotated with #Module (only methods inside that module which are annotated with #Provides). So, you can do #Inject MainActivityPresenter presenter, for instance, not try to inject the whole module, like you tried to do. Modules should be registered on Dagger initialisation, like this (in App#onCreate)
component = DaggerAppComponent.builder()
.mainActivityPresenterModule(MainActivityPresenterModule())
.build()
In MainActivity you only need to call inject to be able to inject your #Inject MainActivityPresenter presenter or any other injects defined in the module, like so:
#Inject MainActivityPresenter presenter
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
(application as App).component.inject(this)
// after #inject(this) above you can start using your injections:
presenter.getUser()
}
Sorry, I wrote code snippets in Kotlin as it was much less to write that way, hopefully you get the idea how it looks in Java.
Related
I have the following (simplified) setup of 1 Application and 1 Activity, 1 Parent Component and 1 Subcomponent:
Application:
public class ExampleApp extends Application {
private AppComponent component;
#Override
public void onCreate() {
super.onCreate();
component = DaggerAppComponent.create();
}
public AppComponent getAppComponent() {
return component;
}
}
AppComponent:
#Singleton
#Component(modules = AppModule.class)
public interface AppComponent {
ActivityComponent.Builder getActivityComponentBuilder();
}
Activity Subcomponent:
#PerActivity
#Subcomponent(modules = {ActivityModule1.class, ActivityModule2.class})
public interface ActivityComponent {
void inject(MainActivity mainActivity);
#Subcomponent.Builder
interface Builder {
#BindsInstance
Builder binding1(#Named("binding1") int binding1);
#BindsInstance
Builder binding2(#Named("binding2") int binding2);
ActivityComponent build();
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
#Inject
TheObjectIWant theObjectIWant1, theObjectIWant2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityComponent component = ((ExampleApp) getApplication()).getAppComponent()
.getActivityComponentBuilder()
.binding1(100)
.binding2(200)
.build();
component.inject(this);
theObjectIWant1.doStuff();
theObjectIWant2.doStuff();
}
}
I would now like to refactor this same example to use the Module.subcomponents attribute instead of the Subcomponent.Builder factory method.
For this, I remove getActivityComponentBuilder() from the AppComponent and add the Module.subcomponents attribute to the AppModule:
AppComponent (new):
#Singleton
#Component(modules = AppModule.class)
public interface AppComponent {
}
AppModule (new):
#Module(subcomponents = ActivityComponent.class)
public abstract class AppModule {
[...]
}
My question is pretty simple. How do I now get the ActivityComponent.Builder into the MainActivity? I can't constructor inject the MainActivity, so there's only really field injection left:
MainActivity (new, theory):
public class MainActivity extends AppCompatActivity {
#Inject
ActivityComponent.Builder activityComponentBuilder
#Inject
TheObjectIWant theObjectIWant1, theObjectIWant2;
((ExampleApp) getApplication()).getAppComponent().inject(this)
[...]
}
But this will not work, since the AppComponent can't inject theObjectIWant1 and theObjectIWant1. They only come from the ActivityComponent.
So is this approach just not suitable here? Since this is more of a theoretical question, please ignore dagger.android here and assume we want to handle this without it.
I am using Dagger2 for dependency injection in quite a large project.
There are 2 different sections of the app that each use multiple usecases.
For each section I have a module that provides the dependencies for that section. I have come across a scenario where I need to use a usecase from moduleA in ModuleB and I am not sure how to include it or add it to be used for that one scenario.I have looked at other answers on stackoverflow but have not found one that answers my question.
For example this answer here
does not help me because it's setup is different and I cannot deduce from this solution how to solve my problem.
and here
I am not certain that I should make any of these dependencies into Singletons.
for example I have ModuleA below that contains AddItem that I need in ModuleB PayForItem
#Module
public class ModuleA {
private final FragmentActivity mActivity;
public ModuleA(FragmentActivity fragmentActivity) {
mActivity = fragmentActivity;
}
...
#Provides
AddItem addItemPresenter(DateMapper dataMapper, ItemdetailUseCase itemDetailUsecase){
return new AddItem(dataMapper,itemDetailUsecase);
}
#Module
public class ModuleB {
private final FragmentActivity mActivity;
public ModuleB(FragmentActivity fragmentActivity) {
mActivity = fragmentActivity;
}
...
#Provides
PayForItem payForItemPresenter(AddItem addItem){
return new AddItem(addItem);
}
Each module is included in its own subcomponent eg:
#Subcomponent(modules = {ModuleA.class})
public interface ModuleAPresentationComponent {
void inject(Fragment testFragment;
}
#Subcomponent(modules = {ModuleB.class})
public interface ModuleBPresentationComponent {
void inject(Fragment testFragment;
}
Both are included in the main component eg:
#Singleton
#Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
public ModuleAPresentationComponent newPresentationComponent(ModuleAPresentationModule presentationModule);
public ModuleBPresentationComponent newPresentationComponent(ModuleBPresentationModule presentationModule);
}
I have a BaseFragment that I use to inject eg:
public abstract class BaseFragment extends Fragment {
private boolean mIsInjectorUsed;
protected static final String PARAM_USER_ID = "param_user_id";
private ApplicationComponent getApplicationComponent() {
return ((AndroidApplication) getActivity().getApplication()).getApplicationComponent();
}
#UiThread
protected ModuleAPresentationComponent getModuleAPresentationComponent() {
if (mIsInjectorUsed) {
throw new RuntimeException("there is no need to use injector more than once");
}
mIsInjectorUsed = true;
return getApplicationComponent()
.newPresentationComponent(new ModuleAPresentationModule(getActivity()));
}
#UiThread
protected ModuleBPresentationComponent getModuleBPresentationComponent() {
if (mIsInjectorUsed) {
throw new RuntimeException("there is no need to use injector more than once");
}
mIsInjectorUsed = true;
return getApplicationComponent()
.newPresentationComponent(new ModuleBPresentationModule(getActivity()));
}
So in my fragment it injects like this:
public class testFragment extends BaseFragment{
#Inject AddItem addItemPresenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getModuleAPresentationComponent().inject(this);
}
}
How would I use AddItem from ModuleA in PayForItem ModuleB without repeating those dependencies in ModuleB?
I am trying to use Dagger 2 to inject my application class, MyApplication as I use it in various places. This is my setup using Dagger 2.11
MyApplication.java
public class MyApplication extends Application implements HasActivityInjector {
#Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
#Override
public void onCreate() {
super.onCreate();
AppInjector.init(this);
}
#Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
AppInjector.java
public class AppInjector {
public static void init(MyApplication application){
//Initialize dagger and inject the application
DaggerAppComponent.builder().application(application).build().inject(application);
application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle aBundle) {
handleActivity(activity);
}
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
}
#Override
public void onActivityPaused(Activity activity) {
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle aBundle) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
}
private static void handleActivity(Activity activity){
if(activity instanceof HasSupportFragmentInjector ){
AndroidInjection.inject(activity);
}
if (activity instanceof FragmentActivity){
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentManager.FragmentLifecycleCallbacks() {
#Override
public void onFragmentCreated(FragmentManager fm, Fragment f,
Bundle savedInstanceState) {
if (f instanceof Injectable) {
Log.i("LifecycleCallbacks", "injected:" + f);
AndroidSupportInjection.inject(f);
}
}
}, true);
}
}
AppComponent.java
#Singleton
#Component(modules = {
AndroidInjectionModule.class,
ActivityBuilder.class,
AppModule.class
})
public interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance Builder application(Application application);
AppComponent build();
}
void inject(MyApplication application);
}
However, every time I try use #Inject MyApplication application in a constructor, dagger throws an error that it has no way to provide it without an #Provides
Furthure more, I am not sure I should be using the Application everywhere, and rather only its Context? If so, how would I provide the Context?
Have a look at your Builder...
#Component.Builder
interface Builder {
--> #BindsInstance Builder application(Application application);
AppComponent build();
}
All Dagger knows about is your Application, you're never mentioning MyApplication, hence injecting it will fail.
I don't know why you'd have to inject MyApplication specifically, but the easiest solution would be to change it to bind your MyApplication instead...
#Component.Builder
interface Builder {
#BindsInstance Builder application(/** --> */ MyApplication application);
AppComponent build();
}
Then Dagger knows about MyApplication but not of Application. To fix this, you can just add a module that binds the other types which is easy enough because you have the subtype...e.g.
#Module interface AppModule { // could also be an abstract class
#Binds Application bindApplication(MyApplication application);
// if you also want to bind context
#Binds Context bindContext(MyApplication application);
}
And just add this module to your component.
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 provideNetworkService() {
return new NetworkService(BASE_URL);
}
#Provides
#Singleton
SharedPreferences provideSharedPreferences(Application app) {
return PreferenceManager.getDefaultSharedPreferences(app);
}
}
PrefsModel.java:
#Module(includes = DataModule.class)
public class PrefsModel {
#Provides
#Singleton
QueryPreferences provideQuery(SharedPreferences prefs) {
return new QueryPreferences(prefs);
}
}
AppComponent.java (I'm exposing QueryPreferences object since I need it in a presenter, hopefully is correct in this way):
#Singleton
#Component(modules = {AppModule.class, DataModule.class, PrefsModel.class})
public interface AppComponent {
void inject(HomeFragment homeFragment);
QueryPreferences preferences();
NetworkService networkService();
}
Then I have the FwApplication.java:
public class FwApplication extends Application {
private static final String TAG = "FwApplication";
private NetworkService mNetworkService;
private AppComponent mDataComponent;
#Override
public void onCreate() {
super.onCreate();
buildComponentAndInject();
}
public static AppComponent component(Context context) {
return ((FwApplication) context.getApplicationContext()).mDataComponent;
}
public void buildComponentAndInject() {
mDataComponent = DaggerComponentInitializer.init(this);
}
public static final class DaggerComponentInitializer {
public static AppComponent init(FwApplication app) {
return DaggerAppComponent.builder()
.appModule(new AppModule(app))
.dataModule(new DataModule())
.build();
}
}
}
Finally I added another module for the presenters:
#Module
public class PresenterModule {
#Provides
Presenter<FwView> provideHomePresenter(NetworkService networkService) {
return new HomePresenterImpl(networkService);
}
#Provides
Presenter<FwView> provideSearchPresenter(NetworkService networkService) {
return new SearchPresenterImpl(networkService);
}
}
And the following component (which returns error because I cannot add a scoped dependencies here):
#Component(dependencies = AppComponent.class, modules = PresenterModule.class)
public interface PresenterComponent {
void inject(HomePresenterImpl presenter);
}
So, I have few questions that are not clear for me reading the documentation online:
How can I fix the error in the presenter component since it depends on NetworkService which is a singleton defined in the AppComponent?
I have an HomeFragment which should implement the HomePresenter with "new HomePresenter(networkService)" but now I don't know how to use the DI defined
EDIT - FIX:
HomeFragment.java:
public class HomeFragment extends Fragment {
private static final String TAG = "FW.HomeFragment";
#Inject
HomePresenterImpl mHomePresenter;
public static HomeFragment newInstance() {
return new HomeFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FwApplication.component(getActivity()).inject(this);
}
Then I modified the presenter constructor in this way:
#Inject
public HomePresenterImpl(NetworkService networkService) {
mNetworkService = networkService;
mInteractor = new InteractorImpl(mNetworkService);
}
Then NetworkService is injected automatically.
I was wondering if it is correct in this way since I have to call for every fragment I have that needs a presenter constructed in the same way as the one above the following code:
FwApplication.component(getActivity()).inject(this);
You are mixing thing up. To provide your presenter, you should switch to something like the following:
Use constructor injection if possible. It will make things much easier
public class HomePresenterImpl {
#Inject
public HomePresenterImpl(NetworkService networkService) {
// ...
}
}
To provide the interface use this constructor injection and depend on the implementation:
Presenter<FwView> provideHomePresenter(HomePresenterImpl homePresenter) {
return homePresenter;
}
This way you don't have to call any constructors yourself. And to actually inject the presenter...
public class MyFragment extends Fragment {
#Inject
Presenter<FwView> mHomePresenter;
public void onCreate(Bundle xxx) {
// simplified. Add your modules / Singleton component
PresenterComponent component = DaggerPresenterComponent.create().inject(this);
}
}
This way you will inject the things. Please read this carefully and try to understand it. This will fix your major problems, you still can not provide 2 presenters of the same type from the same module (in the same scope)
// DON'T
#Provides
Presenter<FwView> provideHomePresenter(NetworkService networkService) { /**/ }
#Provides
Presenter<FwView> provideSearchPresenter(NetworkService networkService) { /**/ }
This will not work. You can not provide 2 objects of the same kind. They are indistinguishable. Have a look at #Qualifiers like #Named if you are sure this is the way you want to go.
You do not have to provide Presenter if #Inject annotation is used in the constructor. #Inject annotation used in the constructor of the class makes that class a part of dependencies graph. So, it also can be injected when needed.
On the other hand, if you add #Inject annotation to fields, but not to constructors, you have to provide that class.
i'm trying to use Dagger 2 into my apps but i'm having some problems regarding the entities repository and i haven't figured it out what i'm missing.
Here is my Application Component:
#Singleton
#Component(
modules = {
AndroidModule.class,
RepositoryModule.class
}
)
public interface ApplicationComponent {
void inject(AndroidApplication app);
IDependenceyRepository dependencyRepository();
}
My modules:
#Module
public class RepositoryModule {
#Provides #Singleton IDependenceyRepository provideDependendencyRepository(Context context) {
return new DependencyRepository(context);
}
}
#Module
public class AndroidModule {
private final AndroidApplication app;
public AndroidModule(AndroidApplication app) {
this.app = app;
}
#Provides #Singleton Context provideApplicationContext() {
return app;
}
}
My AndroidApplication:
public class AndroidApplication extends Application {
private ApplicationComponent component;
#Override
public void onCreate() {
super.onCreate();
setupGraph();
}
private void setupGraph() {
component = DaggerApplicationComponent.builder()
.androidModule(new AndroidModule(this))
.repositoryModule(new RepositoryModule())
.build();
component.inject(this);
}
public ApplicationComponent component() {
return component;
}
}
And here is my activity:
public class MainActivity extends Activity {
#Inject
IDependenceyRepository repository;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.testRepository();
}
private void testRepository() {
Dependency dep = new Dependency();
dep.setDescription("XXX");
dep.setName("AAAA");
try {
repository.save(dep);
Log.d("", repository.queryAll().get(0).getName());
} catch (SQLException e) {
e.printStackTrace();
}
}
What happens is that i'm getting a null pointer exception into the repository. He is not being injected.
If i add this line though:
repository = ((AndroidApplication) getApplication()).component().dependencyRepository();
It works, but the point of the DI it's not to have to worry about this, or am im wrong about that?
I've tried some other example and tutorials but haven't managed to solve my problem.
Thanks in advance,
david.mihola's comment is correct: in order to be able to have #Inject fields initialized, you need to have a method (typically void inject(MyClass)) in your component.
It's worth noting (not specifically for your question, but it could come up) that if you have a base class:
public class BaseActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((AndroidApplication) getApplication()).component().inject(this);
}
}
and in your component:
void inject(BaseActivity);
Injecting into a subclass that has it's own #Inject fields won't work:
public class ActualActivity extends BaseActivity {
#Inject IDependenceyRepository repo;
}
This is because Dagger needs to know the concrete classes that will be injected at compile time, not runtime (like Dagger 1 could do).