deepmindz
deepmindz

Reputation: 608

Dagger 2 build fails on single Inject annotation

I've been trying to figure this out now, for multiple hours, and any help would be appreciated.

So far my project has been building perfectly fine with Dagger 2. I added another instance field to one of my fragments with the @Inject annotation, added the void inject(MyFragment mf) method into the Component I was using. However, after adding this @Inject annotation I get an error saying that it cannot find symbol DaggerAppComponent or DaggerLoginComponent in my Application class (I've commented out the @Inject, and the build worked immediately afterwards).

I made sure to check that all of my modules' methods have the @Provides annotation just in case, and they're all fine.

Again, thanks in advance!

Here is my Application class that provides my components using static methods:

public class CleanupApplication extends Application {

private static AppComponent appComponent;
private static LoginComponent loginComponent;

public static AppComponent getAppComponent() {
    return appComponent;
}

public static LoginComponent getLoginComponent() {
    return loginComponent;
}

public static void setComponents(MainActivity ma) {
    appComponent= DaggerAppComponent.builder().appModule(new AppModule(ma)).build();
    //Update LoginComponent accordingly
    loginComponent= DaggerLoginComponent.builder().appComponent(appComponent).loginModule(
            new LoginModule()).build();
}

@Override
public void onCreate() {
    super.onCreate();
    LeakCanary.install(this);
}


}

and the LoginComponent:

@Component(dependencies = {AppComponent.class}, modules =   {LoginModule.class})
 public interface LoginComponent {
 void inject(LoginListFragment llf);
 void inject(LoginDialogFragment ldf);
 Logins logins();
 MyLogins myLogins();
 }

and the fragment that I'm injecting into that causes the error:

 public class LoginDialogFragment extends DialogFragment implements ExtendedFragment {
 final int MIN_PER_HOUR = 60, SEC_PER_MIN = 60, SEC_PER_MILLISEC =  1000;


ScrollView mDialogLayout;

@Inject  //This one works just fine, using an upstream component
Context ctx;

@Bind(R.id.login_dialog_date_picker)
DatePicker mDatePicker;
@Bind(R.id.login_dialog_time_picker)
TimePicker mTimePicker;
@Bind(R.id.login_dialog_first_name_edit_text)
EditText mFirstName;
@Bind(R.id.login_dialog_last_name_edit_text)
EditText mLastName;
@Bind(R.id.login_dialog_confirmation_code_edit_text)
EditText mConfirmationCode;



@Inject  //THIS IS WHERE IT FAILS
MyLogins loginList;

private int loginPosition;
private MyLogin login;
private MyLoginEvent loginEvent;
private OnSuccessfulCompletion onCompletion;


boolean mIsEdit;


private String mLoginType = null;

public LoginDialogFragment() {
    this(false, -1);
}
public LoginDialogFragment(boolean changeLogin, int pos) {
    this.mIsEdit = changeLogin;
    this.loginPosition=pos;
    CleanupApplication.getLoginComponent().inject(this); //WHERE I INJECT MY LOGINCOMPONENT TO MEMBER FIELDS

}

@Override
public Dialog onCreateDialog(Bundle bundle) {
    mDialogLayout = (ScrollView) getActivity().getLayoutInflater().inflate(R.layout.fragment_login_dialog, null);
    ButterKnife.bind(this, mDialogLayout);


    mDatePicker.setCalendarViewShown(false);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    dialogBuilder.setTitle(mLoginType);
    //dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_login_dialog, null));
    dialogBuilder.setView(mDialogLayout);
    //.setIcon(id)


    if (mIsEdit && loginPosition >= 0) {

        login= (SouthwestLogin) loginList.get(loginPosition);
        Date flightDate=login.getMyLoginEvent().getFlightDate();
        Calendar cal= Calendar.getInstance();
        cal.setTime(flightDate);
        mDatePicker.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)
                , cal.get(Calendar.DAY_OF_MONTH) );
    if(Build.VERSION.SDK_INT>=23) {
        mTimePicker.setHour(cal.get(Calendar.HOUR));
        mTimePicker.setMinute(cal.get(Calendar.MINUTE));
    } else {
        mTimePicker.setCurrentHour(cal.get(Calendar.HOUR));
        mTimePicker.setCurrentMinute(cal.get(Calendar.MINUTE));
    }

        dialogBuilder.setPositiveButton(R.string.dialog_save, dialogClickListener)
                .setNegativeButton(R.string.dialog_cancel, dialogClickListener);

    } else {

        dialogBuilder.setPositiveButton(R.string.dialog_submit, dialogClickListener).
                setNegativeButton(R.string.dialog_cancel, dialogClickListener); //TODO);
    }

    return dialogBuilder.create();
}



 }

EDIT: In by gradle build file, I am using the apt plugin and providing the glassfish annotations. Note, in android studio, while coding it asks to auto import the respective generated components, but when pressing the build button it results in this compile error.

Upvotes: 1

Views: 643

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81529

Normally, all you need to do is add the following dependency to your gradle file:

provided 'org.glassfish:javax.annotation:10.0-b28' //needed to resolve compilation errors

But we'd need the exact compilation errors and possibly your dependencies if the problem persists, and your LoginModule.

Your LoginModule probably does not provide MyLogins.

Upvotes: 2

Related Questions