Skip to content

Instantly share code, notes, and snippets.

@sagarnayak
Last active July 25, 2019 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagarnayak/cda76afe2205dc92da268414f1c493bf to your computer and use it in GitHub Desktop.
Save sagarnayak/cda76afe2205dc92da268414f1c493bf to your computer and use it in GitHub Desktop.
There are few changes needs to be done for the existing android projects with dagger after you update the version. this is the guide for that.

Changes to be made to your Android Dagger injection if you have updated to the latest 2.23.2

Scope Structure

@Scope
@Retention(RetentionPolicy.CLASS)
public @interface ApplicationScope {
}

Module Structure

@Module
public class SharedPreferenceModule {

    @ApplicationScope
    @Provides
    SharedPreferences sharedPreferences(Application application) {
        return application.getSharedPreferences(
                KeyWordsAndConstants.SHARED_PREFERENCE_DB_NAME,
                Context.MODE_PRIVATE
        );
    }
}

SubComponent needs to look like this -

@LoginScope
@Subcomponent(modules = {
        LoginModule.class
})
public interface LoginSubComponent extends AndroidInjector<Login> {
    @Subcomponent.Factory
    public interface Factory extends AndroidInjector.Factory<Login> {
    }
}

AppModule Structure

@Module(
        subcomponents = {
                SplashSubComponent.class,
                LoginSubComponent.class
        }
)
public class AppModule {
}

Builders Structure

@Module
public abstract class LoginActivityBuilder {

    @Binds
    @IntoMap
    @SuppressWarnings("unused")
    @ClassKey(Login.class)
    abstract AndroidInjector.Factory<?> bind(LoginSubComponent.Factory builder);
}

AppComponent Structure

@ApplicationScope
@Component(
        modules = {
                AndroidInjectionModule.class,
                AppModule.class,
                NetworkModule.class,
                RepositoryModule.class,
                LogUtilModule.class,
                SharedPreferenceModule.class,
                SplashActivityBuilder.class,
                LoginActivityBuilder.class
        }
)
public interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }

    void inject(ApplicationClass applicationClass);
}

Application Class Structure

public class ApplicationClass extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingAndroidInjector;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        DaggerAppComponent
                .builder()
                .application(this)
                .build()
                .inject(this);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment