[Dagger/MissingBinding] cannot be provided without an @Provides-annotated method

How to solve [Dagger/MissingBinding] cannot be provided without an @Provides-annotated method error.

You are still using Dagger 2 ( when other developer already move to hilt ) then at compile time you find this error.

error: [Dagger / MissingBinding] cannot be provided without an @ Provides-annotated method. 

How to solve it ? This is the subject of today post !

[Dagger/MissingBinding] cannot be provided without an @Provides-annotated method

Computer science particulary in software engineering  is an area that has its own unlikely circumstances.

If in theory, the architecture of your application seems perfect on paper, in execution, the quality of the product strongly depends on the technical level of the developer.

Especially on its ability to manage unforeseen events, such as bugs for which the solution is unknown.

These time, mobile android development are becoming more and more modular and thanks to our great architecture, we encountered more and more bug.

The project on which i worked were centered on the use of Dagger framework to design this architecture.

In compile time i got crash with this error message: ” [Dagger / MissingBinding] cannot be provided without an @ Provides-annotated method”.

After several unsuccessful searches on Internet, we investigated with brute force. Here is what we found.

Too many solutions

For this problem there are several approaches of solutions also different from each other. This is the usual bug message that is more confuse than needed.

My first experience was with C language when i got StackOverflow. Not the website, the real console error with messages displaying text mentionning StackOverflow error.

Like StackOverFlow , It can be anything in your code that cause this crash.

It’s the same with [Dagger / MissingBinding] cannot be provided without an @ Provides-annotated method. We will therefore not quote every solution that might solve this problem here.

What i found might not be found anywhere because at the time, i didn’t find it on other forum.

What cause the error

Suppose we have two subcomponents that need to build dependencies for an activity (which we will call MainActivity).

The error is raised if both subcomponents build dependencies for the same type of object. Here is an example of two components that could trigger this compilation error.

Sub-component A:

@Subcomponent(modules = [AModule::class])
interface AComponent {

@Subcomponent.Factory
interface Factory {
fun create(): AComponent
}

fun inject(activity: MainActivity) // yellow line
}

Sub component B:

@Subcomponent(modules = [BModule::class])
interface BComponent {

@Subcomponent.Factory
interface Factory {
fun create(): BComponent
}

fun inject(activity: MainActivity) // yellow line
}

This is kotlin code, my prefered android language

The problem here is that more than one component is injecting code in the same Activity.

I am talking about the yellow line. Not the color, i mean the line with the comment yellow line. That’s it.

Even if you only inject one of the component into MainActivity, and you put the other one out completely, the error will still persist.

Solution 1

The solution is to delete one of the component. At best the inject method of the two components should have different parameters type.

This kind of problem is so easy to avoid with tutorials and tests that it is difficult to realize that on Dagger 2, such an operation crashes the source code at compile time.

Solution 2

If I can’t use both components what can I do to take advantage of the dependencies I built in a class? –

The solution lies in the use of the power of modules. They are used precisely for that.

You want to inject component A and B dependencies in the same class to take advantage of their full powers.

What work for me is to delete component A and B and create a third component. Let call it component C which will use the module A and B. The solution should look like this

Sub component C:

@Subcomponent(modules = [AModule::class , BModule::class]) // Green line

interface CComponent {

@Subcomponent.Factory interface Factory {

fun create(): CComponent

}

fun inject(activity: MainActivity) // Yellow line


}

Be careful though with the yellow line. If you dont want to delete component A and B, change the inject method completly in components A and B.

The magic happen in the green line. Not the color, i mean the line with the comment green line. That’s it.

We hope we helped you with this article. Also as human beings, we can be wrong about certain points or even the understanding of the error which we are dealing with.

Do not hesitate to write to us if you have other proposals.

[Dagger/MissingBinding] cannot be provided without an @Provides-annotated method

Leave a Comment