Dagger: class could not be bound with key

Instead of commenting on what you're doing wrong, let's give the correct example, and explain what's happening there.

This snippet is perfect, and stays as it is:

public class Parrot {

    private Language language;

    @Inject
    public Parrot(Language language) {
        this.language = language;
    }

    public void sayHello() {
        System.out.println(language.getSalute());
    }
}

public interface Language {
    public String getSalute();
}

public class EnglishLanguage implements Language {

    @Override
    public String getSalute() {
        return "Hello!";
    }
}

With the @Inject annotation on the Parrot constructor, you're telling Dagger, "Whenever I request an instance of Parrot, use this constructor to instantiate it".

Dagger sees that Parrot needs an instance of the Language interface, and tries to find a way to retrieve it. However, since Language is an interface, Dagger needs to know which concrete type to use.

Since you cannot edit the EnglishLanguage class, you'll need to write a @Provider method for it:

@Module
public class PetshopModule {

  @Provides
  Language provideLanguage() {
    return SystemApp.getSystemLanguage();
  }
}

Now, you should be able to get an instance of Parrot out of your ObjectGraph, like this:

ObjectGraph graph = ObjectGraph.create(new PetshopModule());
Parrot parrot = graph.get(Parrot.class);

I have not tested this, bit this should work.


If you were able to modify the EnglishLanguage class, you could do the following. Annotate the EnglishLanguage constructor with @Inject, and request an EnglishLanguage instance in the Language provider method:

public class EnglishLanguage implements Language {

  @Inject
  public EnglishLanguage() {
  }

  @Override
  public String getSalute() {
    return "Hello!";
  }
}

@Module
public class PetshopModule {

  @Provides
  Language provideLanguage(EnglishLanguage language) {
    return language;
  }
}

In this case, Dagger looks for the Language provider method, and instantiates an EnglishLanguage to return.