dagger-2

Kotlin dagger 2 Android ViewModel injection error

拥有回忆 提交于 2019-12-03 17:09:30
I'm trying to use dagger 2 on my Android application to inject the new ViewModel from arch android library. From what I see on this sample https://github.com/googlesamples/android-architecture-components/tree/e33782ba54ebe87f7e21e03542230695bc893818/GithubBrowserSample I need to use this: @MustBeDocumented @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) @Retention(AnnotationRetention.RUNTIME) @MapKey internal annotation class ViewModelKey(val value: KClass<out ViewModel>) @Module abstract class ViewModelModule

Dagger 2 base class injections

♀尐吖头ヾ 提交于 2019-12-03 16:45:39
In Dagger 1 I had a base class setup such that it would handle creating a scoped graph and injecting dependencies into the current object. For example... public abstract class MyBaseActivity extends Activity { private ObjectGraph graph; protected void onCreate(Bundle savedInstanceState) { graph = ((MyApp) getApplication()).plus(getModules()); graph.inject(this); } protected Object[] getModules(); } public class MyClass extends MyBaseActivity { @Inject SomeDep someDep; @Override protected Object[] getModules() { return new Object[/* Contains a module that provides SomeDep */]; } } This allowed

Binding Into Map With KClass Type

只愿长相守 提交于 2019-12-03 15:04:06
问题 I am trying to bind subclasses of ViewModel into a map by their KClass types: @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(MyViewModel::class) abstract fun bindsMyViewModel(viewModel: MyViewModel): ViewModel @Binds abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory } But I am getting Dagger compiler error: e: ~/Example/app/build/tmp/kapt3/stubs/debug/com/example/app/injection/AppComponent.java:5: error: [dagger.android

View dependency injection with dagger 2

ⅰ亾dé卋堺 提交于 2019-12-03 13:48:10
I have a custom view extending a TextView . Where should I call my component to inject the view? component.inject(customTextView); So, I've find out that I need to add the injection in the constructor of my custom view (in all of them, or make one call the other) Example: public class CustomTextView extends TextView { @Inject AnyProvider anyProvider; public CustomTextView(Context context) { this(context, null); } public CustomTextView(Context AttributeSet attrs) { super(context, attrs); Application.getComponent(context).inject(this); } } My general solution for that kind of thing is this

Android Mocking a Dagger2 injected dependency for a Espresso test

徘徊边缘 提交于 2019-12-03 13:29:00
I have a heavily dependency injected ( dagger2 ) application. I would like to run an espresso test without having the test navigate through the whole application, and log into the application. I would like to start on my teleActivity, and mock the login manager. However in any @test function, we have already hit the null pointer as we have called onCreate. If I override it before we launch the activity (show below) the activity is null. To my understanding, the ability to switch our underlining dependencies is a large reason why we use Dagger2, else it would be just a very over engineered

Dagger 2 Build IllegalArgumentException compileDebugJavaWithJavac

末鹿安然 提交于 2019-12-03 12:13:18
问题 I have been testing out Dagger 2, and everything had been working, until I did a bit of refactoring. Now gradle is throwing an IllegalArgumentException , and I cannot figure out what I changed that is now causing the error. I haven't made any changes to the gradle file, and this seems to be the brunt of the stack trace: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':mobile:compileDebugJavaWithJavac'. at org.gradle.api.internal.tasks.execution

How to create an object using constructor injection?

爱⌒轻易说出口 提交于 2019-12-03 11:53:18
问题 How would I create an instance of Dog with a component which provides Cat. public final class Dog { private final Cat mCat; public final static String TAG = "Dog"; @Inject public Dog(Cat cat) { mCat = cat; Log.e(TAG, "Dog class created"); } } After experimenting with Dagger 2 for a while I have no idea how to use constructor injection – a hint would be nice, thanks. Edit: Whats wrong with the question? After using Dagger 2, following several tutorials and reading the official documentation I

Dagger 2 on Android: inject same dependency in Activity and retained Fragment

百般思念 提交于 2019-12-03 11:39:42
问题 I have objects of classes F1 and F2 that I want to inject in a retained Fragment. I also have an object of class A that depends on Activity, and I want it to be injected in that Activity and in a retained Fragment attached to that Activity's Fragment Manager. I write the following code. First, the module for the Activity dependency: @Module public class MainActivityModule { private Activity mActivity; public MainActivityModule(Activity activity) { mActivity = activity; } @Provides

Dagger 2 constructor injection in kotlin with Named arguments

老子叫甜甜 提交于 2019-12-03 10:47:45
问题 I have this dependency: @Singleton class SpiceMix @Inject constructor(@field:[Named("oregano")] private val oregano: Spice, @field:[Named("sage")] private val sage: Spice, @field:[Named("rosemary")] private val rosemary: Spice) And a module to fulfill its dependencies: @Module class SpiceModule { @Provides @Named("oregano") @Singleton fun provideOregano(): Spice = Oregano() @Provides @Named("sage") @Singleton fun provideSage(): Spice = Sage() @Provides @Named("rosemary") @Singleton fun

dagger android support to androidx.fragment

老子叫甜甜 提交于 2019-12-03 10:22:38
How to inject a fragment from the package androidx.fragment.app.Fragment ? I'm using the dagger-android framework to inject my dependencies in my code. As the documentation says I do this to inject my fragment @Override public void onAttach(Activity activity) { AndroidInjection.inject(this); super.onAttach(activity); // ... } the problem is that AndroidSupportInjection class accept only fragments of the package android.support.v4.app.Fragment or if I use AndroidInjection class only accept fragments of the package android.app.Fragment and I want to use fragments of the androidx.fragment.app