You can use Autadogger to avoid having to write this entire template. I often use this architecture:
build.gradle
apt 'com.github.lukaspili.autodagger2:autodagger2-compiler:1.1' compile 'com.github.lukaspili.autodagger2:autodagger2:1.1'
YourApp.java
@AutoComponent( modules = YourApp.YourAppModule.class ) public class YourApp extends Application { private static YourApp instance; private YourAppComponent component; public YourAppComponent getComponent() { return this.component; } @Override public void onCreate() { super.onCreate(); setupComponent(); } private void setupComponent() { component = DaggerYourAppComponent.builder() .yourAppModule(new YourAppModule(instance)) .build(); } @dagger.Module public static class YourAppModule { private YourApp app; YourAppModule(YourAppApp application) { this.app = application; } @Provides @AutoExpose(YourApp.class) Application provideApplication() { return app; } @Provides @AutoExpose(PoswalaApp.class) Context provideContext() { return app; } @Provides @AutoExpose(YourApp.class) Retrofit provideApiAdapter() { return ApiService.getServiceInstance(); } } }
YourActivity.java
@AutoComponent( dependencies = YourApp.class, modules = YourActivity.YourActivityModule.class ) public class YourActivity extends BaseActivity implements YourActivityView { private YourActivityComponent component; @Inject MyPresenter presenter
Short description:
Your application module will have a βuniversalβ dependency, but in this way you can achieve the use of several modules for the class. You just need to configure
@AutoComponent( dependencies = YourApp.class, modules = { YourActivity.YourActivityModule.class, YourFragment.YourFragmentModule.class } )
block. You can add as many modules as you want using this syntax.
Hope this helps you
source share