The place where random ideas get written down and lost in time.

2020-05-02 - Dagger and Libraries

Category DEV

Here’s an interesting problem in Cab v2:

  • Main app: Main app component.
  • WiThrottle library: Lib component.

The design I want is for the library to be an extension to the main app. The library provides components/modules that the main app can use and inject. The library itself cannot use anything from the app (since it is app-agnostic).

In terms of components/scopes: the library is the outer component, and the app is a sub-component of the library. The app can use elements provided by the library.

When using component dependencies, which one depends on which?

I think that means the app component has a dependency on the lib component.

This seems to work with some caveats:

  • @Singleton @Component AppComponent // dependencies = { LibComponent.class }

Issue:

  • At build time “dagger AppComponent scoped may not reference bindings with different scopes”.
  • ⇒ Two ways to fix that: either remove all scoping info from the dependencies component and everything provided; Can’t even be in the same @Singleton. Obviously that’s not right.
  • ⇒ Both components can have and likely should have different scopes. However the “root” component must provide everything explicitly by using getters in the component.

E.g.:

@Component LibComponent {
 ExportedType1 getExportedType1();
 ExportedType2 getExportedType2();
}
Etc

Module providers in the LibComponent are not exported to the dependent component unless explicitly stated that way. If something needs an explicit module provider, it also needs an “exporter” declaration in the component. If something is not explicitly exported by the root component, dagger will try to use the type as-is, notice the scope discrepancy and basically ask for either a different scope or an explicit provider if the object has no scope.


 Generated on 2025-01-03 by Rig4j 0.1-Exp-f2c0035