Naseeb Sheoran
Naseeb Sheoran

Reputation: 473

Dagger 2 error: android.content.Context cannot be provided without an @Provides-annotated method

I am working on implementing dagger 2 in my project. For this, i have written below lines of code:

@Inject
VideoControllerView mediaController;

@Module
public class PlayerModule {

    @Provides
    VideoControllerView providesVideoControllerView(Context context, VideoControllerView.Controlers cntrls) {
        return new VideoControllerView(context, cntrls);
    }
}


@Component(modules = PlayerModule.class)
public interface PlayerComponent {
    VideoControllerView getVideoControllerView();
}

But when i am trying to compile my app, i am getting below given error:

Error:(14, 25) error: android.content.Context cannot be provided without an @Provides-annotated method.
android.content.Context is injected at
com.multitv.multitvplayersdk.module.PlayerModule.providesVideoControllerView(context, …)
com.multitv.multitvplayersdk.controls.VideoControllerView is provided at
com.multitv.multitvplayersdk.component.PlayerComponent.getVideoControllerView()

I have looked around on how to resolve this issue but of no avail. Please help me.

Upvotes: 25

Views: 19391

Answers (4)

Homayoon Ahmadi
Homayoon Ahmadi

Reputation: 2843

I used @ApplicationContext annotation to fix it:

@Inject
@ApplicationContext
lateinit var appContext: Context

Upvotes: 3

Sean Barbeau
Sean Barbeau

Reputation: 11756

In my case I was trying to migrate a DataModule.kt class from an app module to a library module, and I just had to add this method in DataModule.kt after the migration to enable it to find the context:

    @Provides
    fun provideContext(
        @ApplicationContext context: Context,
    ): Context {
        return context
    }

Upvotes: 6

SpyZip
SpyZip

Reputation: 5540

you need module like:

@Module
public class ContextModule {

    private Context context;

    public ContextModule(Context context) {
        this.context = context;
    }

    @Provides
    @UefaApplicationScope
    Context provideContext() {
        return context;
    }
}

and then method injection:

    @Provides
    @Inject
    VideoControllerView providesVideoControllerView(Context context, VideoControllerView.Controlers cntrls) {
        return new VideoControllerView(context, cntrls);
    }

Upvotes: 6

David Medenjak
David Medenjak

Reputation: 34552

Please try to understand the error.

PlayerModule.providesVideoControllerView(context, …) requires a context, but Dagger does not have access to any Context object, so it cannot pass in any Context. It thus creates the error, telling you that it can't find any Context and that you should add a way for it to do so.

Context cannot be provided without an @Provides-annotated method.

Hence...A context cannot be provided without a @Provides-annotated method.


Make sure that the component your PlayerModule is a part of has access to a context.

Either make it a subcomponent of your ApplicationComponent, add a Context to your PlayerModule, bind your Activity instance as a Context, etc

There's a lot of ways to do this, but in the end you require a method like the following in one of your modules:

@Provides Context provideContext() { return myContext; }

Once Dagger can find a Context the error will go away.

Upvotes: 12

Related Questions