https://kotlinlang.org logo
#dagger
Title
# dagger
m

Marco Righini

06/12/2020, 2:05 PM
Hi! First thing, amazing work with Hilt 😉 Just two questions: • There are some situations in which you need to manually recreate the application dependencies graph and then inject the application again. Is there a way to do it with Hilt? ApplicationComponent is managed by the framework so it seems not. I mean, there’s a way to disable the automatic component creation and app injection in the @HiltAndroidApp and do it manually? • Let’s say I want to maintain the old dagger android way for the activities that I already have. How do I have to deal with AndroidSupportInjectionModule? It’s autogenerated so no way to add @InstallIn(ApplicationComponent::class) Side note: https://codelabs.developers.google.com/codelabs/android-dagger-to-hilt/index.html?index=..%2F..index#4 seems outdated
m

Manuel Vivo

06/12/2020, 2:21 PM
Regarding the migration, you can have the
AndroidSupportInjectionModule
included in a Hilt module
Copy code
@InstallIn(ApplicationComponent::class)
@Module(includes = [AndroidSupportInjectionModule::class]
interface DaggerAndroidModule {}
and then, to tell
dagger.android
to use Hilt’s ApplicationComponent, you can follow the guide here https://dagger.dev/hilt/migration-guide#daggerandroid-application
I’m curious about your first question. What’s your use case? Why would you want to do that?
m

Marco Righini

06/12/2020, 2:24 PM
thanks!! Need to do kind of in-app restart, and it requires to clean app and reinitialize the state of some dependencies
w

wasyl

06/12/2020, 2:52 PM
We also do the first one in UI tests. We don’t want to use Orchestrator as it slows them down a lot, but between tests we still want to reset as much of the application state as possible
m

Manuel Vivo

06/12/2020, 2:55 PM
For UI tests you don’t have to worry because you’ll get a fresh set of components for each test with
@HiltAndroidTest
and the
HiltAndroidRule
👌 2
For the in-app case, it’s not possible to reset the state of the
ApplicationComponent
. The
ApplicationComponentManager
holds a unique instance of it. I’d pay close attention to the
@Singleton
s of your app and reset state manually.
m

Marco Righini

06/12/2020, 5:05 PM
@Manuel Vivo I think creating a custom component and a scope, and then changing singleton to this new scope will allow me to recreate the graph.
Creating a custom component with application component as parent seemed to fix. But then when started migrating from dagger android to AndroidEntryPoint, android class injection fails because standard Hilt components don't see the custom component (as pointed out in the doc)
m

Manuel Vivo

06/13/2020, 6:34 PM
That’s because the rest of the Hilt components don’t have your custom component as a parent. It’s not possible to insert a custom component in the middle of the hierarchy
m

Mike Nakhimovich

06/14/2020, 8:54 PM
It would be a lot cooler if this was possible 😊
m

Marco Righini

06/15/2020, 12:27 PM
Is it in Hilt roadmap?
m

Manuel Vivo

06/18/2020, 11:13 AM
Unfortunately, it’s not at the moment. It’s technically very difficult to implement
4 Views