Dagger 2.10 / 2.11 injection

I try, unsuccessfully, to inject an Activity into the ViewUtils class. I followed several different posts, but I canโ€™t understand what I am missing in my implementation.

I know that this is probably a repetition of the posts below, and I really apologize for that, but I honestly don't see what I am missing. These are the posts I found:

My implementation is as follows:

AppComponent

@Component(modules = { AppModule.class, AndroidSupportInjectionModule.class, ActivityBindingModule.class }) @Singleton public interface AppComponent extends AndroidInjector<EmblyApp> { @Component.Builder abstract class Builder extends AndroidInjector.Builder<EmblyApp> {} } 

ActivityBindingModule

 @Module public abstract class ActivityBindingModule { @ContributesAndroidInjector abstract LoginActivity loginActivity(); } 

LoginSubcomponent

 @Subcomponent(modules = LoginSubcomponent.LoginActivityModule.class) public interface LoginSubcomponent extends AndroidInjector<LoginActivity> { @Subcomponent.Builder abstract class Builder extends AndroidInjector.Builder<LoginActivity> {} @Module abstract class LoginActivityModule { @Binds abstract Activity bindActivity(LoginActivity activity); @Provides @ActivityScope static ViewUtils viewUtils(Activity activity) { return new ViewUtils(activity); } } } 

ViewUtils

 public class ViewUtils { private final Activity activity; @Inject public ViewUtils(Activity activity) { this.activity = activity; } } 

And the error I get is:

 Error:(14, 22) error: [dagger.android.AndroidInjector.inject(T)] android.app.Activity cannot be provided without an @Inject constructor or from an @Provides-annotated method. android.app.Activity is injected at com.emblyapp.app.ui.helpers.ViewUtils.<init>(activity) com.emblyapp.app.ui.helpers.ViewUtils is injected at com.emblyapp.app.ui.authentication.login.LoginActivity.viewUtils com.emblyapp.app.ui.authentication.login.LoginActivity is injected at dagger.android.AndroidInjector.inject(arg0) 

What is wrong here? Thanks for the help!

Edit: I forgot to mention that my LoginActivity has an injection with AndroidInjection

 @Override protected void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); } 
+7
android dependency-injection dagger dagger-2
source share
1 answer

As stated in the documentation for the android dagger :

Pro-tip: If your subcomponent and its builder do not have methods or supertypes other than those specified in step 2, you can use @ContributesAndroidInjector to generate them for you. Instead of steps 2 and 3, add an abstract module method that returns your activity, annotate it with @ContributesAndroidInjector, and indicate the modules you want to install in the subcomponent. If the subcomponent needs cloud cover, also apply region annotations to the method.

Thus, we can get rid of LoginSubcomponent and make the following changes to the ActivityBindingModule :

 @Module public abstract class ActivityBindingModule { @ActivityScope @ContributesAndroidInjector(modules = LoginActivityModule.class) abstract LoginActivity loginActivity(); }
@Module public abstract class ActivityBindingModule { @ActivityScope @ContributesAndroidInjector(modules = LoginActivityModule.class) abstract LoginActivity loginActivity(); } 

LoginActivityModule.java

 @Module abstract class LoginActivityModule { @Binds abstract Activity bindActivity(LoginActivity activity); @Provides @ActivityScope static ViewUtils viewUtils(Activity activity) { return new ViewUtils(activity); } }
@Module abstract class LoginActivityModule { @Binds abstract Activity bindActivity(LoginActivity activity); @Provides @ActivityScope static ViewUtils viewUtils(Activity activity) { return new ViewUtils(activity); } } 

Your own application class:

 public class MyApp extends DaggerApplication { @Inject DispatchingAndroidInjector dispatchingActivityInjector; @Override protected AndroidInjector applicationInjector() { return DaggerAppComponent.builder().create(this); } }
public class MyApp extends DaggerApplication { @Inject DispatchingAndroidInjector dispatchingActivityInjector; @Override protected AndroidInjector applicationInjector() { return DaggerAppComponent.builder().create(this); } } 
+9
source share

All Articles