Skip to content

Instantly share code, notes, and snippets.

@cable729
Last active August 29, 2015 13:56
Show Gist options
  • Save cable729/9088539 to your computer and use it in GitHub Desktop.
Save cable729/9088539 to your computer and use it in GitHub Desktop.
Thoughts on better android development frameworks

#Injection

I want to be able to do this:

// In module, called at startup
bind(GithubApiInterface.class).to(AwesomeImplementation.class);

// In class
public class AuthenticationController extends Auth {
    public AuthenticationController(GithubApiInterface api) {
        this.api = api;
    }
    
    ...
}

// In test
@Test void shouldLoginWithAdminCreds() {
    bindOnce(AuthenticationController.class).to(myStubbedController);
    Injector.get(AuthenicationController.class);
    
    ...
}

Of course, we would never call new AuthenticationConroller() ourselves. We would assume that Auth itself is a dependency to something or another, and so on, until we reach an Activity or Fragment or other top level feature. We want any of these objects to automatically be injected, so how will we do it?

  1. Compile-time Annotation Processing: This has worked before, and is what all of the fastest current popular android frameworks do. Downside is compile time suffers and subclasses are often generated. In some cases frameworks make you reference the subclasses, which is error-prone and ugly.
  2. Run-time Annotation Processing: This is what Roboguice does. It is slower than other methods, and does not give much of a beauty boost.
  3. Intercepting creation of Activities: This should be possible with bytecode manipulation, but it would be slow. Maybe android will someday provide hooks for activity creation so we can inject, or maybe they're there and we just need to search for them.
  4. Creating a subclass of objects that need last level injection: This could easily work, but at the expense of letting users use their own subclasses. This would break SherlockActionBar compatability, but could easily be remedied.

Getting rid of annotations while still being succinct

In AndroidAnnotations you can easily set up a menu with one line, which saves a lot of effort going through 10+ lines of boilerplate on every activity and fragment. However, annotation processing takes time. It would be nice to have an activity class with plugins, so that you could do something like the following:

public MyActivity() {
    setMenu(R.menu.this_menu);
}

It would be even cooler if you could do the following:

// In module (one-time setup)
bind(DrawerInterface).toLambda(() => {
    DrawerInterface appDrawer = new Drawer(R.plugins.ics_drawer);
    appDrawer.items.add(...);
    return appDrawer;
});

// In activity
public MyActivity(DrawerInterface appDrawer) {
    setDrawer(appDrawer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment