Dagger and Butter Knife vs Android Annotations

I am evaluating the framework of Injection (DI) dependencies for an Android app. The main rivals are: a dagger (with a butter knife) and Android annotations. I understand that the Dagger and ButterKnife are on the same source square, and they complement each other. Here are the key matrices I'm looking for:

  • Ease of use (our build is based on Gradle and we use the Android Studio IDE)
  • Testing support (we use Robotium for functional testing and RoboLectric for unit testing)
  • Performance (do DI frameworks use reflection which is faster?)
+54
android dependency-injection dagger butterknife android-annotations
Jun 22 '14 at 13:39
source share
10 answers

AndroidAnnotations
uses compilation of processing annotation time. It creates a subclass with an underscore attached to the original name ( MyActivity_ generated from MyActivity ). Therefore, for it to work, you should always use the generated class for references instead of the original class.

It has a very rich feature set, see the list of available annotations .

Butterknife
also uses compilation of time annotation processing, but generates search classes that are used by the central class ( ButterKnife ). This means that you can use your original class for links, but you need to call the injection manually. Copy from ButterKnife introduction:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } 

The set of functions is not so rich, ButterKnife supports viewing injections (the equivalent of AndroidAnnotations will be @ViewById and @ViewsById ) and some event binding (for a complete list, see the namespace directory here , just recount the OnXXX event OnXXX ).

Dagger
this is a DI implementation for Android similar to Guice. It also uses compile-time annotation processing and generates graphs of the objects you use for manual injection. You distinguish between the graph of the application object and the graphs of cloud objects for injection, for example. in activity. Here you see an example Application.onCreate :

 @Override public void onCreate() { super.onCreate(); objectGraph = ObjectGraph.create(getModules().toArray()); objectGraph.inject(this); // use injected classes } 

It seemed to me that starting with a dagger is more difficult, but it can only be my experience. However, see some videos for a better start: 1 , 2

In terms of feature set, I would say that Dagger implements functionality that can be compared with the functionality of AndroidAnnotation @EBean and @Bean .

Summary
If you are comparing ease of use, testing support, and performance, I cannot find much difference between using AndroidAnnotation and ButterKnife + Dagger. Differences in the programming model (use classes with _ instead of using the source and call the injection manually) and in the set of functions.

AndroidAnnotation gives you a complete list of features, but links you to specific libraries. For example, if you use its rest api, you should use Spring Android. You also have annotations for features like OrmLite ( @OrmLiteDao ), regardless of whether you use OrmLite or not.

In the end, it is a matter of taste, at least in my opinion.

+47
Sep 30 '14 at 17:16
source share

Here is a nice article on the Dzone Blog .

We need to compare the characteristics of each of them, for example:

  • Required Minimum Banks
  • ActionBarSherlock Compatibility
  • Click Listener Injection
  • POJO injection
  • Performance

enter image description here

Only the Pojo injection is missing in the rhino! It looks like Butterknife - the winner!

Source

+19
01 Oct '14 at 11:44
source share

Google specifically asks whether to use dependency injection. But while reading there, ask for their seam to talk more about the guice / reflection-based DI library. things like the android annotation do not use reflection and fully compile the time-generated code, while the butter knife and dagger use a small amount of reflection optimized for android. but presumably a little more powerful than the android annotation. It really depends on the project and how much you have achieved success. in my opinion, just using an oil knife is enough to speed up code development yourself. if you need a little more android androids. and finally, if you are ready to take a small hit in performance due to reflection, the best option without absolutely destructive electronic reflection-based work is a dagger + an oil knife.

+5
Sep 26 '14 at 22:57
source share

Try Toothpick .

Toothpick (per README):

  • pure java
  • quickly, it does not use reflection, but processing annotations
  • simple, flexible, extensible and powerful, reliable and tested
  • thread safety
  • documented and open source
  • security area: it provides leak-free applications.
  • test oriented: it simplifies testing
  • it works great with Android or any other context-based environment (like web containers).

In most cases, it can be faster than Dagger 2. It is much simpler.

Note: Yes, I am one of the authors.

+5
Dec 09 '16 at 6:17
source share

Use Android or Butterknife annotations to simplify coding. But do not go after Rogogis! Roboguice forces your activity, fragments extend to roboguice classes. Not at all fun!

+3
Jul 04 '15 at 18:45
source share

It looks like Google is picking a dagger because they are developing it with Square , which created it.

As for Butterknife itself and the dagger, there is a SO difference-between-dagger-and-butterknife-android question that clarifies how they complement each other.

+1
Jul 03 '15 at 10:53
source share

reddit-thread , mentioned by @ChrLipp, has someone who used all three in one project , says high dagger + churn, but also gives AndroidAnnotations its place:

For dependency injection, butterknife is used for views, a dagger is used for all objects and it is highly recommended and annotating Android creates more opportunities for Android development instead of introducing objects into your classes, so each library is different from each other. The dagger is equivalent to Guice, but it is very much faster. The dagger is stronger than ButterKnife and Android Annotations, because they enter all objects, not ButterKnife and Android Annotations, which enter only a specific set of objects.

A dagger can be a pain to tune and tune, but it's worth it, you did it. But then again, because all this is completely different from each other, it all depends on your needs. project.

In addition, speaking of the fact that each of you is completely different, in your project you can use ButterKnife, Android annotations and a dagger all in one and the same if you really want it. Each of them has the same idea, but something else so that you can use all of them.

+1
Jul 03 '15 at 10:56
source share

In the end, if you use one of the three, it will be difficult for you to switch to data binding to Android. This is the fastest if you need to consider performance:

https://developer.android.com/tools/data-binding/guide.html

+1
Apr 28 '16 at 15:24
source share

I think the best (in terms of performance) ButterKnife and AndroidAnnotation is the second. ButterKnife uses Annotation compilation time (RetentionPolicy.CLASS), but it injects code at runtime, which leads to more time effort. Instead, AndroidAnnotations process all annotations at compile time.

0
Oct 07 '14 at 11:03
source share

Why worry about dependency injection?

Before moving on, one might even ask: β€œBut why do I even consider DI? People say that the code is cleaner, but my code is never dirty.” Now this may be true when you look back at what you wrote a few days ago for a lesser hobby project where you are not doing anything complicated, skip unit tests, etc. But the reality is different. There are many things you get through the use of Injection Dependency, some of which move more than others.

enter image description here See a complete comparison of all Android Dependency injection libraries

Testability

This is probably the most important of my views. Whenever you write unit tests (and you should always write them), you need to make sure that you are testing your own code and only for the class in question. You do not want to depend on the logic of any library or other class that is tested separately. How do you do this? The best way is to use layouts that are introduced instead of real implementations. In the end, you don’t want the class test that speaks to an external third-party service to depend on your Internet connection and your real credentials for which you pay.

Separation of code and responsibility

Based on the fact that Ive mentioned testing, it becomes clear that code separation will occur naturally. This will allow you to break the code into smaller pieces that are responsible for one thing at a time. Smaller pieces of code lead to cleanliness, cleanliness leads to readability, readability leads to fewer errors. And thereby we increase the maintainability of our code.

Replaceability and adaptation

Refactoring is a natural part of any development project. We will develop code that needs to be changed in the future or even completely replaced some implementations. The less we have to do this, the better. With Dependency Injection, you can easily adapt your code, introduce new dependencies, and even completely change them.

0
Nov 06 '17 at 7:20
source share



All Articles