dagger

Using Dagger2 with Lombok

喜夏-厌秋 提交于 2019-12-09 16:43:42
问题 Has anyone used Lombok 1.16 with Dagger2? My current code looks like: @AllArgsConstructor(onConstructor = @__(@Inject)) public class JuiceMaker { private final Apple apple; The error is: JuiceMaker cannot be provided without an @Inject constructor or from an @Provides-annotated method. Without the Lombok annotation this actually works, so: public class JuiceMaker { private final Apple apple; @Inject public JuiceMaker(Apple apple){ this.apple = apple } } works 回答1: This is a copy paste version

How to resolve a circular dependency while still using Dagger2?

筅森魡賤 提交于 2019-12-09 14:46:03
问题 I have two classes, Foo<T> and Bar , which depend on each other, as well as various other classes. I am using Dagger-2 for dependency injection, but if I naively add the circular dependency, Dagger hits a stack overflow at runtime. What's a good way to refactor the classes to fix this, while still using Dagger to inject all the other dependencies, and with minimal duplication and changes to existing calls? 回答1: The easy way out is to use Lazy<T> on one side. Lazy<Foo> foo; @Inject Bar(Lazy

Getting rid of Dagger 2 warning “Generating a MembersInjector”

邮差的信 提交于 2019-12-09 07:38:30
问题 Given the following classes abstract class AbstractClass { @Inject SomeDependency someDependency; } class SomeClass extends AbstractClass { @Inject AnotherDependency anotherDepenency; public void onCreate() { component = // Get component instance somehow component.inject(this); } } in Dagger 2 when injecting dependencies into a class which extends from an abstract base class which also contains dependencies, Dagger shows a warning of the kind Generating a MembersInjector for AbstractClass.

When should I use Factory<T> instead of Provider<T>

别来无恙 提交于 2019-12-08 17:45:24
问题 The Dagger documentation shows using a Provider<Filter> to get Filter instances, which appears to make perfect sense. I am writing a ListAdapter which instantiates Views, which I'd like Dagger to inject. I'm tempted to inject a Provider<ViewType> into my ListAdapter , and call mViewProvider.get() to instantiate views. However, the Dagger documentation says: Injecting Provider<T> has the possibility of creating confusing code, and may be a design smell of mis-scoped or mis-structured objects

Creating test dependencies when using Dagger2

送分小仙女□ 提交于 2019-12-08 15:46:49
问题 While reading the docs for dagger 2 I cannot find an easy way to provide a dependency when building an app for testing. The only clue I've found is this: Dagger 2 doesn't support overrides. Modules that override for simple testing fakes can create a subclass of the module to emulate that behaviour. Modules that use overrides and rely on dependency injection should be decomposed so that the overridden modules are instead represented as a choice between two modules. I don't understand how I

Dagger 2 - Why is this a dependency cycle?

巧了我就是萌 提交于 2019-12-08 15:40:33
问题 I'm trying to inject the application's Context into 2 other objects, an AuthManager and an ApiClient . Both of them depends on said context, and the ApiClient depends on the AuthManager . Why is this a dependency cycle, if Context doesn't have a reference to the others 2? can this be solved? EDIT: here is some code @Module public class AppModule { private final Application application; public AppModule(Application application) { this.application = application; } @Provides @Singleton Context

Error: cannot find symbol class DaggerAppComponent

帅比萌擦擦* 提交于 2019-12-08 11:48:31
问题 I downloaded several sample projects from github that use dagger, e. g. Moxy sample project (trying to run github-sample) but everywhere there is the same error - «cannot find symbol class DaggerAppComponent». I did not make any changes in the projects just downloaded and tried to run them. Gradle version - 3.1.2 AndroidStudio – 3.3 def dagger = '2.7' implementation "com.google.dagger:dagger:$dagger" annotationProcessor "com.google.dagger:dagger-compiler:$dagger" Does anybody has an idea how

In Dagger are Singletons within the sub-graph cached or will they always be recreated when a new activity sub-graph is constructed?

孤街醉人 提交于 2019-12-08 11:31:30
I'm using Dagger to create activity specific object graphs. Within this subgraph, I make use of a Singleton MyPresentationModel . When i exit my activity, and enter the activity again, my expectation is that a new instance of the activity specific object graph is created, which in turn would create a new instance of Singleton MyPresentationModel ( by virtue of the @Singleton semantic per Dagger. See this So answer for specifics ) which would then last for the life of the activity specific object graph. However, this is not what i'm observing, every time the activity specific object graph is

Dagger2 one module for two different scopes

走远了吗. 提交于 2019-12-08 08:17:35
问题 In my app i have an ApplicationScope which provides me with stuff like context and shared prefs. 2 sub components, LoggedInComponent and LoggedOutComponent. Those 2 subcomponents have other subcomponents which are less relevant to the issue. I have a NetworkModule which creates my retrofit instance. Both subcomponents need the NetworkModule but the retrofit might change during the login because the base url and other settings might change. I was wondering which approach it is better to take:

In Dagger are Singletons within the sub-graph cached or will they always be recreated when a new activity sub-graph is constructed?

与世无争的帅哥 提交于 2019-12-08 06:42:40
问题 I'm using Dagger to create activity specific object graphs. Within this subgraph, I make use of a Singleton MyPresentationModel . When i exit my activity, and enter the activity again, my expectation is that a new instance of the activity specific object graph is created, which in turn would create a new instance of Singleton MyPresentationModel (by virtue of the @Singleton semantic per Dagger. See this So answer for specifics) which would then last for the life of the activity specific