What are the advantages of Dagger 2 over Dagger 1?
So far I found (just) 2:
In the same time I found one big disadvantage: you cannot have module overrides (@Module(overrides = true)
) in Dagger 2, which is largely annoying at least for me - it was very useful for unit test.
Are there other advantages / disadvantages?
x: It is an adaptation of an earlier version created by Square and now maintained by Google. Dagger2 is compile time Dependency injection framework that generates code to connect dependencies at compile time.
Hilt is Jetpack's recommended library for dependency injection in Android. Hilt defines a standard way to do DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you.
Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.
Benefits of using Dagger Dagger frees you from writing tedious and error-prone boilerplate code by: Generating the AppContainer code (application graph) that you manually implemented in the manual DI section. Creating factories for the classes available in the application graph.
Some advantages and disadvantages taken from https://blog.gouline.net/2015/05/04/dagger-2-even-sharper-less-square/ and http://google.github.io/dagger/dagger-1-migration.html:
Advantages of Dagger 2:
@Singleton
.And disadvantages:
inject()
method now has a strong type association with the injection target. This is good for debugging, but it complicates a common practice of injecting from base classes (e.g. base activities, fragments etc).overrides
. Modules that override for simple testing fakes can create a subclass of the module to emulate that behavior. Modules that use overrides and rely on dependency injection should be decomposed so that the overriden modules are instead represented as a choice between two modules.EDIT 2016/11/16: This is not a technical advantage, but Dagger 1 is now deprecated (as of September 15, 2016) and will no longer be actively developed. They recommend migrating to Dagger 2.
I wouldn't recommend having a look at toothpick.
As a co-author, I am obviously over biased, but yeah this one is far simpler to use, at least as fast in most cases than the daggers. And really the scope tree is a very very powerful way to develop more advanced features like recycling instances in scopes spanning on multiple activities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With