简体   繁体   中英

Dagger 2, How to add interface in dependency

I just started working with dagger 2. I have created dependency graph for application level dependency. Now that i wanted to create dependency that are required for a specific activity So i created another Component for activity then i created the Module and scope for that component. Now that when i am done writing all the code i build the project but i get compiler error which i am unable to solve.

Here is what i am doing.

@FeedsCatalogActivityScope
@Component(modules = FeedsCatalogActivityModule.class, dependencies = FeederApplicationComponent.class)

//My activity requires Catalog adapter so i am creating dependency for that
public interface FeedsCatalogActivityComponent {
    CatalogAdapter getCatalogAdapter();
}

Here is the Module

@Module
public class FeedsCatalogActivityModule {

    private final SelectedInterfaceListener selectedInterfaceListener;

    public FeedsCatalogActivityModule(SelectedInterfaceListener selectedInterfaceListener) {
        this.selectedInterfaceListener = selectedInterfaceListener;
    }

    @Provides
    @FeedsCatalogActivityScope
    public CatalogAdapter catalogAdapter(Picasso picasso, SelectedInterfaceListener mSelectesInterfaceListener) {
        return new CatalogAdapter(picasso, mSelectesInterfaceListener);
    }

}

Here is the scope

    @Scope
public @interface FeedsCatalogActivityScope {
}

So now when i build i get this error

/Users/Zeeshan/Desktop/personal/Feeder/app/src/main/java/io/droidninja/feeder/FeederApplication.java Error:(10, 31) error: cannot find symbol class DaggerFeederApplicationComponent /Users/Zeeshan/Desktop/personal/Feeder/app/src/main/java/io/droidninja/feeder/ui/activities/FeedsCatalogActivityComponent.java Error:(13, 20) error: io.droidninja.feeder.ui.adapters.SelectedInterfaceListener cannot be provided without an @Provides-annotated method. io.droidninja.feeder.ui.adapters.SelectedInterfaceListener is injected at io.droidninja.feeder.ui.activities.FeedsCatalogActivityModule.catalogAdapter(…, mSelectesInterfaceListener) io.droidninja.feeder.ui.adapters.CatalogAdapter is provided at io.droidninja.feeder.ui.activities.FeedsCatalogActivityComponent.getCatalogAdapter() Error:Execution failed for task ':app:compileDebugJavaWithJavac'. Compilation failed; see the compiler error output for details.

What i am understanding is that i have problem in FeedsCatalogActivityModule . How should i provide SelectedInterfaceListener ? It is a interface.

PS i am new to dagger2 i just get started with it.

Well, you must somehow provide an implementation for that interface. Take a look at:

Example

@Binds
abstract SelectedInterfaceListener provideDsListPresenter(SelectedInterfaceListenerImpl impl);

According to your code, you are suppose to pass an instance of SelectedInterfaceListener to FeedsCatalogActivityModule when your are building FeedsCatalogActivityComponent inside the activity that uses it.

in your activity create a method that setup the component like this

private void initializeInjector() {
    FeedsCatalogActivityComponent feedsCatalogActivityComponent = DaggerFeedsCatalogActivityComponent.builder()
            .feederApplicationComponent(feederApplicationComponent()/* create this method and it should return a correctly built  FeederApplicationComponent */ )
            .feedsCatalogActivityModule(
                new FeedsCatalogActivityModule(
                    new SelectedInterfaceListener() 
                    /* pass in the instance of SelectedInterfaceListener here */ )
                ).build(); 
}

And then remember to change the Module provides method to

@Provides
@FeedsCatalogActivityScope
public CatalogAdapter catalogAdapter(Picasso picassor) {
    return new CatalogAdapter(picasso, this.selectedInterfaceListener);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM