问题描述
我评估依赖注入(DI)框架Android应用程序。顶级的竞争者是:匕首(用黄油刀)和Android注解。据我所知,匕首和ButterKnife是来自同一个源 - 广场和相互补充。 Here're是我要找的关键矩阵:
- 易于使用(我们的构建是基于摇篮,我们采用Android Studio IDE中)
- 在测试的支持(我们使用Robotium进行功能测试和RoboLectric单元测试)
- 性能(DI框架使用反射,哪一个是更快?)
AndroidAnnotations
使用编译时批注处理。它会产生一个子类apppended原来的名称下划线( MyActivity _
从 MyActivity
生成)。所以有它的工作,你总是使用生成的类的引用,而不是你原来的类。
它有一个非常丰富的功能集,会看到可用注释中的列表。
Butterknife
使用同样的编译时间注释处理,但它产生一个用于通过中央级的取景器类( ButterKnife
)。这意味着你可以使用你原来的类引用,但是你必须手动调用注入。从ButterKnife引进副本:
@覆盖公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.simple_activity); ButterKnife.inject(本); // TODO使用注入的观点......}
该功能集不是那么丰富,ButterKnife支持视图注射液(AndroidAnnotations相当于将 @ViewById
和 @ViewsById
)和一些事件绑定(对于一个完整列表,请参阅命名空间目录here,只是算 OnXXX
事件注解)。
匕首
是DI的实现为Android,类似于吉斯。它还使用编译时批注处理和生成您使用手动注射对象图。要区分应用程序对象图和范围的对象图之间的注射如在活动。在这里,你看到一个 Application.onCreate
例如:
@覆盖公共无效的onCreate(){ super.onCreate(); objectGraph = ObjectGraph.create(getModules()的toArray()); objectGraph.inject(本); //使用注射类}
我发现这是很难下手匕首,但是这可能只是我的经验。但看到一些视频在这里一个更好的开始: 1 ,的
从视图下的功能设定点我想说的是匕首实现的功能可能被比作AndroidAnnotation的 @EBean
和 @Bean
的功能。
摘要
如果您比较易用性,测试支持和性能我无法找到使用AndroidAnnotation和ButterKnife +匕首太大的区别。差异是在编程模型和功能集。(利用原有的和手动调用的注射使用类_
代替)
AndroidAnnotation为您提供了功能的完整列表,但绑你某些库。例如,如果你使用它的REST API,你必须使用Spring的Android。您还可以批注像OrmLite功能( @OrmLiteDao
)不管您使用OrmLite与否。
目前到底是一个品味的问题,至少在我看来。
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 @ViewById
and @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.
这篇关于匕首和黄油刀与Android的注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!