Dagger and Butter Knife vs. Android Annotations

31,419

Solution 1

AndroidAnnotations
uses compile time annotation processing. It generates a sub class with an underscore apppended to the original name (MyActivity_ generated from MyActivity). So to have it work you always have to use the generated class for references instead of your original class.

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

Butterknife
uses also compile time annotation processing, but it generates finder classes which are used by a central class (ButterKnife). This means that you can use your original class for referencing, but you have to call the injection manually. A copy from the ButterKnife introduction:

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

The feature set is not so rich, ButterKnife supports view injection (AndroidAnnotations equivalent would be @ViewByIdand @ViewsById) and some event binding (for a complete list see the namespace directory here, just count the OnXXX event annotations).

Dagger
is a DI implementation for Android, similar to Guice. It also uses compile time annotation processing and generates object graphs which you use for manually injection. You distinguish between application object graph and scoped object graphs for injecting e.g. in activities. Here you see an Application.onCreate example:

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

I found it is harder to start with dagger, but this might be only my experience. However see some videos here for a better start: 1, 2

From the feature set point of view I would say that Dagger implements functionalities which could be compared to AndroidAnnotation's @EBean and @Bean functionality.

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

AndroidAnnotation gives you a full list of functionalities, but ties you to certain libraries. For example if you use it's rest api you have to use Spring Android. You also have annotations for features like OrmLite (@OrmLiteDao) regardless if you use OrmLite or not.

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

Solution 2

Here is the Nice article in Dzone blog.

We to need to compare the features of each, such as :

  • Minimum Jars required
  • ActionBarSherlock compatibility
  • Injection for click listeners
  • POJO injection
  • Performance

enter image description here

Only Pojo Injection missing in butterknife! So looks like Butterknife is the winner!

Source

Solution 3

Google does ask specifically not to use dependency injection.

But by reading their request they seem to be referring more to the Guice and reflection based DI library's. Libraries such as android annotation use no reflection instead employing compile time generated code, while butterknife and dagger uses a small amount of reflection optimised for android but are supposedly slightly more powerful than android annotation. It really depends on the project and how much of a performance hit you are willing to take. In my opinion just using butterknife is sufficient to speed up code development by itself. If you need slightly more use android annotation and lastly if you are willing to take a slight performance hit due to reflection the best option without absolutely destroying performance with a powerhouse Guice based reflection use dagger + butterknife.

Solution 4

You should give a try at Toothpick.

Toothpick is (per the README):

  • pure java
  • fast, it doesn't use reflection but annotation processing
  • simple, flexible, extensible & powerful, robust & tested
  • thread safe
  • documented & Open Source
  • scope safe : it enforces leak free apps
  • test oriented : it makes tests easier
  • it works very well with Android or any other context based framework (such as web containers)

It can even be faster than Dagger 2 in most cases, and it's much simpler.

Note: Yes, I am one of the authors.

Solution 5

Use Android Annotations or Butterknife to ease your coding. But don't go for Roboguice! Roboguice forces your activies, fragments to extend to roboguice classes. Not fun, at all!

Dagger 2 is a much better option. You can use it along with Android Annotations if you'd like. I would just use Android Annotations for a simple app, but these days is good to work more with Dagger.

Share:
31,419
user3277846
Author by

user3277846

Updated on November 13, 2020

Comments

  • user3277846
    user3277846 over 3 years

    I am evaluating Dependency Injection (DI) frameworks for an Android app. The top contenders are: Dagger (with Butter Knife) and Android Annotations. I understand that Dagger and ButterKnife are from the same source- square and they complement each other. Here're are the key matrices that I am looking for:

    1. Ease of use (our build is based on Gradle and we use Android Studio IDE)
    2. Testing support (we use Robotium for functional testing and RoboLectric for unit testing)
    3. Performance (DI frameworks use reflection, which one is faster?)
  • ech0s7r
    ech0s7r over 9 years
    I think you made a mistake about ButterKnife: it uses compile time Annotation (RetentionPolicy.CLASS), but it inject code at runtime, resulting greater effort of time.
  • ChrLipp
    ChrLipp over 9 years
    I think that all solutions produce code during the compile time and this code is called (at runtime) to perform the injection. In Android Annotations for example it generates a subclass (MyActivity_) which performs the injection for MyActivity. In Butterknife you have to call ButterKnife.inject(this) which delegates to the generated code.
  • John D.
    John D. over 9 years
    Another row I would consider adding to this chart is "Support for Library Projects" which Butterknife does not support, but AndroidAnnotations does via the resName attribute (see github.com/excilys/androidannotations/wiki/Library-projects)‌​. This was a dealbreaker for me using Butterknife.
  • Pier Betos
    Pier Betos about 8 years
    No love for Android's own Data binding library? developer.android.com/tools/data-binding/guide.html You'll eventually forget all that.
  • Pier Betos
    Pier Betos about 8 years
  • TmTron
    TmTron over 7 years
    -1: the question was not about RoboGuice (which is anyway retired and not maintained any longer), but about AndroidAnnotations
  • r00tandy
    r00tandy over 7 years
    Sorry for digging out. the last paragraph is no more correct. Android Annotations is now modular, so you can include OrmLite-Support just if you really use OrmLite.
  • vijay
    vijay over 6 years
    Whats the problem in extending from RoboActivity (that extends from Activity) while you can extend from Activity? you are not losing anything here.
  • Juan Mendez
    Juan Mendez over 5 years
    It's an old library. It used to force you to use their Android component flavors. Now the best option is Dagger 2. Good luck.
  • Yousha Aleayoub
    Yousha Aleayoub over 5 years
    Can we use Android annotation processor for Toothpick?
  • Roman Gherta
    Roman Gherta about 5 years
    I'd like to know your oppinion on the access modifiers. For example you cannot use private access modifiers with some of these libraries, at least Android Annotations from my experience. How does this affect the security of the app. Would this imply a risk?
  • ChrLipp
    ChrLipp about 5 years
    Since Android Annotations is based on inheritance, you have to use protected access modifiers. Yes, this is weaker than private, but does not affect security or impy any risk.