dagger-2

How do you provide a GoogleApiClient dependency with Dagger2?

ⅰ亾dé卋堺 提交于 2019-12-05 05:02:59
I've started using Dagger2 to manage dependencies and I'm trying to understand how I can use DI to provide a singleton GoogleApiClient. The motivations for this are: reduce boilerplate code : multiple Activities & Fragments require a GoogleApiClient improve testability : currently these Activities and Fragments are not well tested I want to provide a Singleton GoogleApiClient at the Application scope. How do you handle callbacks? Whether you choose an auto-managed or manually-managed connection , there are some callbacks that must be handled: GoogleApiClient.ConnectionCallbacks (manual only)

Kotlin + Dagger2: cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method

亡梦爱人 提交于 2019-12-05 04:39:38
I'm getting the following error: Error:(8, 1) error: java.lang.String cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. I'm stuck trying to make a module that provides two qualified Strings. Here is the simplified setup of dagger. @Singleton @Component(modules = [GreetingsModule::class]) interface AppComponent { fun inject(activity: MainActivity) } @Qualifier annotation class Spanish @Qualifier annotation class French @Qualifier annotation class English @Module @Singleton class GreetingsModule { @Provides @Spanish fun providesHola(): String

dagger cannot inject type parameter field

送分小仙女□ 提交于 2019-12-05 04:07:30
I'm working on an android application and I'm trying to inject a field which is type parameterized in an abstract class : BaseListFragment public abstract class BaseListFragment<E, A extends ArrayAdapter<E>, S> extends BaseFragment { @Inject protected S service; } But I get this following error at compile : error: cannot find symbol class S Here is my code for BaseFragment : public class BaseFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((App) getActivity().getApplication()).inject(this); } } here is my service

What are the advantages of using DispatchingAndroidInjector<> and the other dagger.android classes?

拈花ヽ惹草 提交于 2019-12-05 03:30:14
I'm working on setting up Dagger 2 into my android project. It is my first time with this framework and everything goes well so far. But I'm seeing different approaches on the way you can set up this framework in your project and I wonder which one is better, because I compare both and for me the result is kind of the same. I followed this guide: https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2 Searching on Internet all of them use this approach. It use @Module and @Component to define the dependencies. and your application ends up like this: public class

Dagger 2: how to change provided dependencies at runtime

南笙酒味 提交于 2019-12-05 01:44:53
In order to learn Dagger 2 i decided to rewrite my application but I'm stuck with finding the proper solution for the following problem. For the purpose of this example let's assume we have an interface called Mode : public interface Mode { Object1 obj1(); //some other methods providing objects for app } and two implementations: NormalMode and DemoMode . Mode is stored in singleton so it could be accessed from anywhere within application. public enum ModeManager { INSTANCE,; private Mode mode; public Mode mode() { if (mode == null) mode = new NormalMode(); return mode; } public void mode(Mode

Dagger 2 base class injections

末鹿安然 提交于 2019-12-05 01:03:13
问题 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

Dagger2 custom @Qualifier usage

时间秒杀一切 提交于 2019-12-05 00:30:11
Suppose I'm building a car and I have several Brake beans with different implementations class Car { @Inject Car(@BrakeType(value="abs")Brake frontBrake, @BrakeType(value="nonabs")Brake rearBrake) { } } @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface BrakeType { String value(); } interface Brake {} @BrakeType(value="abs") class AbsBrakeImpl implements Brake { @Inject AbsBrakeImpl() {} } @BrakeType(value="nonabs") class BrakeImpl implements Brake { @Inject BrakeImpl() {} } why does my CarModule have to define @Provides for the specific Brake types? Shouldn't the custom

dagger2 cannot find symbol

三世轮回 提交于 2019-12-04 20:45:32
I'm following the Dagger2 sample of TODO app but encounted with 2 errors. Error1: can't find symbol DaggerNetComponent . (Which is actually there) Error2: Sharedpreference can't be provided without @provider-annotated method.(Which I think results from error1) And here's my long but simple code: Three modules: @Module public class AppModule { private final Application mApplication; AppModule(Application application) { mApplication = application; } @Provides Application provideApplication() { return mApplication; } } @Module public class LoadingModule { public final LoadingContract.View mView;

How to inject current activity in a collaborator using Dagger 2.11 Android injector

不打扰是莪最后的温柔 提交于 2019-12-04 14:53:52
问题 I used to inject the current activity or activity context on some collaborators shared by several activities injecting the parent activity (BaseActivity or AppCompactActivity). But for some reason I cannot do it using Dagger 2.11 Android Injector. This is my Activity Module: @Module public class ActivityModule { private final Activity activity; public ActivityModule(Activity activity) { this.activity = activity; } @Provides @PerActivity ActColaborator provideActCollaborator() { return new

How can I replace Activity scoped dependencies with mocks using Dagger2

↘锁芯ラ 提交于 2019-12-04 12:40:51
I have a scoped dependency in my Activity and I want to test that activity with some mocks. I have read about different approach that suggest to replace Application component with a test component during the test, but what I want is to replace the Activity component. For example, I want to test the Activity against mock presenter in my MVP setup. I believe that replacing component by calling setComponent() on Activity will not work, because Activity dependencies already injected via field injection, so during the test, real object will be used. How can I resolve this issue? What about Dagger1?