Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger2 Error: Module Must Be Set

I was trying to do SubScoping in Dagger2. However, I am not able to figure out this compilation error:-> ...MyApplicationModule must be set which happens in my LogInFragment. If someone will try to throw some light on this error. I would really be glad.

This is MyApplication Class:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        MyInjector.initialize(this);
    }
}

This is MyInjector Class:

public enum MyInjector {
    INSTANCE;

    MyApplicationComponent myApplicationComponent;


    private MyInjector() {
    }

    public static void initialize(MyApplication myApplication) {

        MyApplicationComponent myApplicationComponent = DaggerMyApplicationComponent.builder()
                .myApplicationModule(new MyApplicationModule(myApplication))
                .build();
        INSTANCE.myApplicationComponent = myApplicationComponent;
    }

    public static MyApplicationComponent get() {
        return INSTANCE.myApplicationComponent;
    }

}

This is MyApplicationComponent Class:

@Component (modules = {MyApplicationModule.class}) 
public interface MyApplicationComponent { 

}

This is MyApplicationModule Class

@Module
public class MyApplicationModule {

    private final MyApplication myApplication;

    public MyApplicationModule(MyApplication myApplication) {
        this.myApplication = myApplication;
    }

    @Singleton
    @Provides
    SharedPreferences providesSharedPreferences(Context context) {
        return context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE);
    }

    @Singleton
    @Provides
    public Context providesMyApplicationContext() {
        return this.myApplication.getApplicationContext();
    }

    @Singleton
    @Provides
    public LocationManager providesLocationService(Context context) {
        return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    @Singleton
    @Provides
    public MyDatabaseManager providesMyDatabaseManager(Context context) {
        return MyDatabaseManager.getInstance(context);
    }

    @Singleton
    @Provides
    public AccountSystemModel providesAccountSystemModel(Context context) {
        return MyDatabaseManager.getInstance(context);
    }

    @Singleton
    @Provides
    public MyApplication providesMyApplication(){
        return this.myApplication;
    }

}

This is where I am trying to Subscope

This is MyLogIn Component Class

@Singleton
@Component(modules = {MyApplicationModule.class}, dependencies = {MyApplicationComponent.class})
public interface LogInComponent {
    LogInPresenter signInPresenter();
}

This is where the Compilation Error happens

This is MyLogInActivityFragment

@Override protected void injectDependencies() {
   logInComponent = DaggerLogInComponent.builder()
                    .myApplicationComponent(MyInjector.get())
                    .build();
}
like image 792
Kamil Kamili Avatar asked Jan 09 '17 15:01

Kamil Kamili


People also ask

What is dagger2 module?

Modules are a way of telling Dagger how to provide dependencies from the dependency graph. These are typically high level dependencies that you aren't already contributing to the dependency graph through the @Inject constructor annotation we discussed in our previous article.

What is Android dagger2?

Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

What is dagger2 used for?

Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.

How does dagger generate code?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.


2 Answers

Your LogInComponent depends on MyApplicationComponent which contains MyApplicationModule. You shouldn't be declaring this same module in the LogInComponent too. Remove it and it will compile.


Also, make sure you expose dependencies you need in the LogInComponent from MyApplicationComponent by adding them to the component interface like so:

@Component (modules = {MyApplicationModule.class}) 
public interface MyApplicationComponent { 
    Context context();
    SharedPreferences sharedPreferences();
    // ...
}

Another tip – if you need all dependencies from the MyApplicationComponent you might want to read about subcomponents.

like image 107
Egor Neliuba Avatar answered Sep 18 '22 15:09

Egor Neliuba


The error can be caused by an abstract class module. Modules can't be used by Dagger if they are abstract classes.

like image 42
dgngulcan Avatar answered Sep 18 '22 15:09

dgngulcan