Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
/ arrow Public archive

Java library and annotation processor that makes dagger multibindings easy

License

Notifications You must be signed in to change notification settings

UlrichRaab/arrow

Repository files navigation

Arrow

Version 0.6

Arrow is currently in development and NOT STABLE. Use it on your own risk.

I assume that you already know how to use Dagger 2. If not, you should read Dependency Injection with Dagger 2 and Activities Subcomponents Multibinding in Dagger 2.

A common Dagger setup on Android normally involves an app component and an app module. The module provides dependencies and the component is used to inject these dependencies into objects like activities or fragments. Injection is done by using a code like the one shown below:

public class MyActivity extends Activity {

    ...

    private void injectDependencies() {
        ((MyApp)getApplication())
            .getAppComponent()
            .inject(this);
    }
}

The example looks good and not to complicated but as soon as you want to inject dependencies into more then a couple activities your AppComponent will get cluttered.

TODO Add example code

A good solution is to use subcomponents for every activity or group of activities. This leads to the question how to create subcomponents. I personally prefer to use multibinding to solve this problem. Implementing multibinding can be challenging and there is also some boilerplate code which you need to write to make it work.

See: Activities Subcomponents Multibinding in Dagger 2

I created Arrow to simplify this by using an annotation processor. Arrow uses the dagger Subcomponent and Subcomponent.Builder annotations and generates the multibinding module for you. You can then create subcomponents by using the Arrow class.

public class MyActivity extends Activity {

    ...

    private void injectDependencies() {
        Arrow.subcomponentBuilder(MyActivitySubcomponent.Builder.class)
             .build()
             .inject(this);
    }
}

Google released a Dagger 2 android support library which also allows you to easily inject dependencies into Activities, Fragments, Services, etc. Arrow is not limited to these base framework types.

Setup

In the build.gradle of your project add:

repositories {
    maven {
       url 'https://dl.bintray.com/ulrichraab/maven'
    }
}

In the build.gradle of your app module add:

dependencies {
    compile 'de.ulrichraab.arrow:arrow:<version>'
    annotationProcessor 'de.ulrichraab.arrow:arrow-processor:<version>'
}

Example

See demo app