Dagger 和 Butter Knife 对比 Android 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24351817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dagger and Butter Knife vs. Android Annotations
提问by user3277846
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:
我正在评估 Android 应用程序的依赖注入 (DI) 框架。最大的竞争者是:Dagger(带 Butter Knife)和 Android Annotations。我知道 Dagger 和 ButterKnife 来自同一个源 - 方格并且它们相辅相成。以下是我正在寻找的关键矩阵:
- 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?)
- 易用性(我们的构建基于 Gradle,我们使用 Android Studio IDE)
- 测试支持(我们使用 Robotium 进行功能测试,使用 RoboLectric 进行单元测试)
- 性能(DI 框架使用反射,哪个更快?)
回答by ChrLipp
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.
AndroidAnnotations
使用编译时注解处理。它生成一个下划线附加到原始名称(MyActivity_
从生成)的子类MyActivity
。所以要让它工作,你总是必须使用生成的类作为引用而不是原始类。
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:
Butterknife
也使用编译时注释处理,但它生成由中央类 (ButterKnife
)使用的 finder 类。这意味着您可以使用原始类进行引用,但您必须手动调用注入。ButterKnife 介绍的副本:
@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 @ViewById
and @ViewsById
) and some event binding (for a complete list see the namespace directory here, just count the OnXXX
event annotations).
功能集不是很丰富,ButterKnife 支持视图注入(AndroidAnnotations 等价于@ViewById
and @ViewsById
)和一些事件绑定(完整列表见这里的命名空间目录,只计算OnXXX
事件注释)。
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:
Dagger
是 Android 的 DI 实现,类似于 Guice。它还使用编译时注释处理并生成用于手动注入的对象图。您可以区分应用程序对象图和作用域对象图,例如在活动中注入。在这里您可以看到一个Application.onCreate
示例:
@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
我发现从 dagger 开始比较困难,但这可能只是我的经验。但是,请在此处查看一些视频以获得更好的开始: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.
从功能集的角度来看,我会说 Dagger 实现了可以与 AndroidAnnotation@EBean
和@Bean
功能进行比较的功能。
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 和 ButterKnife+Dagger 之间没有太大区别。不同之处在于编程模型(使用类_
而不是使用原始类并手动调用注入)和功能集。
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.
AndroidAnnotation 为您提供了完整的功能列表,但会将您与某些库联系起来。例如,如果您使用它的 rest api,则必须使用 Spring Android。@OrmLiteDao
无论您是否使用 OrmLite,您还可以对 OrmLite ( )等功能进行注释。
At the end it is a matter of taste, at least in my opinion.
最后,这是一个品味问题,至少在我看来。
回答by LOG_TAG
Here is the Nice article in Dzone blog.
这是Dzone 博客中的 Nice 文章。
We to need to compare the features of each, such as :
我们需要比较每个的功能,例如:
- Minimum Jars required
- ActionBarSherlock compatibility
- Injection for click listeners
- POJO injection
- Performance
- 最少需要的罐子
- ActionBarSherlock 兼容性
- 单击侦听器的注入
- POJO注入
- 表现
Only Pojo Injection missing in butterknife! So looks like Butterknife is the winner!
黄油刀中只缺少 Pojo 注射液!所以看起来 Butterknife 是赢家!
回答by ChadJPetersen
Google does ask specifically not to use dependency injection.
Google 确实特别要求不要使用依赖注入。
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
.
但是通过阅读他们的请求,他们似乎更多地指的是Guice
基于反射的 DI 库。诸如 android annotation 之类的库不使用反射,而是使用编译时生成的代码,而butterknife
anddagger
使用了少量针对 android 优化的反射,但据说比android annotation
. 这实际上取决于项目以及您愿意承受的性能损失。在我看来,仅使用butterknife
本身就足以加速代码开发。如果您需要更多使用android annotation
,最后如果您愿意因反射而对性能造成轻微影响,那么最好的选择是在不完全破坏Guice
基于强大反射的性能的情况下使用dagger
+ butterknife
。
回答by Snicolas
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)
- 纯Java
- 快,它不使用反射而是使用注释处理
- 简单、灵活、可扩展且功能强大、稳健且经过测试
- 线程安全
- 文档化和开源
- 范围安全:它强制执行无泄漏应用程序
- 面向测试:它使测试更容易
- 它适用于 Android 或任何其他基于上下文的框架(例如 Web 容器)
It can even be faster than Dagger 2 in most cases, and it's much simpler.
在大多数情况下,它甚至可以比 Dagger 2 更快,而且要简单得多。
Note: Yes, I am one of the authors.
注意:是的,我是作者之一。
回答by Juan Mendez
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!
使用 Android Annotations 或 Butterknife 来简化您的编码。但是不要去Roboguice!Roboguice 强制您的活动、片段扩展到 roboguice 类。一点都不好玩!
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.
Dagger 2 是一个更好的选择。如果您愿意,您可以将其与 Android Annotations 一起使用。我只会将 Android Annotations 用于一个简单的应用程序,但现在更适合使用 Dagger。
回答by Pier Betos
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:
最终,如果您使用三者之一,您将很难过渡到 Android 的数据绑定。如果您需要考虑性能,那是最快的:
回答by serv-inc
Seems like Google chooses dagger, as they are developing it jointly with Square, who created it.
似乎 Google 选择了 dagger,因为他们正在与创建它的Square 联合开发它。
Concerning Butterknife and Dagger themselves, there is the SO question difference-between-dagger-and-butterknife-androidwhich clarifies how they complement each other.
关于 Butterknife 和 Dagger 本身,有一个 SO question difference- between-dagger-and-butterknife-android,它阐明了它们如何相互补充。
回答by serv-inc
The reddit-threadmentioned by @ChrLipp has someone who used all three on the same project, speaks highly of dagger+butterknife but also gives AndroidAnnotations its place:
@ChrLipp 提到的reddit 线程有人在同一个项目中使用了所有三个,高度评价了 dagger+butterknife 但也给了 AndroidAnnotations 它的位置:
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.
对于依赖注入,butterknife 用于视图,Dagger 用于所有对象,强烈推荐,Android Annotations 创建更多的框架来开发 Android 而不是将对象注入到您的类中,因此每个库彼此完全不同。Dagger 相当于 Guice,但速度要快得多。Dagger 比 ButterKnife 和 Android Annotations 更强大,因为它注入所有对象,而不是只注入特定对象集的 ButterKnife 和 Android Annotations。
Dagger 的设置和配置可能很麻烦,但一旦完成就非常值得。但话又说回来,因为这些彼此完全不同,所以这完全取决于您对项目的需求。
另外,说到每一个都非常不同,在你的项目中,如果你真的愿意,你可以在同一个项目中使用 ButterKnife、Android Annotations 和 Dagger。他们每个人都有相同的想法,但做了不同的事情,所以你可以使用它们。
回答by ech0s7r
I think the better between (in terms of performance) ButterKnife and AndroidAnnotation is the second. ButterKnife uses compile time Annotation (RetentionPolicy.CLASS), but it inject code at runtime, resulting greater effort of time. Instead, AndroidAnnotations, process all annotations at compile time.
我认为(在性能方面) ButterKnife 和 AndroidAnnotation 之间的更好是第二个。ButterKnife 使用编译时注解(RetentionPolicy.CLASS),但它在运行时注入代码,导致更多的时间工作。相反,AndroidAnnotations 在编译时处理所有注释。