Skip to content

Instantly share code, notes, and snippets.

@geftimov
Created November 5, 2014 11:32
Show Gist options
  • Save geftimov/a30cbe5d8528e163c175 to your computer and use it in GitHub Desktop.
Save geftimov/a30cbe5d8528e163c175 to your computer and use it in GitHub Desktop.
Android dagger config.
//in Application class
private static ObjectGraph graph;
@Override
public void onCreate() {
super.onCreate();
graph = ObjectGraph.create(getModules().toArray());
}
protected List<Object> getModules() {
final List<Object> objects = new ArrayList<Object>();
objects.add(new AndroidModule(this));
return objects;
}
public static void inject(Object object) {
graph.inject(object);
}
// in build gradle
compile 'com.squareup.dagger:dagger:1.2.1'
compile 'com.squareup:javawriter:2.5.0'
compile 'com.squareup.dagger:dagger-compiler:1.2.1'
//AndroidModule
@Module(injects = {BaseActivity.class, BaseFragment.class},
complete = false,
library = true)
public class AndroidModule {
private final JiraApplication application;
public AndroidModule(JiraApplication application) {
this.application = application;
}
/**
* Allow the application context to be injected but require that it be annotated with
* {@link @Annotation} to explicitly differentiate it from an activity context.
*/
@Provides
@Singleton
Context provideContext() {
return application.getApplicationContext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment