0Dagger 2: MVVM unable to scope a module

Viewed 1793

I am new to the MVVM and Dagger and I am trying ot implement MVVM with Dagger 2 and Butterknife in my app. unfortunatelly I get the following error:

error: @Modules cannot be scoped. Did you mean to scope a method instead?

I get this error when I try to make my 'ViewModelModule' singleton. Here is my class:

@Singleton
@Module
public abstract class ViewModelModule { ... }

I am refering 'ViewModelModule' in my ' ApplicationModule ' which looks like so:

 @Singleton
 @Module(includes = ViewModelModule.class)
 public class ApplicationModule { ... }

If I remove the 'Singleton' annotation everithing works fine. But what am I missing? What am I doing wrong?

1 Answers

Modules mustn't be singletons, singletons could be the dependencies that modules provides. For instance:

@Module
public class ViewModelModule { 
    @Provides 
    @Singleton
    public String provideFoo() {
        return "Foo";
    }
}

So, remove @singleton annotation from your modules declaration.

Related