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


i have 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 respective simple component:

@singleton @component(         modules = applicationmodule.class ) public interface applicationcomponent {      customapplication getcustomapplication();      context getapplicationcontext();  } 

and i'm creating component here:

public class customapplication extends application {      ...      private applicationcomponent component;      @override     protected void attachbasecontext(context base) {         super.attachbasecontext(base);         multidex.install(this);     }      @override     public void oncreate() {         super.oncreate();          component = daggerapplicationcomponent.builder()                 .applicationmodule(new applicationmodule(this))                 .build(); 

it throws error @ compile time: error:(22, 13) error: android.content.context cannot provided without @provides-annotated method, can see annotated @provides.

it's strange because problem goes away when take qualifier annotation off.

just in case, @forapplication qualifier:

@qualifier @retention(runtime) public @interface forapplication { } 

this practically textbook dagger2 example. doing wrong?

after quite while of trial , error i've seem found cause, it's ambiguity of context because @forapplication missing in places context needed.

also may frail understanding of dagger2 @ moment, boilerplate quite prone developer errors.

anyhow... finds problem have add qualifier annotations in every place dependency used:

@singleton @component(         modules = applicationmodule.class ) public interface applicationcomponent {      customapplication getcustomapplication();      @forapplication context getapplicationcontext();  } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -