question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@ContributesAndroidInjector module support

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
fabioCollinicommented, Aug 30, 2017

@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.

1reaction
fabioCollinicommented, Sep 4, 2017

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 customizeBuilder to 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found