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

Last update March 13, 2023 at 06:43 am

You are using Dagger 2 then at compile time you find error: [Dagger / MissingBinding] cannot be provided without an @ Provides-annotated method. What to do to solve it?

Article sponsored by Zetaplus. You will find projects ofapplications and websites created by our service in our portfolio

Engineering software is an area that has its own unlikely circumstances. If in theory, the architecture of the application seems perfect for the project, 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.

Recently in the development of a android application very modular (thanks to our great architecture) we encountered a problem. The project being centered on the use of the framework Dagger to design this architecture, we found the 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.

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

For this problem there are several approaches of solutions also different from each other. We will therefore not quote them here. Ours is not easy to find elsewhere, that's why we exhibit it on tedidev.com.

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 a compilation error.

Sub-component A:

@Subcomponent (modules = [AModule :: class]) interface AComponent {@ Subcomponent.Factory interface Factory {fun create (): AComponent}

fun inject (activity: MainActivity)
}

Sub component B:

@Subcomponent (modules = [BModule :: class]) interface BComponent {@ Subcomponent.Factory interface Factory {fun create (): BComponent}

fun inject (activity: MainActivity)
}

In case you are stressed out about the language, this is kotlin. Our core development language.

Even if you only inject one two into the MainActivity class, and you put the other out completely, the error will just persist.

The solution is to remove one of them. At best the inject method of the two components should have completely different parameter types.

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.

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 application of modules. They are used precisely for that.

You want to use component A and B dependencies in a class to take advantage of their full powers. We propose to create a C component which will use the module of A and B. The solution would look like this

Sub component C:

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

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

fun inject (activity: MainActivity)

}

Be careful though with the yellow line. Consider changing or removing the inject method (in yellow) in components A and B.

We hope we helped you with this article. Also being 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.

Subscribe to our newsletter

* Indicates required

Conclusion

Maybe you will like Troubleshooting: java.lang.ClassCastException: android.app.Application cannot be cast to MyApplication et Oneplus unveils OxygenOS 11

The best open source code editors you should definitely discover

Leave comments

Your email address will not be published. Required fields are marked with *

18 + 16 =