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); }
android dependency-injection dagger dagger-2
Peddro
source share