Bharat Kul Ratan Bharat Kul Ratan - 5 months ago 126
Java Question

Dagger 2 giving error '...cannot be provided without an @Provides-annotated method'

I'm trying to build a simple android project for dagger 2 understanding. I currently have the component

MyComponent.java

@Component(modules = {MyModule.class})
public interface MyComponent {
MyModule provideModule();
}


MyModule.java

@Module
public class MyModule {
@Provides
Repo provideRepo(){
return new Repo();
}
}


Repo.java

public class Repo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


I've added these to build.gradle for app

apt 'com.google.dagger:dagger-compiler:2.2'
compile 'com.google.dagger:dagger:2.2'
provided 'javax.annotation:jsr250-api:1.0'


Also in dependencies for project I've used this

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'


When building the project, I'm getting this error

Error:(10, 14) error: com.example.bharatkulratan.daggernotworking.MyModule cannot be provided without an @Inject constructor or from an @Provides-annotated method.


The error reads out for missing
@Provided
annotation but
provideRepo()

method is already having the annotation of
@Provided
. I'm unable to figure out the missing part. Please help.

Answer

Remove this

MyModule provideModule();

And add this in its place

Repo provideRepo();
Comments