dagger-2

Mock injected ViewModel

蓝咒 提交于 2019-12-12 05:48:50
问题 I need to run Android instrumented test on StarterActivity . Here is how it goes public class StarterActivity extends BaseActivity<ActivityStarterBinding> { @Inject protected StarterViewModel starterViewModel; @Override public int getContentView() { return R.layout.activity_starter; } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getApplicationComponent().inject(this); } //... } And I call for starterViewModel method in onStart.

Dagger2 what is the purpose of adding a scope tag to components?

拜拜、爱过 提交于 2019-12-12 01:45:10
问题 I've created a component and it only last for the lifetime of the activity. I did not use any scope annotations and only quick example of the life time of the component looks like this: public class MainActivity extends AppCompatActivity { private final String TAG = getClass().getSimpleName(); @Inject AlmondButter someAlmondButter; @Inject CashewSandwich sandwich; SandwichComponent sandwichComponent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate

Android Tests: use Dagger2 + Gradle

社会主义新天地 提交于 2019-12-12 01:36:15
问题 I understand how Dagger2 works, I understand it allows to easily swap dependencies, so we can use mocks for testing. Point is that I am not sure I understand how am I supposed to provide different Dagger2 Components implementations for testing and for debug/production. Would I need to create 2 Gradle productFlavors (e.g "Production"/"Test") that would contain 2 different Components definition? Or can I specify that I want to use the mock Component for test compile and the non mock Component

Android Java ViewModel with Dagger 2

☆樱花仙子☆ 提交于 2019-12-12 00:52:48
问题 I'm trying to use viewmodel with dagger 2. However, on orientation change, ViewModel is being recreated. I get SwipeRefreshLayout NullPointerException on featuredViewModel.getLoading.observe() . Where is my fail? Dagger 2 Fragment Module (ContributesAndroidInjector) @Module(includes = {ApplicationModule.class, PicassoModule.class}) public class FeaturedProjectsModule { @Provides FeaturedViewModel provideMainViewModel(FeaturedProjectsFragment fragment, ViewModelProvider.Factory factory) {

ClassCastException: Application object is not my custom application object

二次信任 提交于 2019-12-11 18:36:41
问题 Often in Android code we use this pattern of storing data in the Application object and then accessing it via casting as such: ((MyApplicationObject)getApplication()).getDaggerComponent() getApplication() should always be MyApplicationObject because I have specified it in the AndroidManifest.xml . However, when you deploy this code, sometimes it causes a ClassCastException and we get a different object instead. How can this be? What is the correct workaround for this issue? 回答1: Resolved in

Android error: cannot find symbol class DaggerAppComponent

蓝咒 提交于 2019-12-11 17:56:11
问题 My colleague developed an Application according to this repository and I should make some changes In my case After some changes and remove Google Room Library I faced this problem: error: cannot find symbol class DaggerApplicationComponent After several rebuilds still cannot generate Project dagger2Version = '2.20' implementation "com.google.dagger:dagger:$rootProject.dagger2Version" annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version" implementation "com.google

Dagger - Setting a Dynamic Value to SubComponent Builder [duplicate]

帅比萌擦擦* 提交于 2019-12-11 17:38:10
问题 This question already has answers here : Dagger 2.10 Android subcomponents and builders (2 answers) Closed last year . I want to set a value to my sub-component builder at the time of building it. If simple Dagger 2 is concerned, we can achieve like following: @UserScope @Subcomponent(modules = {UserModule.class, ActivityBuilder.class, NewEditQuotationModule.class}) public interface UserComponent { NewEditQuotationComponent.Builder getNewEditQuotationComponent(); void inject(UserManager

What is @AutoAnnotation for? How could it be used?

我只是一个虾纸丫 提交于 2019-12-11 17:07:18
问题 In https://dagger.dev/multibindings.html, I read about @AutoAnnotation. It has a reference to https://github.com/google/auto/blob/master/value/src/main/java/com/google/auto/value/AutoAnnotation.java. It is also mentioned in https://github.com/google/auto/blob/57dfad360306619a820e6aae4a14a1aa67c29299/value/userguide/howto.md#annotation I read about it, can't get to understand it. I manage to access it from my Android code using implementation 'com.google.auto.value:auto-value:1.5.2' kapt 'com

how to provide different handling function to the injected InfiniteScrollListener instance - in kotlin

流过昼夜 提交于 2019-12-11 16:29:21
问题 This is related to using Dagger2 to provide a type with params got error "cannot be provided without an @Provides-annotated method" mean Seems I find a work around (thanks @David Medenjak for explain the error), it works, but not sure if there is better way to do it, especially to provide the flexibility of proving different handling function to the injected InfiniteScrollListener instance. Current solution the handling function is hard coded in the PresentorModule. For that I don't think it

No injector factory bound for Class<>

感情迁移 提交于 2019-12-11 16:21:29
问题 I saw the same question in stack but they try to fix the error with the @ContributesAndroidInjector but in Dagger documentation says use @ContributesAndroidInjector is only optional, so here are my classes: My MainActivityComponent : @Subcomponent(modules = [ MainBuilder::class ]) @ActivityScope interface MainComponent: AndroidInjector<MainActivity>{ @Subcomponent.Factory interface Factory: AndroidInjector.Factory<MainActivity> } My AplicationBinder : @Module(subcomponents = [ MainComponent: