I have heard there two major dependency injection libraries used in android one is roboguice and the other one is ButterKnife the main difference between them is roboguice is run time and buttter knife is compile time,so i am confused which has better performance on application wheather its run time or compile,if any idea throws your thoughts here.
Butter Knife will always be faster because it's based on code generation rather than reflection.
However, you can't call what it does dependency injection. It's just view lookup. It could potentially be called view binding as well. The fact that it refers to itself as "view injection" (note the quotes) is a tongue-in-cheek joke.
I've been using Roboguice for 3 months and I think it's a really close approximation of what Spring framework does (using injection). Roboguice is really powerful and lets you inject views, Interfaces and classes (even with the app/activity context if you need it).
Even, If you are worried about Roboguice performance, they've launched Roboblender which helps to improve significantly Roboguice performance.
You may find Roboguice documentation in the following Github link
Butterknife does not seem to me a dependency injection library. Rather than I can call it a view library which makes binding easier, reduces findViewById(...) statements. There is an article comparing these two libraries http://java.dzone.com/articles/dependency-injection-roboguice
Related
I have a question. I have an application that I wrote by extracting data from Api. And I want to add dependency injection to it. Which library will work best and why? How should I choose?
In Android there are multiple libraries to support dependency injection here are few of most used ones by developers.
The ones provided by google are Dagger and Hilt. But they have a bigger learning curve compared to other DI libraries. Still I would recommend to you these in your project.
Hilt is the latest counterpart and is a written over the existing Dagger DI and comes with all the abilities of Dagger hence it is recommended from now on over dagger since it provides a standard way to use DI in your application which was missing in Dagger.
Check out about Dagger from this link
Check out about Hilt from this link
Both the above DI libraries automatically generates code that mimics the code you would have written manually to provide dependency. The code is generated at compile time and therefore if there comes any issue with providing dependency it will be shown at compile time hence avoiding runtime issues regarding fulfilling dependencies.
There are Kotlin specific dependency injection libraries too such as Koin which is more easy to learn and implement compared to Dagger.
Check out about Koin from this link
Koin is usually considered for small to medium sized projects while Dagger and Hilt is considered for medium to large sized projects.
Dagger hilt library is best for dependency injection very less code for injection you can simply inject anything also no need to provide any view model factories for your view models
I'm reading about Dependency Injections and found 2 libs that get my attention, AndroidAnnotations and Dagger 2. What I saw is that AA has a lot of functionalities including DI, but most of the developers are using Dagger 2 for DI.
I was wondering what is the diference between DI with AA and DI with Dagger 2? If I use AA it means I don't need Dagger 2?
I couldn't find much information for DI with AA and comparison with other libraries.
Any info would help a lot.
I do not think AA and Dagger can be compared.
Dagger is a general dependency injection library, with lots of capabilities. It is designed to run on Android as well, but it does not need Android, it can be applied on pure Java projects. It has lots of dependency injection features for a fully code-generation based dependency injector.
AndroidAnnotations is an annotation-based framework for Android. It does have a limited dependency injection module (which is only a small subset of AA), however that is not its main feature. It adds annotation based, boilerplate removing APIs for lots of thing for Android, which are used in every project and normally require an awful lot of unnecessary code, like view and resource injection, event handling, instance state restoration, threading, etc. You can see all the use cases of AA here.
Dagger and AA can coexist, actually it really makes sense to use the sophisticated dep injection from Dagger and the lot of features of AA together (i do in all of my projects).
Disclaimer: i am an AndroidAnnotations developer.
recently I have created one sample application by implementing Dagger 2 and Android Architectural Components (Room and Viewmodel) which can help you understand dependency injection using dagger library along with MVVM architecture.
Here is the github project link
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:
Ease of use (our build is based on Gradle and we use Android Studio IDE)
Testing support (we use Robotium for functional testing and RoboLectric for unit testing)
Performance (DI frameworks use reflection, which one is faster?)
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.
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
Only Pojo Injection missing in butterknife! So looks like Butterknife is the winner!
Source
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.
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.
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.
Seems like Google chooses dagger, as they are developing it jointly with Square, who created it.
Concerning Butterknife and Dagger themselves, there is the SO question difference-between-dagger-and-butterknife-android which clarifies how they complement each other.
The reddit-thread mentioned by #ChrLipp has someone who used all three on the same project, speaks highly of dagger+butterknife but also gives AndroidAnnotations its place:
For dependency injection, butterknife is used for Views, Dagger is
used for all objects and is highly recommended and Android Annotations
creates more of a framework for developing Android instead of
injecting objects into your classes so each library are quite
different from each other. Dagger is equivalent to Guice but is much
much faster. Dagger is more powerful then ButterKnife and Android
Annotations as it injects all objects rather than ButterKnife and
Android Annotations which only inject a certain set of objects.
Dagger can be a pain to setup and configure but is well worth it once
you have it done. But then again, because these are all quite
different from each other, it all depends on what your needs are for
the project.
Also, speaking of each one being quite different, in your project you
can use ButterKnife, Android Annotations and Dagger all in the same
project if you really want to. They each have the same idea but do
something different so you could use them all.
Eventually if you use one of the three, you'll have a hard time transitioning to Android's databinding. That's what's fastest if you need to consider performance:
https://developer.android.com/tools/data-binding/guide.html
So I have come across this best practices on Android articles on memory performance.
http://developer.android.com/training/articles/memory.html
They said
Avoid dependency injection frameworks
Using a dependency injection framework such as Guice or RoboGuice may
be attractive because they can simplify the code you write and provide
an adaptive environment that's useful for testing and other
configuration changes. However, these frameworks tend to perform a lot
of process initialization by scanning your code for annotations, which
can require significant amounts of your code to be mapped into RAM
even though you don't need it. These mapped pages are allocated into
clean memory so Android can drop them, but that won't happen until the
pages have been left in memory for a long period of time.
But what about Dagger which they claim to be fast. Not sure which one should I go for?
This recommendation does not apply equally to all dependency injection frameworks.
..frameworks [that work like Guice] tend to perform a lot of process initialization by scanning your code for annotations, which can require significant amounts of your code to be mapped into RAM even though you don't need it..
Thus, if using a DI/IoC framework that doesn't scan for said [run-time] annotations, implying the [excessive] use of reflection, then this reason doesn't apply. While Dagger does use annotations these are used differently than by Guice1 and avoid the problem stated.
Since Dagger was written as "A fast dependency injector for Android and Java", the authors have designed it for this purpose and believe that it is suitable for such a target - go ahead, give it a try.
1 Dagger uses compile-time annotations (well, mostly) instead of relying on run-time annotations and reflection; it is the run-time annotation scanning and reflection that causes the issue the memory guide was warning about.
The Android team has recently updated their recommendation to suggest developers use Dagger 2.
The previous recommendation was based on the high cost of reflection. Since Dagger 2 no longer uses reflection - Dagger 1 did - they believe it "can be used in Android apps without needless runtime cost or memory usage".
(Disclaimer: I'm the Dagger 2 team manager.)
The creator of Dagger, #JakeWharton, also wrote a simpler view "injection" framework called Butterknife
because all the RoboGuice converts were complaining about lack of
"view injection" with Dagger.
You use it like this:
class ExampleActivity extends Activity {
#InjectView(R.id.title) TextView title;
#InjectView(R.id.subtitle) TextView subtitle;
#InjectView(R.id.footer) TextView footer;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO Use "injected" views...
}
}
Dependency injection best practices articles were recently added to Android developer official website. In these articles developers are encouraged to use Dagger 2 as a primary DI library for project medium (4-7 screens) and large (8+ screens) apps.
Dagger facilitates using DI in your app by creating and managing the graph of dependencies for you. It provides fully static and compile-time dependencies addressing many of the development and performance issues of reflection-based solutions such as Guice.
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?
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
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
#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!
The difference is pretty straightforward: A butter knife is like a dagger only infinitely less sharp.
As it is pointed out in the documentation.
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