I'm learning ButterKnife and dagger and came to know that Butterknife only helps to avoid boilerplate codes(Binding views) and it doesn't really inject. And dagger compliment Butterknife in this case as it injects code. Now what is the difference between Binding views and Injecting views?
2 Answers
Butterknife - Binding views: It binds the view from XML layout to a Java member variable. So you don't have to call findViewById
manually to bind the views.
Dagger - dependency injection library, to inject dependencies:
Dependency Injection, in simple terms, means that you pass (inject) the dependencies to any class rather than creating the dependency inside the class itself.
Say, you have a class called ClassA which requires an instance of ClassB. One way is you can create the instance of ClassB inside the Class A itself. But in dependency injection, we create the ClassB instance outside and pass it to ClassA either through its constructor or a setter method.
Dependency injection can be done without any framework but it gets ugly if you have a large project and it has lots of complicated dependencies. Dagger is an Android framework which helps to do dependency injection in a cleaner way.

- 13,447
- 7
- 35
- 45
-
is there anything that called "view injection" ?or "view injection" and "dependency injection " are same? – Abhishek Kumar Aug 04 '17 at 19:23
-
I suppose, "view injection" just means binding the views from xml to java. Check here: https://github.com/JakeWharton/kotterknife – Bob Aug 04 '17 at 19:26
-
1Dependency injection is a programming design pattern/ concept, using which we can inject any dependencies, it includes View dependencies, to any other class. – Bob Aug 04 '17 at 19:27
-
"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!" this is the comment by JAKE WHARTON , the developer of both dagger and butterknife. Now he used injection and binding as two different words – Abhishek Kumar Aug 04 '17 at 19:35
-
you can see above comment here on the link:- https://stackoverflow.com/questions/20821148/difference-between-dagger-and-butterknife-android?noredirect=1&lq=1 – Abhishek Kumar Aug 04 '17 at 19:36
-
Yep. Butterknife is to bind views without findViewById boilerplate. Dagger is to inject dependencies. – Bob Aug 04 '17 at 19:39
-
can you tell me why does "ButterKnife.bind(this, view)" is used? – Abhishek Kumar Aug 04 '17 at 20:10
-
That bind method call actually generates the findViewById code and assigns them to the variables in our code, as though we have written it manually. More info here: http://jakewharton.github.io/butterknife/ – Bob Aug 04 '17 at 20:13
Dagger Dependency Injection 'constructs instances of your application classes and satisfies their dependencies'. The way it constructs your instances is based upon the rulesets defined in your dagger provides and modules.
Butterknife uses annotation processing to inject views. It has methods to simplify finding views by Id's, etc.

- 587
- 1
- 3
- 12
-
It doesn't answer my question "What is the difference between Binding Views and Injecting Views?" – Abhishek Kumar Aug 04 '17 at 19:06