Feature Request: Allow Hilt to consume Dagger `@Component`s
See original GitHub issueBackground
In my company, we expose our internal Android libraries as Dagger @Components
. The rationally behind this pattern is that:
- we want to avoid conflicting binding (by exposing
@Modules
instead) - we want a clear declaration of each library’s dependencies
In Hilt docs, Custom Components section, you state:
Consider if a non-Hilt (regular Dagger) component is sufficient
Still that use case seems not to be supported (yet) because:
- I couldn’t find an API to property import a Dagger component (without re-writing all providers again). An equivalent to https://dagger.dev/api/2.22/dagger/Component.html#dependencies--
- If by using Hilt compiler, you declare a
@Module
without an@InstallIn
annotation (which will be the case for Dagger components), ~it fails~ (you can actually use@DisableInstallInCheck
for this)
Use case
Allow Hilt components to somehow consume (inherit its exposed providers) Dagger @Component
s. If this is supported already, I couldn’t find any documentation or example about it.
For instance, one way we may support this is by providing a component in a Hilt module:
@Module
@InstallsOn(SingletonComponent::class)
object AppModule {
// by providing an instance of `LoggerComponent`, Hilt will be smart enough to generate a provider `Logger` from it
@Provides
fun provideLoggerComponent(application: Application): LoggerComponent =
DaggerLoggerComponent.create(application)
}
class MainActivity : Activity {
@Inject
lateinit var logger: Logger
fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
logger.log("onCreate")
}
}
And from our library module:
@Component
interface LoggerComponent {
val logger: Logger
@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context)
}
}
interface Logger {
fun log(log: String)
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Hilt in multi-module apps - Android Developers
Hilt code generation needs access to all the Gradle modules that use Hilt. ... Use Dagger as usual in the feature module.
Read more >Understanding Dependency Injection with Hilt. - Nyame Bismark
Hilt allows you to use dependency Injection in your app and creates a container of the dependencies and automatically manages the lifecycle ...
Read more >Custom Components - Hilt - Dagger
For components with a limited purpose sometimes it is better to use a non-Hilt component. For example, consider a production component that represents...
Read more >Dagger Hilt Tutorial - Step by Step Guide - MindOrks
To just take care of where to inject dependencies and rest all of the code generations happens by dagger itself by using annotations...
Read more >All about Hilt. Get deeper understanding about Hilt.
Hilt is a dependency injection framework for Android that is built on top of Dagger 2. First of all, we have to understand...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Good enough. We’ll probably build it internally then, let it mature to cover our needs, and then we’ll try to open source it as a third-party Hilt extension. We’ll let you know. Thanks!
Hi - If you open source it I will gladly take a look.
Two things to remember:
@ImportDaggerComponent
) with@GeneratesRootInput
and remember to apply thehilt-compiler
to the runtime project where this API is.@Module
with@OriginatingElement
Both of these are explained here: https://dagger.dev/hilt/creating-extensions
Also, plz don’t use
@DaggerGenerated
, that is internal not meant to be use by external code gens.