dagger-2

Method injection using Dagger 2

做~自己de王妃 提交于 2019-11-27 11:33:30
问题 I haven't managed to find a good explanation/example on method injection using Dagger 2. Could someone please help me understand? Example: @Inject public Dinner makeDinner(Pasta pasta, Sauce sauce) { mPan.add(pasta); mPan.add(sauce); return mPan.cookDinner(); } So if I annotate my method with @Inject , am I correct to assume that the arguments in the method signature will be injected with defined objects from the object graph? How can I use this method in my code then? It will still expect me

RuntimeException with Dagger 2 on Android 7.0 and Samsung devices

喜欢而已 提交于 2019-11-27 11:03:20
问题 On my Google Play console I see quite a lot crash reports since I started to use Dagger 2, but only on Android 7.0 and mainly on Samsung devices, some Huawai and Motorola devices and some rare Xperia devices: java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2984) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3045) at android.app.ActivityThread.-wrap14 (ActivityThread.java) at android.app.ActivityThread$H

Android lifecycle library ViewModel using dagger 2

╄→尐↘猪︶ㄣ 提交于 2019-11-27 10:47:41
I have a ViewModel class just like the one defined in the Connecting ViewModel and repository section of Architecture guide . When I run my app I get a runtime exception. Does anyone know how to get around this? Should I not be injecting the ViewModel? Is there a way to tell the ViewModelProvider to use Dagger to create the model? public class DispatchActivityModel extends ViewModel { private final API api; @Inject public DispatchActivityModel(API api) { this.api = api; } } Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor at java.lang.Class

Android Unit Tests with Dagger 2

谁都会走 提交于 2019-11-27 10:16:04
问题 I have an Android app that uses Dagger 2 for dependency injection. I am also using the latest gradle build tools that allow a build variant for unit testing and one for instrumentation tests. I am using java.util.Random in my app, and I want to mock this for testing. The classes I'm testing don't use any Android stuff, so they're just regular java classes. In my main code I define a Component in a class that extends the Application class, but in the unit tests I'm not using an Application . I

Dagger 2.2 component builder module method deprecated

不羁的心 提交于 2019-11-27 09:40:41
问题 I started using dagger 2.2 and the module methods in the Component builder are deprecated. This is my Application component : @Component(modules = ApplicationModule.class) public interface ApplicationComponent { void inject(Application application); } And the Application module: @Module public class ApplicationModule { Application application; public ApplicationModule(Application application) { this.application = application; } @Provides @Singleton Application providesApplication() { return

Why is my field `null` after injection? How do I inject my object?

微笑、不失礼 提交于 2019-11-27 09:35:28
This is a Canonical Question because there are a lot of misconceptions about object initialization with Dagger 2. If your question was flagged as a duplicate please read this post carefully and make sure to understand the difference between constructor injection and field injection. I try to inject a Context into my presenter, but I get a NullPointerException when trying to use it. class MyPresenter { @Inject Context context; private MyView view; @Inject MyPresenter(MyView view) { this.view = view; } } My module looks like this @Module class MyModule { @Provides MyPresenter provideMyPresenter

Update to Kotlin 1.3.30 breaks build with Dagger 2.21

我们两清 提交于 2019-11-27 09:24:00
Build error after update from Kotling 1.3.21 to 1.3.30: AppComponent.java:16: error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>, javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method. Reproduced on two different projects with similar dependencies on Kotlin, Dagger and Architecture components. I suspect it somehow related to the recent kapt updates in kotlin 1.3.30: https://blog.jetbrains.com/kotlin/2019/04/kotlin-1-3-30-released/ Tried to disable/enable the kapt options from the article,

Dagger- Should we create each component and module for each Activity/ Fragment

好久不见. 提交于 2019-11-27 08:58:18
问题 I've been working with dagger2 for a while. And I got confused wether to create an own component/module for each Activity/ Fragment. Please help me clarify this: For example, We have an app, and the app has about 50 screens. We will implement the code following the MVP pattern and Dagger2 for DI. Suppose that we have 50 activities and 50 presenters. In my opinion, usually we should organize the code like this : Create an AppComponent and AppModule which will provide all objects that will be

Dagger not generating components for /test class

大兔子大兔子 提交于 2019-11-27 07:44:12
I am following the guide here: https://github.com/ecgreb/dagger-2-testing-demo I have the following setup in my app/src/main (the injection and @Provides code omitted): public class FlingyApplication extends Application { @Singleton @Component(modules = { FlingyModule.class }) public interface FlingyComponent } @Module public class FlingyModule In app/src/test: public class TestFlingyApplication extends Application { @Singleton @Component(modules = { TestFlingyModule.class }) public interface TestFlingyComponent extends FlingyComponent } @Module public class TestFlingyModule So far, it is

How to use dependency injection for injecting constructor in a ViewModel

做~自己de王妃 提交于 2019-11-27 06:24:10
问题 I am trying to implement the example on https://developer.android.com/jetpack/docs/guide. This explains how tan android app should be structured. When I use the same code, I get following error. java.lang.Class<com.example.UserProfileViewModel> has no zero argument constructor I could figure out that this error has something to do with viewModel = ViewModelProviders.of(this).get(UserProfileViewModel.class); When I write a default zero input constructor for ViewModel I get the following error.