Programing

Dagger and Butter Knife vs. Android 주석

lottogame 2020. 11. 25. 07:29
반응형

Dagger and Butter Knife vs. Android 주석


Android 앱용 DI (Dependency Injection) 프레임 워크를 평가하고 있습니다. 최고의 경쟁자는 Dagger (Butter Knife 포함) 및 Android Annotations입니다. 나는 Dagger와 ButterKnife가 동일한 소스 스퀘어에서 나 왔으며 서로를 보완한다는 것을 이해합니다. 내가 찾고있는 주요 매트릭스는 다음과 같습니다.

  1. 사용 용이성 (빌드는 Gradle을 기반으로하며 Android Studio IDE를 사용합니다)
  2. 테스트 지원 (기능 테스트에는 Robotium을 사용하고 단위 테스트에는 RoboLectric을 사용)
  3. 성능 (DI 프레임 워크는 리플렉션을 사용하는데 어느 것이 더 빠릅니까?)

AndroidAnnotations
는 컴파일 시간 주석 처리를 사용합니다. 원래 이름 (에서MyActivity_생성됨MyActivity)에밑줄이 추가 된 하위 클래스를 생성합니다. 따라서 작동하려면 항상 원래 클래스 대신 생성 된 클래스를 참조 용으로 사용해야합니다.

매우 풍부한 기능 세트 가 있습니다 . 사용 가능한 주석 목록을 참조하십시오 .

Butterknife
는 컴파일 시간 주석 처리도 사용하지만 중앙 클래스 (ButterKnife)에서사용하는 파인더 클래스를 생성합니다. 이는 참조를 위해 원래 클래스를 사용할 수 있지만 수동으로 주입을 호출해야 함을 의미합니다. ButterKnife 소개의 사본 :

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

기능 세트가 그다지 풍부하지 않으며 ButterKnife는 뷰 삽입 (AndroidAnnotations에 해당하는 @ViewById@ViewsById)과 일부 이벤트 바인딩 (전체 목록은 여기 에서 네임 스페이스 디렉토리를 참조 하고 OnXXX이벤트 주석 만 계산 )을 지원합니다.

Dagger
는 Guice와 유사한 Android 용 DI 구현입니다. 또한 컴파일 시간 주석 처리를 사용하고 수동 주입에 사용하는 개체 그래프를 생성합니다. 예를 들어 활동에 삽입하기 위해 응용 프로그램 개체 그래프와 범위가 지정된 개체 그래프를 구분합니다. 여기에Application.onCreate예가 있습니다.

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

단검으로 시작하는 것이 더 어렵다는 것을 알았지 만 이것은 내 경험 일 수도 있습니다. 그러나 더 나은 시작을 위해 여기에서 몇 가지 비디오를 참조하십시오 : 1 , 2

기능 세트 관점에서 Dagger는 AndroidAnnotation @EBean@Bean기능 비교할 수있는 기능을 구현한다고 말할 수 있습니다 .

요약
사용 편의성, 테스트 지원 및 성능을 비교하는 경우 AndroidAnnotation과 ButterKnife + Dagger를 사용하는 것 사이에 큰 차이를 찾을 수 없습니다. 차이점은 프로그래밍 모델 ( _원래 클래스를 사용하는 대신 클래스를 사용하고 수동으로 인젝션 호출)과 기능 세트에 있습니다.

AndroidAnnotation은 전체 기능 목록을 제공하지만 특정 라이브러리에 연결합니다. 예를 들어 나머지 API를 사용하는 경우 Spring Android를 사용해야합니다. OrmLite @OrmLiteDao사용 여부에 관계없이 OrmLite ( )와 같은 기능에 대한 주석도 있습니다 .

결국 그것은 적어도 제 생각에는 맛의 문제입니다.


다음은 Dzone 블로그 의 Nice 기사입니다 .

다음과 같이 각각의 기능을 비교해야합니다.

  • 필요한 최소 항아리
  • ActionBarSherlock 호환성
  • 클릭 리스너를위한 주입
  • POJO 주입
  • 공연

여기에 이미지 설명 입력

버터 나이프에서 Pojo 주입 만 누락되었습니다! Butterknife가 승자 인 것 같습니다!

출처


Google은 특별히 의존성 주입을 사용하지 말 것을 요청합니다.

그러나 그들의 요청을 읽음으로써 그들은 Guice반사 기반 DI 라이브러리를 더 많이 참조하는 것 같습니다 . Android 주석과 같은 라이브러리는 컴파일 시간 생성 코드를 사용하는 대신 리플렉션을 사용하지 않는 반면 butterknife, 및 daggerAndroid에 최적화 된 소량의 리플렉션을 사용하지만 android annotation. 그것은 실제로 프로젝트와 당신이 기꺼이 감수 할 성능의 정도에 달려 있습니다. 제 생각에는 그냥 사용 butterknife하는 것만으로도 코드 개발 속도를 높일 수 있습니다. 당신은 약간 더 사용이 필요하면 android annotation당신은 절대적으로 최강의 성능을 파괴하지 않고 인한 반사 최선의 선택을 약간의 성능 저하를 감수있는 마지막 경우 Guice를 기반으로 반사를 사용 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


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.

참고 URL : https://stackoverflow.com/questions/24351817/dagger-and-butter-knife-vs-android-annotations

반응형