FAQ Database Discussion Community


Dagger 2 Custom Scope for each Fragment

android,android-fragments,dagger-2
I'm a little confused about how to best use the custom scoping in Dagger 2. I've looked at a couple different articles which seem to suggest two different ways of doing it: MVP Presenters that Survive Configuration Changes Part-2 (Github repo): Uses unique custom scopes for each fragment, e.g. @Hello1Scope...

Android Dagger 2 POJO field Inject null

android,dependency-injection,dagger-2
Just started using Dagger 2 today and I'm a bit confused on how exactly I need to set everything up. I'm trying to inject a POJO, but it's always null. First, some code: App.java private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent .builder() .appModule(new AppModule(this)) .build();...

Dagger2 component with more than one dependencies

java,android,dagger-2
This is what I currently have and it works: @FragmentScope @Component(dependencies = {FacebookComponent.class}, modules = {FragmentFacebookLoginModule.class}) public interface FragmentFacebookLoginComponent { void inject(FragmentFacebookLogin fragment); } Now I want to add another dependency. I changed it to this: @Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, modules = {FragmentFacebookLoginModule.class}) But now I get this error message:...

Dependency injection with Dagger2 on Android

java,android,dagger-2
Currently I'm not a fan of dependency injection but I want to give them a try. This is what I have: import android.os.Bundle; import com.facebook.CallbackManager; import rx.android.app.RxActivity; public class ActivityMain2 extends RxActivity { private CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); callbackManager = CallbackManager.Factory.create(); } } Now...

Unable to inject class with Dagger 2

android,dagger-2
I am attempting to add Dagger 2 to my Android Project. I think I understand the concepts up to the point of where I build the graph. At that point I'm shooting in the dark and that is where I'm going wrong. Everything compiles, but the injected field is null...

Dagger 2 error: dependency “cannot be provided without an @Inject constructor” while it actually annotated with @Inject

android,dagger,dagger-2
I've started using Dagger 2 and faced strange issue that looks like a bug to me. I have 3 modules, that are composed into one subcomponent, which in turn extends/pluses higher level component. Subcomponent is pretty simple: just combination of modules and a single injection point: @Singleton @Subcomponent( modules =...

Functions are missing from the Application object, when using multidex and Dagger 2

android,android-gradle,dagger-2,multidex,android-multidex
We've been having problems recently with Multidexing, specifically when building for the Dalvik VM using the gradle multidex plugin ('com.android.support:multidex:1.0.0'). It appears as if gradle has difficulty resolving the correct classes which should be included in the main dex file. As a result, we end up with a build process...

Context cannot be provided without an @Provides-annotated method, but it is?

android,dagger,dagger-2
I have the following simple module: @Module public class ApplicationModule { private CustomApplication customApplication; public ApplicationModule(CustomApplication customApplication) { this.customApplication = customApplication; } @Provides @Singleton CustomApplication provideCustomApplication() { return this.customApplication; } @Provides @Singleton @ForApplication Context provideApplicationContext() { return this.customApplication; } } And the respective simple component: @Singleton @Component( modules = ApplicationModule.class...

Gradle dependency - com.google.auto:auto-common:1.0-SNAPSHOT

maven,gradle,dagger-2,auto-value
How do I get com.google.auto:auto-common:1.0-SNAPSHOT (transitive dependency) to resolve, in my gradle build? build.gradle: apply plugin: 'java' repositories { maven { mavenLocal() mavenCentral() url "http://snapshots.maven.codehaus.org/maven2" url "http://oss.sonatype.org/content/groups/public" url "http://nativelibs4java.sourceforge.net/maven" url "http://repository.jboss.org/" } } dependencies { compile 'com.google.dagger:dagger:2.0-SNAPSHOT' compile 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT' compile...

Dagger2 Component as Static Global Variable

android,dagger,dagger-2
My Android app has multiple activities. The MainActivity constructs a Dagger2 component in its onCreate() method and assigns it to a static variable so any class can call the static method MainActivity.getDaggerComponent() to then inject itself with dependencies. The problem I'm discovering is that when I start up my VideoPlayerActivity,...

dynamic binding with dagger 2

android,dagger-2
I have problems migrating my Android app from Dagger to Dagger2. I use Dagger to inject mocks at different layers during testing (unit , functional device and everything in between). E.g. I have a NetworkingModule which provides a HttpTransport and a ServerConnector. For some test I just mock the http...

Do Dagger 2 and Android Annotations work together?

android,android-annotations,dagger-2
I've read here that Android Annotations has been updated to work with Dagger (1), but I am not sure if it automatically applies to Dagger 2 as well. Does anybody use AA and Dagger 2 together? Thanks....

Dagger2 module: how to obtain context to pass to the constructor of a class I want to provide

java,android,dagger-2
First time using Dagger2. In my android application I have a MyApplication class that extends Application. I also have an ImageAssistant class that is a collection of related image-processing methods. In my MyApplicaiton class I used to instantiate an ImageAssistant for all the activities to use. Now I am trying...

How to create an object using constructor injection?

dagger-2
How would I create an instance of Dog with an 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...

Dagger2 dependency - Gradle

android,android-gradle,dagger-2
I'm trying to add Dagger2 to my project in Android Studio but I can't find proper dependency to paste in build.gradle. Could you help and send me the proper line?

Android Unit Tests with Dagger 2

java,android,unit-testing,dagger-2
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...

java.lang.NoSuchMethodError on compile

java,android,butterknife,dagger-2,retrolambda
I'm trying to compile an Android project unsuccessfully. The error message is: Execution failed for task ':mobile:_compileAppDebug'. java.lang.NoSuchMethodError: com.google.auto.common.MoreTypes.asTypeElements(Ljavax/lang/model/util/Types;Ljava/lang/Iterable;)Lcom/google/common/collect/ImmutableSet; Here are my module's gradle dependencies in which I specify a number of libraries including google Auto: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':library') compile 'com.google.dagger:dagger:2.0-SNAPSHOT'...

Dagger 2 - unable to inject object

android,dependency-injection,dagger-2
Im trying to do a very simple dependency injection in a Android app. I am using dagger 2 as a DI tool. The issue is no injection is occuring: here is my code: //behold Motor.java in all its awe. public class Motor { private int rpm; public Motor(){ this.rpm =...

Gradle: force use of a repository for a dependency

android,gradle,dagger-2
I would like to know if there is a way to force gradle to use a repository for one dependency. For instance with: buildscript { repositories { jcenter() mavenCentral() maven { url 'https://www.testfairy.com/maven' } maven { url 'https://maven.fabric.io/repo' } maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } dependencies { classpath 'com.android.tools.build:gradle:1.0.1' classpath...

Problems with singletons when using component dependencies

java,dagger-2
I'm having a problem in understanding why the following code doesn't work. I have following project structure: @Component(modules = CCModule.class) public interface CComponent { XXX getXXX(); } where @Module public class CCModule { @Provides @Singleton public XXX provide XXX(){ return new XXX(); } } and @Component(dependencies = CComponent.class, modules =...

is @Singleton in dagger 2 thread safe?

java,android,multithreading,thread-safety,dagger-2
I'm trying to move everything in my app away from singletons, because I've been made aware that it's a bad programming practice, with that said, I'm looking into implementing Dagger 2 dependency injection. And I'm wondering, when you do @Singleton in Dagger 2 is that thread synchronized? if not how...

How to use Dagger's 2 @Named qualifier in Kotlin

android,kotlin,dagger-2
Recently I had a problem with @Named qualifier in Kotlin. I thought that changing from this: var boldTypeface: Typeface? = null [Inject] set into this var boldTypeface: Typeface? = null [Inject Named("bold")] set or var boldTypeface: Typeface? = null [Inject] [Named("bold")] set would solve my problem. But it didn't, it's...

Dagger 2.0 Constructor injection and Singleton

dependency-injection,dagger-2
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...

Break cyclic dependency in order to use dependency injection

java,dependency-injection,dagger-2
I just started using dagger 2 and have not used any other dependency injection framework before. Now I'm stuck with a cyclic dependency and I don't know how to solve it properly. Consider the following example in a server application, which uses the Reactor pattern with Java NIO: I have...

Dagger2 Custom Scopes : How do custom-scopes (@ActivityScope) actually work?

android,android-activity,scope,dagger-2
I am reading the source code for Dagger2 Component Scopes Test on GitHub, and I've seen a "custom scope" defined for activities called @ActivityScope, but I've seen it in other projects including the 4-module CleanArchitecture that has its @PerActivity scope. But literally, the code for the @ActivityScope annotation is the...

Dagger2 - null instead of injected object

android,dagger-2
For making things simple, suppose I want to inject EmailValidator from apache validators into my activity: public class MainActivity extends FragmentActivity { @Inject EmailValidator emailValidator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } I have a MainModule class: @Module public class MainModule { @Provides public EmailValidator providesEmailValidator() { return...