0

I'm trying to use Dagger2 in my project, but after installation I have an error:

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

There are my Module and Component files:

import javax.inject.Singleton;

import dagger.Component;

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

    Context context();
}

Module:

@Module
public class ApplicationModule {
    public Context context;

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

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

I use the follow dependences:

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

Also, I have this error:

    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
Aleksandr Gorshkov
  • 473
  • 1
  • 6
  • 16
  • Presumably the Singleton annotation can't be used on an Interface because it's not a concrete type. – Reinstate Monica Dec 12 '16 at 15:41
  • This all _looks_ right. Is there another error somewhere in your build output? I could imagine that the processor isn't recognizing that `ApplicationComponent` is a `@Component` because something else is causing it to fail to compile. – gk5885 Dec 12 '16 at 20:10
  • There's nothing wrong with that as far as I can see. I do this all the time. My guess is that something else causes it to fail, like gk5885 said. – Fred Dec 13 '16 at 04:43
  • @gk5885 Yes, I have one another error, but I can't undersand what's wrong.I add it to the question – Aleksandr Gorshkov Dec 13 '16 at 06:45
  • 1
    That error suggests that you somehow have multiple versions of Dagger on your classpath. 2.8 is the latest version. I would make sure that you have that version and only that version on your annotation processor path. – gk5885 Dec 13 '16 at 17:09
  • @gk5885 maybe you know some libraties, which can cause thia error? I can show whole build.gradle file – Aleksandr Gorshkov Dec 14 '16 at 06:44
  • @rz0 get a list of dependencies from your app and you may be able to find the other library that has the Dagger dependency - you can do that following the instructions in [this answer](https://stackoverflow.com/questions/12288133/what-is-the-gradle-artifact-dependency-graph-command) – David Rawson Dec 24 '16 at 10:52

1 Answers1

0

What happens when you remove the @Singleton annotation from the ApplicationComponent?

The exception is telling you that dagger 2 does not support annotating components, but you can use the annotation like this:

@Singleton
Context context();
dazza5000
  • 7,075
  • 9
  • 44
  • 89