2
votes

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.

2
please share your code to see what is missing..Matias Elorriaga
look at GitHub link written above... expecially the classes ABModule, ABComponent, WholeApplication... thanks!!Alex Mawashi

2 Answers

8
votes

Oh, I've figured it out.

You added this line:

    component = new DaggerABComponent.builder().aBModule(new ABModule()).build();

And it should be this line:

    component = DaggerABComponent.builder().aBModule(new ABModule()).build();

So just remove new and then it'll work.

-1
votes

All fields with @Inject annotation must be public: @Inject public AClass a; in your Activity