Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

DaggerIntegration

Csaba Kozák edited this page Oct 11, 2015 · 3 revisions

Integrating Dagger and AndroidAnnotations

Dagger and AndroidAnnotations can easily work together, combined usage of both frameworks is described here. This guide uses Dagger 1; for Dagger 2, see this guide. The article assumes that the reader is familiar with Dagger 1 usage of normal Android projects. If not, the Dagger documentation and example projects should be read first.

Injecting into @EActivity

If you would like to inject Dagger dependencies into an @EActivity annotated Activity, you need to add following modifications:

First, you have to call the injector to inject the dependencies by calling ObjectGraph.inject():

@EActivity
public class DaggerActivity extends Activity {

    @AfterInject
    void onInjectDependencies() {
        ((MyApplication) getApplication()).component().inject(this);
    }

    @Inject
    DaggerDependency dependency;
}

This step is also required in non-AA Dagger projects. You can add this injection code to a common parent class, to avoid code duplication.

Second, you have to use the AA generated classes in your modules. This step is where AA and normal Dagger usage really differs.

@Module(
        injects = {
                DaggerActivity_.class, // note the underscore
                ...
        }
)
public class DaggerModule {
}

Injecting into @EFragment

Usage of @EFragment annotated @Fragment with Dagger is very similar to @EActivity.

First, you have to call the injector to inject the dependencies by calling ObjectGraph.inject():

@EFragment
public class DaggerFragment extends Fragment {

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        ObjectGraph objectGraph = ...; // obtain the graph as usual
        objectGraph.inject(this);
    }

    @Inject
    DaggerDependency dependency;
}

Second, you have to use the AA generated classes in your modules.

@Module(
        injects = {
                DaggerFragment_.class, // note the underscore
                ...
        }
)
public class DaggerModule {
}

Injecting into @EBean

If you want to inject Dagger dependencies into @EBean annotated AA beans, you have to follow these steps:

First, you have to call the injector to inject the dependencies by calling ObjectGraph.inject():

@EBean
public class DaggerBean {

    public DaggerBean() {
        ObjectGraph objectGraph = ...; // obtain the graph as usual
        objectGraph.inject(this);
    }

    @Inject
    DaggerDependency dependency;
}

Second, you have to use the AA generated classes in your modules.

@Module(
        injects = {
                DaggerBean_.class, // note the underscore
                ...
        }
)
public class DaggerModule {
}

Injecting into other components

Injecting into other components like @EView or @EService follows the same pattern which was described here. You have to call the injector in the constructor / creation lifecycle method, and add the generated class instead of the annotated class in the @Module's inject parameter array.

Providing @EBean

If you want to use Dagger to inject an instance of an AA enhanced class, like @EBean, you have to add a @Provides method in your module. In that method, you have to create an instance of the generated class, by calling the generated methods manually.

@EBean
public class SomeDependency {
    // ...
}
@Module(library = true, complete = false)
public class DaggerModule {

    @Provides
    SomeDependency provideSomeDependency(Context context) {
        return SomeDependency_.getInstance_(context);
    }
}

You have to also provide an Android Context object, as usual in Dagger Android projects. After this is done, you can inject the @EBean with Dagger to another class:

public class DaggerClass {
    
    @Inject
    SomeDependency someDependency; // will have an instance of SomeDependency_
}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally