Dependency Injection with Dagger2
Add the dependencies
dependencies {
...
// Add Dagger dependencies
compile 'com.google.dagger:dagger:2.12'
annotationProcessor 'com.google.dagger:dagger-compiler:2.12'
compile 'com.google.dagger:dagger:' + daggerVersion
// Using classes in dagger.android
compile 'com.google.dagger:dagger-android:2.12'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.12'
// Using classes in dagger.android with support libraries
compile 'com.google.dagger:dagger-android-support:2.12'
}
Fix conflict with dependency
Error:Conflict with dependency 'com.google.code.findbugs:jsr305' in project ':app'. Resolved versions for app (3.0.1) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.apply plugin: 'com.android.application' android { ... configurations.all { resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2' } }
InjectingActivityobjects
Create the application object and its component.
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); } }<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hsy.di"> <application android:name=".MyApp" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> ... </activity> </application> </manifest>@Component public interface MyAppComponent extends AndroidInjector<MyApp> { }Install
AndroidInjectionModulein the application component.@Component(modules = {AndroidInjectionModule.class}) public interface MyAppComponent extends AndroidInjector<MyApp> { }Define the subcomponent for
MainActivity.@Subcomponent interface MainActivitySubcomponent extends AndroidInjector<MainActivity> { @Subcomponent.Builder abstract class Builder extends AndroidInjector.Builder<MainActivity> {} }Defind a module that binds the subcomponent builder.
@Module(subcomponents = MainActivitySubcomponent.class) abstract class MainActivityModule { @Binds @IntoMap @ActivityKey(MainActivity.class) abstract AndroidInjector.Factory<? extends Activity> bindMainActivityInjectorFactory( MainActivitySubcomponent.Builder builder); }Add above module to the component that injects the
Application.@Component(modules = {AndroidInjectionModule.class, MainActivityModule.class}) public interface MyAppComponent extends AndroidInjector<MyApp> { }Make the
ApplicationimplementHasActivityInjector.public class MyApp extends Application implements HasActivityInjector { @Inject DispatchingAndroidInjector<Activity> dispatchingAndroidInjector; @Override public void onCreate() { super.onCreate(); DaggerMyAppComponent.create().inject(this); } @Override public AndroidInjector<Activity> activityInjector() { return dispatchingAndroidInjector; } }Call
AndroidInjection.inject(this).public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }Select Build > Make Project to trigger the generation of the code. Then
DaggerMyAppComponentwill be find.
Using Dagger 2 for dependency injection in Android - Tutorial