dagger-2

How do I inject an abstraction using an IOC container like Dagger 2

前提是你 提交于 2020-01-05 05:54:23
问题 What I would like to do is to have an abstraction, and pass the abstraction as parameter. So that if one day something better than retrofit comes along and I want to switch it, it will be easy to do so. Although I have no idea on how do it. Here is a basic overview of what I want to do: public interface ApiClient{ Object getClient() } public class RetrofitClient implements ApiClient{ private static Retrofit retrofit = null; @Override public Retrofit getClient(){ if(retrofit == null){

How to avoid binding FragmentX to Fragment

╄→尐↘猪︶ㄣ 提交于 2020-01-05 04:36:10
问题 How to avoid binding FragmentX to Fragment I am having several files where I just declare the binding of a FragmentX to a Fragment (or ActivityX to Activity ) in order to be able to inject the objects as base class dependencies. Those files look like this @Module abstract class FragmentXModule { @Binds @FragmentScoped internal abstract fun bindFragment(fragmentX: FragmentX): Fragment } This is repeating over and over again. Is it possible to avoid those file creation repeat and group all

dagger2 GC overhead limit exceeded

血红的双手。 提交于 2020-01-05 04:04:10
问题 I'm following the Dagger2 sample of TODO app but encounted with OutOfMemoryError: GC overhead limit exceeded. Along with it are 600+ warning likes: warning: Ignoring InnerClasses attribute for an anonymous inner class (org.eclipse.osgi.internal.baseadaptor.BaseStorage$StateSaver$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class

Is the inject method a reserved name? how Dagger-2 knows to implement its body?

血红的双手。 提交于 2020-01-04 10:42:30
问题 I've seen this snippet: @Component(modules = {TestActivityModule.class}) public interface TestActivityComponent { void inject(TestActivity activity); } But the inject method is not implemented in user code (but auto-generated in Dagger-2 code). So is the inject a reserved name? how Dagger-2 knows to implement this method? 回答1: Ok, I got it: the name doesn't matter, it can be e.g. squeeze , as long as the provided type contains @Inject fields(s)/methods(s)/constructor(s), Dagger-2 will

How to inject dependencies in ViewModels using Dagger2

自古美人都是妖i 提交于 2020-01-04 05:57:35
问题 I am trying to implement new architecture (MVVM + RxJava2 + Dagger2 + Retrofit) in my existing project. I have set up whole above architecture and tested on HomeActivity. Dependencies injected in HomeViewModel. So Now I was trying to inject dependencies same as HomeViewModel in a FollowingViewModel of FollowingFragment which is a container Fragment of HomeActivity.But injected dependencies always return null(Not Initiziling). I am following this project riggaroo/android-arch-components-date

Why I still get “Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6”

*爱你&永不变心* 提交于 2020-01-04 05:55:09
问题 I am developing an Android project with Kotlin and Dagger 2. I have a NetworkModule it is supposed to provide a singleton instance of Retrofit. in which I define all those provider functions. All code snippet below are inside NetworkModule : @Module object NetworkModule { ... } I want to provide HttpRequestInterceptor , this is what I tried: @Provides @JvmStatic internal fun provideHttpRequestInterceptor(): Interceptor { // compiler error: Cannot inline bytecode built with JVM target 1.8 into

Injecting test module with dagger2

荒凉一梦 提交于 2020-01-03 13:31:09
问题 I use Dagger2 in my android app. Basically I inject a HttpClient (interface) in MainActivity . @Module public class MainActivityModule{ @Provides public HttpClient providesHttpComponent(){ return new RealHttpClient(); } } @Component( modules = MainActivityModule.class ) public interface MainActivityComponent { public MainActivity injectActivity(MainActivity); } public class MainActivity extends Activity { public void onCreate(Bundle saved){ super.onCreate(); injectDependencies(); } protected

Injecting Androidx Fragments using Dagger 2

隐身守侯 提交于 2020-01-03 07:06:08
问题 I want to inject my Androidx fragments using dagger 2. In my activity I have: public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject Reposetory reposetory; @Inject DispatchingAndroidInjector<androidx.fragment.app.Fragment> dispatchingAndroidInjector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public AndroidInjector<androidx.fragment.app

Dagger 2 create singleton instance

自古美人都是妖i 提交于 2020-01-03 03:01:36
问题 Consider the scenario that I am calling Webservice at Presenter A and holding the response data at the same Presenter. I want to utilize the same response data at Presenter E. But I cant pass the response object to each presenter B, C, D. So, I tried to store my response object at separate Holder class with getter & setter. I initialized Holder class using Dagger Inject constructor annotation and tried to consume it at Presenter E . But I got Empty response instead of my datas . Can any one

Dagger 2.0 Constructor injection and Singleton

天大地大妈咪最大 提交于 2020-01-02 06:13:09
问题 is it possible to use Dagger 2.0 Constructor injection and singleton at one time. I don't find the answer in the documentation. Example: @Singleton public class MyClass { private final OtherClass member; @Inject public MyClass(OtherClass member){ this.member = member; } } Constructor injection does work for sure. But is it guaranteed that MyClass is created as a singleton if I write @Singleton on the class? Thank you 回答1: Yes. Since Dagger 2 generates the source code for you, it is easy to