Wednesday, March 11, 2015

MyNotes: Injection Framework

The next item on my agenda is to bring in an injection framework.

I am ambivalent towards these frameworks in general.  Using  them has pros and cons.  In the pro column is that it can simplify testing and it definitely cleans up the code.  In the con column is that once you commit to a framework, it is tough to get it back out.  That means that you are bound to features of the framework, so think carefully about it.  
It sounds counter-intuitive, but I generally don't use them for large or complex apps because they cannot be easily removed and the cost to try may be prohibitive.  It also reduces flexibility of moving code to different states of the Fragment and Activity life-cycles.  
I decide to use one in this app because I haven't used one in a real app for quite a while and I want to see if they have improved.  The app is also small enough and simple enough that I can yank the framework out without too much fuss.

If you choose to use a framework, you have a lot of options. My top two candidates are Roboguice and ButterKnife.  
  • Roboguice is a true dependency injection framework and is probably the most common framework to use and support can easily be found in many online forums.  It also has the bonus of working in any class, not just UI classes.  On the downside, is that the injections occur at Runtime, so it can slow down execution and errors aren't caught until Runtime.  It also does not work great in library project due to the resource ids not being unique.  There is a work around, but it kind of sucks.
  • ButterKnife isn't a true dependency injection framework.  It is great at removing all of the boilerplate code from the UI classes, but it only works in the UI objects.  All other classes have to be done the old-fashioned way.  In the plus column, is that the "injections" are down at compile time so the errors are caught there and there isn't a Runtime impact.  It also works in library projects really well.  Plus, I was a big fan of the ActionBarSherlock and ButterKnife was developed by the same person.

Either one should work well within my app,   I hate to admit, but I basically flipped a coin.  It came up heads, so I brought in Roboguice.  I'm still not sold on it and may change my mind later though...

Step 1: Update the MyNotes build.gradle file.

Add a new proguard file for the roboguice to the appropriate area (the file is empty for now):

android { 
    // Library specific proguard files
    proguardFiles ''

Add in the RoboGuice dependencies:
dependencies {
    compile 'org.roboguice:roboguice:3.+'
    provided 'org.roboguice:roboblender:3.+'

For the record, I basically just took that right off their webpage.

Step 2: Add the dependency injection to the MainActivity java code.

There isn't much there yet, but it looks like this:

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {


The changes for this entry can be found at

No comments :

Post a Comment