Search code examples
androiddependency-injectiondaggerbutterknife

Dagger for Android - cant define injection for class


I cant seem to use the injects word when trying to define classes that will use my injections.

Here is my MainModule.java class:

import dagger.Module;
import dagger.Provides;


@Module()
public class MainModule {

    private Context context;

    public MainModule(Context context) {
        this.context = context;
    }


    @Provides
    Object provideSomething(Context context) {
        return new Object();
    }
}



@Module()
 class SubModule1 {

    private Context context;

    public SubModule1(Context context) {
        this.context = context;
    }

    @Provides
    Object provideSomethingElse(Context context) {
        return new Object();
    }
}

Here is my MainApplication class that extends application in android:

public class MainApplication extends Application {

    private ObjectGraph objectGraph;


    @Override
    public void onCreate() {
        super.onCreate();
        objectGraph = ObjectGraph.create(new MainModule(this));
        objectGraph = ObjectGraph.create(new SubModule1(this));
        objectGraph = ObjectGraph.create(new ActivityModule());
        objectGraph.inject(this);
}
    }

And here is the issue im having below, android studio wont see the injects keyword so i cant use it.

The ActivityModule.java class is below:

@Module(
        injects=
                ListPageActivity.class

) 
public class ActivityModule { }

Again its the injects keyword in ActiveModule that is not recognized in my IDE. Here is my gradle build dependency:

dependencies {
    compile files('libs/volley.jar')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.android.support:cardview-v7:21.0.0'
    compile 'com.jakewharton:butterknife:6.0.0'
    compile 'com.squareup:dagger:0.9.1'

}

Solution

  • Change your version of dagger. Everything seems to be OK for me with the posted code.

    compile 'com.squareup.dagger:dagger:1.2.2'
    

    The group id changed so know you can find the project on maven central here