69

Can anyone point out the difference between Dagger and Butterknife? I know that Butterknife is a view injection library and Dagger is a dependency injection library. But the documentation online seems a bit overhead for me. According to Butterknife documentation, you can do non-activity injections as well, which is what Dagger does? Or did I misunderstand something?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
user2511882
  • 9,022
  • 10
  • 51
  • 59

5 Answers5

98

ButterKnife is targeted to inject views only. Non-activity injection just means that you can provide your own view root to inject views from (like with manually inflated views, etc.). Dagger is a bit more complicated. It can inject anything you want as long as you specified Module - class which satisfies those dependencies (alternatively you can use constructor injection).

As a bottom line - I would say ButterKnife helps you to avoid all that boilerplate code for creating views (aka (TextView)findViewById(R.id.some_text_view);. Nothing more. Under the hood it still does all that boring code. So it is not really an injection..

Also it worth mentioning that Jake Wharton is one of the developers for both those cool libs :)

Here is some useful links:

Dagger sample project on GitHub

Dagger presentation on Devoxx (Jake Wharton)

Dagger hangout with Dagger devs

Don't have much of useful ButterKnife links. It really simple and straight forward though, so hopefully you don't need any

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • 2
    so do you mean that we can use dagger to inject textviews,editviews etc? – user2511882 Dec 29 '13 at 02:05
  • 2
    I believe you can inject views in Dagger using qualifiers (see QUALIFIERS section in Dagger documentation) where you pass id as a qualifier, but it would look really weird. I thinks Dagger creators will be really surprised if they find out someone is using Dagger for views injection:) But technically, I don't see any reasons why it shouldn't work.. – Pavel Dudka Dec 29 '13 at 02:28
  • 2
    To be honest, there isn't much documentation online on how to use dagger,butterknife. I am looking for some tutorials but can't find any. And the official docs seem to much overhead for me to grasp everything. I shall accept your answer anyways, but if you can post some useful links, it would be helpful. Cheers! – user2511882 Dec 29 '13 at 02:46
  • 96
    If you use Dagger to try and inject views you're going to have a very bad time :) Try to think of Butter Knife as a means of binding views rather than injection. I only called it injection because all the RoboGuice converts were complaining about lack of "view injection" with Dagger. It's not really injection at all. Like the answer mentions, it's just a way to reduce boilerplate. Under the hood it's just calling `findViewById` like you would! – Jake Wharton Dec 29 '13 at 10:31
  • 1
    Straight from the horses mouth... So to speak. Spreading the good word! :) – worked Mar 02 '15 at 06:49
  • 1
    As per me, Dagger and Butterknife both complement each other. We can use both together in our project. – Abhishek Jain Nov 12 '16 at 13:51
  • Pavel Dudka and @JakeWharton, you have a high score in [tag:dagger]. Can you please cast your votes to mark other tags like [dagger-android](https://stackoverflow.com/questions/tagged/dagger-android) and [dagger.android](https://stackoverflow.com/questions/tagged/dagger.android) as synonyms of tag [dagger](https://stackoverflow.com/tags/dagger/synonyms)? – naXa stands with Ukraine Oct 06 '18 at 10:36
  • @PavelDudka https://stackoverflow.com/questions/68230812/unable-to-use-in-local-variable-bindviews-not-applicable-to-local-variable-bu pls look and help me – anshul raj Jul 04 '21 at 01:42
5

Here is a link to the Buterknife documentation. It's very straightforward. However, what the documentation doesn't say but the design of Butter Knife implies is that you can also use Butter Knife in custom views. Just replace "this" with "context" or "getContext" so you determine the scope.

Link: http://jakewharton.github.io/butterknife/

I combine Butter Knife, parcelable and easyAdapter for list views in my project. Reason is less boilerplate and with parcelable faster and cleaner parceling. So if you have a lot of ListViews, I recommend this approach.

Links:

https://github.com/johncarl81/parceler

https://github.com/ribot/easy-adapter

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
greenspand
  • 749
  • 2
  • 8
  • 15
5

@JakeWharton's answers it partially in the comment:

TL;DR: They complement each other. Use Butterknife for injecting views, Dagger for the rest.

If you use Dagger to try and inject views you're going to have a very bad time :) Try to think of Butter Knife as a means of binding views rather than injection. I only called it injection because all the RoboGuice converts were complaining about lack of "view injection" with Dagger. It's not really injection at all. Like the answer mentions, it's just a way to reduce boilerplate. Under the hood it's just calling findViewById like you would!

serv-inc
  • 35,772
  • 9
  • 166
  • 188
4

The difference is pretty straightforward: A butter knife is like a dagger only infinitely less sharp.
As it is pointed out in the documentation.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • 4
    I actually read this in the documentation but ended up here for a better explanation. – Maurice Gavin Sep 02 '15 at 14:25
  • But a dagger implies only infinite more boilerplate code for view binding, because you have to cast any View subclass you could get. ButterfKnife determines cast type at compile time. You, can't tell dagger to do so. – Louis CAD Sep 02 '15 at 16:36
  • @LouisCAD Dagger is not designed for view binding ([see comment](http://stackoverflow.com/questions/20821148/difference-between-dagger-and-butterknife-android#comment31227839_20821326)). – naXa stands with Ukraine Sep 02 '15 at 22:05
0

ButterKnife was made to simplify registering click listeners, and to reduce the boilerplate provided by findViewById() calls.

Dagger and Dagger2 are general purpose dependency injection systems.

Ignore the text on Guice and MVVM, and read Dependency Injection and The Android Way. This pretty much answers what Dagger is meant to do and simplify.

https://spring.io/blog/2011/08/26/clean-code-in-android-applications

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428