Dagger2 injection across modules replace implementation of Interface

  Kiến thức lập trình

I am breaking up my project into different Android Studio modules. At the base, I have a headless modules with core business logic. Down the module dependency tree, I have an app modules that encompasses the Android Application.

In the core logic, I am using dependency injection for things like logging/error handling. In my core module, there is a good amount of code that leverages the interface I am trying to log to, so I am using an EntryPoint to provide the Logging class.

In the app module, I have the “real” logging code that will handle sending info to the server. So I really want to “inject” THAT implementation when I build the entire project so that all the code gets the real one.

I have searched and read a number of articles on Dagger2, modules, binding, and componenents but I have not found a way to allow the the core module to compile my core module (outside of using Optional) while using my real implementation in the application.

In a perfect world, I can see two solutions:

  1. Try to ignore the dependency injection in the core module knowing it will be provided in the app module. I saw a library prop in Dagger 1 that appeared to do this.
  2. Provide a dummy implementation of the interface in the core module and then swap it out in the app module for the real implementation when I build the apk. I think this MIGHT be possible with subcomponents but haven’t had luck…

Any help would be really appreciated!

New contributor

Brian Bauman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website