dagger-2

Dependencies from two components in one activity

我的未来我决定 提交于 2019-12-04 12:15:45
问题 I'm playing with Dagger-2 to figure how to integrate it in our existing application and I'm facing something which I can't understand or I'm doing wrong. My situation : 3 API without any annotated constructor ( each one in its own file ) public class DbApi { public void doSomething(String username, String password) { } } public class RestApi { public void doSomething(String username, String password) { } } public class SocketApi { public void doSomething(String username, String password) { }

Disable Dagger injection in tests

删除回忆录丶 提交于 2019-12-04 12:02:14
I have the following LoginFragment that uses Dagger to inject its fields: class LoginFragment : DaggerFragment() { @Inject lateinit var viewModelFactory: ViewModelProvider.Factory override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this, viewModelFactory) .get(LoginViewModel::class.java) } I also have a corresponding test that mocks the LoginViewModel according to the documentation from Google: "You can create the fragment and provide it a mock ViewModel." @MediumTest @RunWith(AndroidJUnit4::class) class

Is “inject everything” a bad practice in Android?

偶尔善良 提交于 2019-12-04 11:44:16
Studying about dependency injection I found some approaches that suggests to inject everything and other saying that it's not necessary to do so . In my current project, my rule of thumb regarding Dependency Injection is " if the class was created by me, I make it injectable ". In other words only classes like SimpleDateFormat , ArrayList , HashMap are newables in my project. My intent doing this approach is that I can @Inject any class anywhere once calling Injector.getApplicationComponent().inject(this) in the Activity . Basically all my classes have a non-args constructor with @Inject . I

Why it still works without installing AndroidInjectionModule?

允我心安 提交于 2019-12-04 10:02:28
According to Dagger documentation about injecting activity objects , it says that installing AndroidInjectionModule in your application component. However, everything is fine without it. Does it means that I don't need to declare it? Under what circumstances will it be wrong? For example: Injected instance data class Food(val name: String) Module @Module class FoodModule{ @Provides fun provideFood(): Food { return Food("cholocate") } } BindingModule @Module abstract class MainActivityModule { @ContributesAndroidInjector(modules = [FoodModule::class]) abstract fun FoodShop(): MainActivity }

Dagger 2.10/2.11 injecting Activity failing

爱⌒轻易说出口 提交于 2019-12-04 09:46:54
问题 I have been trying to, unsuccessfully, inject the Activity in a class ViewUtils. I have followed a couple of different posts but I can't seem to understand what am I missing in my implementation. I know this is probably a repetition of the posts below and I really apologize for that but I honestly cannot see what am I missing. These are the posts I've found: Dagger 2.10 Android subcomponents and builders How to create custom scoped modules in dagger 2.10 https://google.github.io/dagger

CustomView dependency injection with dagger 2 (within activity scope)

二次信任 提交于 2019-12-04 08:23:51
问题 My question is similar to this. So for instance, I have a LiveData implementation: public class CustomLiveData extends LiveData<SomeEvent> { @Inject public CustomLiveData(@ActivityContext Context context) { //.... } } that I want to inject into a custom view: public class CustomView extends View { @Inject SomeApplicationProvider anyProvider; @Inject CustomLiveData dataProvider; // Getting @com.di.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides

Dagger 2 - how to avoid code repetition for injecting dependencies that require activity context

倖福魔咒の 提交于 2019-12-04 06:47:34
问题 The project I'm working on has a number of utility classes that require activity context. I don't want to have to declare a new @Provides method for each activity that uses the dependency. i.e. I don't want this: @Provides static Navigator providesNavigator(ActivityOne activity) { returns new Navigator(activity); } // ...and in another module @Provides static Navigator providesNavigator(ActivityTwo activity) { returns new Navigator(activity); } So instead I declare these utilities in a single

Android Dagger-2 how to provide the dependency for method parameters

江枫思渺然 提交于 2019-12-04 06:28:51
问题 I have a module FragmentModule @Module public class FragmentModule { @Provides public static PickerDashboardFragment providesPickerDashboard(int status, String name, Object someComplexObject) { PickerDashboardFragment fragment = new PickerDashboardFragment(); Bundle b = new Bundle(); b.putInt("status", status); b.putString("name", name); b.putInt("object", someComplexObject); fragment.setArguments(bundle); return fragment; } @Provides public static PickingFragment providesPickingFragment() {

Dagger 2 : How to bind Fragment Map in parent component from subcomponent for FragmentFactory

爷,独闯天下 提交于 2019-12-04 06:03:40
I have this Dagger 2 configuration: AppComponent.kt @Singleton @Component( modules = [ AndroidSupportInjectionModule::class, AppModule::class, ActivityBindingModule::class, ] ) interface AppComponent : AndroidInjector<AppApplication> { @Component.Builder abstract class Builder : AndroidInjector.Builder<AppApplication>() } ActivityBindingModule.kt @Module abstract class ActivityBindingModule { @ActivityScoped @ContributesAndroidInjector( modules = [ MainActivityModule::class, FragmentModule::class //a fragment factory for each activity ] ) internal abstract fun mainActivity(): MainActivity }

Dagger2 not resolving dependency all the way

牧云@^-^@ 提交于 2019-12-04 05:59:03
问题 I have a MainActivity which injects Presenter, presenter object injects interactor and interactor object injects APIHelper. All the providers of presenter, interactor and APIHelper are there in MainModule. @Module public class MainActivityModule { private final MainActivity activity; //Context context; public MainActivityModule (MainActivity activity) { this.activity = activity; } @Provides @Singleton public MainViewPresenter providesMainPresenter(){ return new MainPresenterImpl(activity); }