Dagger 2编译错误

时间:2016-12-12 14:49:48

标签: android dagger-2

我试图在我的项目中使用Dagger2,但在安装后我遇到了错误:

Error:(14, 8) error: Scoping annotations are only allowed on concrete types and @Provides methods: ApplicationComponent

我的模块和组件文件:

import javax.inject.Singleton;

import dagger.Component;

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
    void inject(BaseActivity activity);

    Context context();
}

模块:

@Module
public class ApplicationModule {
    public Context context;

    public ApplicationModule(@NonNull Context context) {
        this.context = context;
    }

    @Provides
    @Singleton
    Context provideApplicationContext() {
        return this.context;
    }
}

我使用以下依赖:

apt 'com.google.dagger:dagger-compiler:2.7'
compile 'com.google.dagger:dagger:2.7'
provided 'javax.annotation:jsr250-api:1.0'

另外,我有这个错误:

    Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: dagger.internal.codegen.Util.isAnyAnnotationPresent(Ljavax/lang/model/element/Element;Ljava/lang/Iterable;)Z

1 个答案:

答案 0 :(得分:0)

从ApplicationComponent中删除@Singleton注释会发生什么?

例外情况是告诉你dagger 2不支持注释组件,但你可以使用这样的注释:

@Singleton
Context context();