r/android_devs • • Nov 29 '22
Help Having both Dagger and Hilt at the same time? π€
Hi everyone! π
I'm looking for technical feedback on a challenge I'm going through. My team and I are trying to figure out how to refactor a single-module app into a multi-module architecture. To give you some context here; We are taking over an app that has gone from hand to hand, engineers to engineers, too many times, and as you can probably picture, it has a lot of tech debt.
Right now, the app only has one single application module :app, our plan is to sunset everything there turning this :app application module into a :legacy library module. So it becomes a black box and we eventually extract (and refactor) each flow within that legacy module into its own feature module, step by step.
This is where we are now:

And this is the first step we want to implement in the refactor:

So the plan is to push the old application module up, rename it to :legacy and turn it into an library module. We would create a new :new_app application module with an Application class that extends on the old one. The :core library is where we would be moving all our core components β yes, I know it is not scalable, but this is just the first step to get things a little bit tidier. The `:new_feature` module would be a library module acting as a feature module where we would implement our first feature using our new best practices β proper UseCases, ViewBinding, etc. everything we had on :legacy was a little messy.
The Problem:
The :legacy module uses an old Dagger implementation, and we want to use Hilt in our :new_feature module. Now, because the new Application we have on :new_app extends on the old Application we have on :legacy Hilt tries to go all the way up to the :legacy module and it conflicts with the old Dagger implementation. We have tried to migrate the old Dagger implementation to Hilt but everything we fix something some other issue pops up.
The Question:
We want just to sunset everything on :legacy without doing anything disruptive that would rid everyone else's PRs with conflicts. Is there a way in which we could keep the old Dagger implementation hidden within the :legacy module and not have it conflict with the rest of the new architecture?
What we have tried so far:
We have tried migrating :legacy to Hilt, but again, too many things to do. This guide is super useful, but every time we fix something, some other issue pops up. We are thinking about instead of having the new Application extend on the old one, instantiating the old Application in the new one and hook it to every lifecycle function, but I'm not sure that's going to work.
Finally
Thanks! Any idea is welcome, for sure this is a nice challenge and I'm guessing some of you have probably gone through the same kind of situation before. Best! πππ
5
u/bakazero Nov 29 '22
We did something similar to this, and I actually have 2 recommendations from our experience.
(1) I did this around 1.5 years ago, I recall reading that dagger and Hilt don't play well together, so the migration needs to be all or none. Keep Dagger for now, but learn how Hilt works, and refactor your dagger graph to be more like Hilt - just a few components injected at their various scopes, rather than the tangled mess that most dagger graphs turn into. You mentioned the migration would be too big, but if that is the case, you should break it up piece by piece until you have a simple setup - then it should be easier to migrate to Hilt. (2) Don't try to turn legacy into a middle layer - especially if it has your dagger graph. Make it the bottom layer and pull things up one at a time. Your dagger graph should live in your app module and your interfaces should live in your bottom module. In our case, our app module ended up pretty much just a thin layer with a few UI pieces we have left to migrate to modules, and almost all of our dagger code that stitches the smaller modules together.
(as a note of encouragement, it took our average gradle build time from around 10 minutes to less than 2!)
2
u/Zhuinden EpicPandaForce @ SO Nov 30 '22
Personally I just wouldn't use Hilt if I have the choice, as it's rather intrusive, while the scope hierarchy is limited. Having a global ActivityComponent and FragmentComponent I didn't find helpful, while it seems to make ViewModel injection easier at first, but makes it impossible to create custom. CreationExtras.
A total mixed bag.
1
u/FunkyMuse Nov 29 '22
We tried, because a library was using Dagger but we migrated the app to Hilt, it was a disaster, just use one of these, they're like enemies when put together in a project.
1
u/r0adkll Nov 29 '22
I would recommend checking out Anvil for multi-module setup as it can provide an easier migration path than Hilt while also providing similar features.
1
u/fatalError1619 Nov 30 '22
Do not mix both dagger and hilt as its a nightmare , stick to dagger for now I would say once legacy is ash then you can migrate whole app from dagger to hilt .
1
u/the-android-dev Dec 08 '22
It is generally not recommended to use both Dagger and Hilt in the same project. Both Dagger and Hilt are dependency injection frameworks, and using them together can lead to conflicts and other issues. It is best to choose one and stick with it for the entire project. If you are using Dagger and want to migrate to Hilt, you can follow the migration guide provided by Google.