Search code examples
javaandroiddependency-injectionroboguice

Should I Use RoboGuice or other Dependency Injection Frameworks?


I heard from a friend that Coading gets a lot Easier and simpler with RoboGuice. But I found Official document of Android when I was reading this. In its 2nd Point the Author said:

Official document of Android suggests to avoid dependency injection frameworks like Guice or RoboGuice, reason being is it’s a reflection based library and it creates overheads at run time. Even it tends 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. (Here I would say we can use Android annotation library because it creates duplicate copy of annotated classes so it won’t create any overhead at runtime but at compile time.)

So Should I use it or Not? Any Help Will Be Appretiated.


Solution

  • Yes it's getting easier but as you read in the official document like you stated above that kind of dependency injection can slow down your app performance because it depends on Java's Reflection.
    But not all dependency injection are using Reflection like Butterknife and Dagger 2 so it won't affect your app performance.

    Example of Butterknife usage :

    class ButterknifeActivity extends Activity {
      @Bind(R.id.title) TextView title;
      @Bind(R.id.logo) ImageView logo;
    
      @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        ButterKnife.bind(this);
      }
    }
    

    With Butterknife you don't need to call findViewById and cast it, instead the library will do that for you.