question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Allow Hilt to consume Dagger `@Component`s

See original GitHub issue

Background

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:

  1. 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--
  2. 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 @Components. 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:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gmazzocommented, Nov 21, 2021

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!

1reaction
danysantiagocommented, Nov 17, 2021

Hi - If you open source it I will gladly take a look.

Two things to remember:

  • Annotated your annotation API (@ImportDaggerComponent) with @GeneratesRootInput and remember to apply the hilt-compiler to the runtime project where this API is.
  • Annotate the generated @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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found