Android 0Dagger 2:MVVM无法确定模块的范围

Android 0Dagger 2:MVVM无法确定模块的范围,android,mvvm,dagger-2,Android,Mvvm,Dagger 2,我是MVVM和Dagger的新手,我正在尝试在我的应用程序中使用Dagger 2和Butterknife实现MVVM。不幸的是,我得到了以下错误: error: @Modules cannot be scoped. Did you mean to scope a method instead? 当我尝试将“ViewModelModule”设置为单例时,会出现此错误。这是我的班级: @Singleton @Module public abstract class ViewModelModule

我是MVVM和Dagger的新手,我正在尝试在我的应用程序中使用Dagger 2和Butterknife实现MVVM。不幸的是,我得到了以下错误:

error: @Modules cannot be scoped. Did you mean to scope a method instead?
当我尝试将“ViewModelModule”设置为单例时,会出现此错误。这是我的班级:

@Singleton
@Module
public abstract class ViewModelModule { ... }
我在“ApplicationModule”中引用了“ViewModelModule”,看起来是这样的:

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

如果我删除了“Singleton”注释,那么一切都很好。但我错过了什么?我做错了什么?

模块不能是单例,单例可以是模块提供的依赖项。例如:

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

因此,从您的模块声明中删除
@singleton
注释。

删除
@singleton
的modules@Onik但这会不会导致应用程序中出现问题?我看到了很多同时使用MVVM和Dagger 2的例子,其中每个例子中的“ViewModelModule”都是Singleton,我遵循这个指南,正如你所看到的,这个家伙正在将模块做成Singleton,这让我有点困惑。但是你确定模块不是单例是很好的吗?我从来没有在我的模块中使用过作用域,我不知道这是否有意义。我使用范围来定义组件/子组件范围,并定义模块提供的依赖项的范围。