@ContributesAndroidInjector module support
See original GitHub issueI am using the dagger2 android injector helper classes and would like to provide mocks of some objects in the ApplicationModule (not shown). However, the module is abstract as dagger generates the activity injector code so I cannot create an instance of the module in my mock TestRule to call the constructor of DaggerMock. Is there a way to mock this without adding an extra subcomponent?
ApplicationModule.java
@Module
@SuppressWarnings("WeakerAccess")
public abstract class ApplicationModule {
@PerActivity
@ContributesAndroidInjector(modules = ActivityModule.class)
abstract BaseActivity contributeBaseActivityInjector();
...
}
Here is my application component, which uses a builder
ApplicationComponent.java
@Singleton
@Component(modules = {ApplicationModule.class, AndroidInjectionModule.class})
public interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(TheApplication app);
ApplicationComponent build();
}
Here is the application class.
TheApplication.java
public class TheApplication extends Application implements
HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> mDispatchingAndroidInjector;
private ApplicationComponent mApplicationComponent;
@Override
public void onCreate() {
super.onCreate();
getComponent().inject(this);
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return mDispatchingAndroidInjector;
}
public ApplicationComponent getComponent() {
if (mApplicationComponent == null) {
mApplicationComponent = DaggerApplicationComponent.builder()
.application(this)
.build();
}
return mApplicationComponent;
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:7 (3 by maintainers)
Top Results From Across the Web
ContributesAndroidInjector - Dagger
Generates an AndroidInjector for the return type of this method. The injector is implemented with a Subcomponent and will be a child of...
Read more >Dagger 2 with ContributesAndroidInjector - Is it easy? - Medium
android.support framework classes. This module should be installed in the root-most component which will use these types. In Simple terms we use ...
Read more >Dagger 2 Annotations: @Binds & @ContributesAndroidInjector
In this article we will briefly look at two annotations : @Binds and @ContributesAndroidInjector. It is not mandatory to have read the…
Read more >android - How to generate module consisting ... - Stack Overflow
The error you are getting is because @Binds and @ContributesAndroidInjector methods must be abstract because they don't have method bodies. That ...
Read more >ContributesAndroidInjector module for a BaseActivity #851
This type supports members injection but cannot be implicitly provided. Have @ContributesAndroidInjector support for this? //Builder @module
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@RoRoche the new dagger-android library is not supported in current version but I am working to support it (at least for replacing objects defined in application component). I hope I’ll release a new release soon.
I have pushed a new version to support Dagger Android (you can try it using version 0.7.1-beta1). You must add an invocation to method
customizeBuilderto set the application, an example is available here. In this version Dagger Android is supported in Espresso test to replace objects defined in Application component, I’ll continue to investigate to find a way to support it in JUnit tests and for subcomponents and dependent components.