Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swordfighting-with-dagger

 swordfighting-with-dagger

Here,Idorenyin Obong explains the basics concepts of dependency injection with Dagger 2

Android Nigeria Community

February 19, 2018
Tweet

More Decks by Android Nigeria Community

Other Decks in Programming

Transcript

  1. Dependency Injection • It is a design pattern. • We

    are not initializing anything. • Giving an object its instance variables. - James Shore
  2. // before dependency injection public class Nsikak { AndroidNigeria androidNigeria

    = new AndroidNigeria(); public Nsikak() {} void teachMVVM(){ androidNigeria.submitC4P(); } }
  3. // with dependency injection public class Nsikak { AndroidNigeria androidNigeria;

    public Nsikak(AndroidNigeria androidNigeria) { this.androidNigeria = androidNigeria; } void teachMVVM(){ androidNigeria.submitC4P(); } }
  4. The Module class • Must be annotated with @Module •

    Should contain methods annotated with @Provides or @Binds
  5. The Module class • Must be annotated with @Module •

    Should contain methods annotated with @Provides or @Binds
  6. The Module class Must be annotated with @Module Should contain

    methods annotated with @Provides or @Binds
  7. Crash course on Dagger2 • How you provide dependencies -

    by using a Module class • How you request dependencies - by using the @Inject annotation
  8. Crash course on Dagger2 • How you provide dependencies -

    by using a Module class • How you request dependencies - by using the @Inject annotation • What happens between the injection and the Module ?
  9. The Component Interface • It is an interface - obviously.

    • It is annotated with @Component. • It manages all your modules.
  10. The Component Interface • It is an interface - obviously.

    • It is annotated with @Component. • It manages all your modules. • At runtime, Dagger generates a class that implements this interface.
  11. The Component Interface • It is an interface - obviously.

    • It is annotated with @Component. • It manages all your modules. • At runtime, Dagger generates a class that implements this interface. • The generated class provides injected instances of the app’s modules.
  12. Code implementation 1. Create a new Android project. 2. Add

    Dagger’s dependencies. implementation 'com.google.dagger:dagger:2.14.1' annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' // dagger android implementation 'com.google.dagger:dagger-android-support:2.14.1' annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'
  13. @Module public abstract class AppModule { @Provides @Singleton static ApiService

    providesWebService () { Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(BuildConfig.END_POINT) .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder .client(providesOkHttpClientBuilder()) .build(); return retrofit.create(ApiService.class); } }
  14. Code implementation 3. Add dependencies to the graph - create

    module classes. 4. Setup the component interface.
  15. @Component(modules = {AppModule.class, AndroidSupportInjectionModule.class}) public interface AppComponent extends AndroidInjector<AppController> {

    @Override void inject(AppController instance); @Component.Builder interface Builder { @BindsInstance AppComponent.Builder application(AppController application); AppComponent build(); } }
  16. Code implementation 3. Add dependencies to the graph - create

    module classes. 4. Setup the component interface. 5. Configure the Application class.
  17. public class AppController extends Application implements HasActivityInjector { @Override public

    void onCreate() { super.onCreate(); DaggerAppComponent.builder().application(this) .build().inject(this); } }
  18. public class AppController extends Application implements HasActivityInjector { @Inject DispatchingAndroidInjector<Activity>

    activityDispatchingAndroidInjector; @Override public void onCreate() { super.onCreate(); DaggerAppComponent.builder().application(this) .build().inject(this); } @Override public AndroidInjector<Activity> activityInjector() { return activityDispatchingAndroidInjector; } }
  19. Code implementation 3. Add dependencies to the graph - create

    module classes. 4. Setup the component interface. 5. Configure the Application class. 6. Prepare your activities to receive dependencies.
  20. public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Override public

    AndroidInjector<Fragment> supportFragmentInjector() { return supportFragmentInjector; } }
  21. public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject DispatchingAndroidInjector<Fragment>

    supportFragmentInjector; @Override public AndroidInjector<Fragment> supportFragmentInjector() { return supportFragmentInjector; } }
  22. public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject DispatchingAndroidInjector<Fragment>

    supportFragmentInjector; @Override protected void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); } @Override public AndroidInjector<Fragment> supportFragmentInjector() { return supportFragmentInjector; } }
  23. @Module public abstract class AppModule { @ContributesAndroidInjector abstract MainActivity mainActivity();

    @Provides static ApiService providesWebService () { Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(BuildConfig.END_POINT) .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder .client(providesOkHttpClientBuilder()) .build(); return retrofit.create(ApiService.class); }
  24. Code implementation 3. Add dependencies to the graph - create

    module classes. 4. Setup the component interface. 5. Configure the Application class. 6. Prepare your activities to receive dependencies. 7. Call your dependencies.
  25. public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject AndroidNigeria

    androidNigeria; @Inject ApiService apiService; @Inject DispatchingAndroidInjector<Fragment> supportFragmentInjector; @Override protected void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); androidNigeria.submitC4P(); } @Override public AndroidInjector<Fragment> supportFragmentInjector() { return supportFragmentInjector; } }
  26. Why is it necessary to use Dagger? • It creates

    a dependency graph at compile time. • Offers constructor, field, and method injection.