Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java dependency injection: Dagger 1 vs Dagger 2, which is better?

What are the advantages of Dagger 2 over Dagger 1?

So far I found (just) 2:

  • Dagger 2 allows you to use code obfuscation with proguard
  • Dagger 2 is faster (which is not much of an advantage when using it for android application but it is sure an important thing if you use it for some kind of server)

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?

like image 332
Ognyan Avatar asked Jul 11 '15 08:07

Ognyan


People also ask

What is the difference between dagger and dagger 2?

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.

Which dependency injection is better in Android?

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.

What is dagger 2 used for?

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.

What is the benefit of dagger?

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.


2 Answers

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:

  • No more reflection - everything is done as concrete calls (ProGuard works with no configuration at all)
  • No more runtime graph composition - improves performance, including the per-request cases
  • Traceable - better generated code and no reflection help make the code readable and easy to follow
  • Supports method injection in addition to field and constructor injection which were the only two types supported by Dagger 1
  • Modules require less configuration than Dagger 1
  • Allows users to use any well-formed scope annotation. Dagger 1 only supported a single scope: @Singleton.

And disadvantages:

  • The 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).
  • Component implementation requires rebuilding the project to appear and any injection-related compile errors result in the class disappearing (i.e. not being generated).
  • Doesn't support 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.

like image 137
snafu109 Avatar answered Oct 01 '22 21:10

snafu109


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.

like image 22
Snicolas Avatar answered Oct 01 '22 21:10

Snicolas