I'm using Dagger 2 in Android Studio, but when try to create Component through DaggerMyComponent.builder().myModule(new MyModule()).build(), I always see the word "builder()" in red and it says "cannot resolve symbol builder".
I must say that I've cleaned and builted many times the project, even commenting the component instance and decommenting it after rebuild. I must also say that I can inspect the DaggerMyComponent.class, and everything seems ok, in fact, I can import that class with no problem.
The example is shared on GithHub, at this link:
https://github.com/alessandroargentieri/LetsDagger2Together
I have two Classes: BClass and AClass which depends on the first one. I have ABModule.class which is the factory method and ABComponent which is the component interface. The DaggerABComponent is created into WholeApplication.class which extends Application.
package mawashi.alex.letsdagger2together.Application;
import android.app.Application;
import mawashi.alex.letsdagger2together.DaggerClasses.ABComponent;
import mawashi.alex.letsdagger2together.DaggerClasses.ABModule;
import mawashi.alex.letsdagger2together.DaggerClasses.DaggerABComponent;
public class WholeApplication extends Application {
static ABComponent component;
@Override
public void onCreate() {
super.onCreate();
//here is where builder() is not recognized
component = new DaggerABComponent.builder().aBModule(new ABModule()).build();
}
public static ABComponent getComponent(){
return component;
}
}
Module:
package mawashi.alex.letsdagger2together.DaggerClasses;
import dagger.Module;
import dagger.Provides;
import mawashi.alex.letsdagger2together.Model.AClass;
import mawashi.alex.letsdagger2together.Model.BClass;
@Module
public class ABModule {
@Provides
public BClass provideBClass(){
return new BClass("xxx");
}
@Provides
public AClass provideAClass(BClass bClass){
return new AClass(bClass);
}
}
Component:
package mawashi.alex.letsdagger2together.DaggerClasses;
import dagger.Component;
import mawashi.alex.letsdagger2together.MainActivity;
@Component (modules = {ABModule.class})
public interface ABComponent {
public void inject(MainActivity mainActivity);
}
Classes which are to be injected:
package mawashi.alex.letsdagger2together.Model;
public class BClass {
private String xfactor;
public BClass(String xfactor){
this.xfactor = xfactor;
}
public String getXfactor(){
return xfactor;
}
}
package mawashi.alex.letsdagger2together.Model;
public class AClass {
private BClass b;
public AClass(BClass b){
this.b = b;
}
public String getYfactor(){
return "Y-" + b.getXfactor() + "-Y";
}
}
Place where to inject AClass and BClass:
package mawashi.alex.letsdagger2together;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import javax.inject.Inject;
import mawashi.alex.letsdagger2together.Application.WholeApplication;
import mawashi.alex.letsdagger2together.DaggerClasses.ABModule;
import mawashi.alex.letsdagger2together.DaggerClasses.DaggerABComponent;
import mawashi.alex.letsdagger2together.Model.AClass;
public class MainActivity extends AppCompatActivity {
@Inject AClass a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WholeApplication.getComponent().inject(this);
Toast.makeText(this, a.getYfactor(),Toast.LENGTH_LONG).show();
}
}
If anyone knows what I mistake it would be very nice to me. Thanks to everybody.