24

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ognyan
  • 13,452
  • 5
  • 64
  • 82
  • 3
    *Don't ask about: Product or service recommendations or comparisons* – Selvin Jul 11 '15 at 09:23
  • You asked a similare question here: http://stackoverflow.com/questions/31354735/how-to-migrate-missing-inject-from-module-with-complete-false-from-dagger-1-to. I think this post should be an edit to that one, – Phantômaxx Jul 11 '15 at 09:47
  • 4
    @Selvin Question like this are sometimes of great value to the community because they draw attention to a common problem like "Which Dagger DI to choose and why?". I am sorry for violating the rule you quoted but there is also "Some subjective questions are allowed", "All subjective questions are expected to be constructive." If your comment receives more votes than the current one - I will delete the question. – Ognyan Jul 11 '15 at 11:30
  • @DerGol...lum The other question is about quite specific technical problem. The current one is more about getting exhaustive list of advantages/problems so one can decide from himself which Dagger to choose. I will delete the question if it turns out to provoke meaningless discussion or flame... – Ognyan Jul 11 '15 at 11:34
  • Well, I totally agree with what @Selvin commented. But this could be an edit to your previous post, making that one richer and deduplicating. – Phantômaxx Jul 11 '15 at 12:40
  • https://youtu.be/oK_XtfXPkqw?t=33m14s – subhash Sep 26 '15 at 06:53

2 Answers2

23

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.

snafu109
  • 582
  • 4
  • 13
  • 1
    I can't find the source of the information anymore, but D2 is supposed to be 17% faster than D1.. – Snicolas Jul 14 '16 at 16:19
3

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.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • You wouldn't? Well I would recommend Toothpick - I've used Dagger 2 a lot more, but this library hasn't caused me any trouble yet. – npace Apr 11 '17 at 13:06