skyshine
skyshine

Reputation: 2864

Constructor injection cannot find symbol method inject members

I'm working on an Android application which has dagger dependencies. When injecting class through constructor injection it's throwing an error that it cannot find symbol. If I provide the dependency through an @Provides method defined inside a module, everything works fine.

The code:

  public class SixthGenericTest {

        @Inject
        FirstTest firstTest;

        @Inject
        public SixthGenericTest()
        {
            Injection.create().getAppComponent().inject(this);
        }
        public String getData(){
            return firstTest.getTestName();
        }
    }

    @Singleton
    @Component(modules = {FirstModule.class})
    public interface AppComponent {
        void inject(SixthGenericTest sixthGenericTest);

    }

and the error I got:

Error:(19, 28) error: cannot find symbol method injectMembers(MembersInjector,SixthGenericTest)

Upvotes: 2

Views: 4687

Answers (2)

Pablo C. García
Pablo C. García

Reputation: 22394

Upgrade your dagger version

dependencies {
  compile 'com.google.dagger:dagger:2.x'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
}

Check last version here

Upvotes: 5

Samuel Eminet
Samuel Eminet

Reputation: 4737

You don't need to injectInjection.create().getAppComponent().inject(this); @Inject annotated constructeur is enough since it is automatically added to the graph, you may though add your scope above your class name

@Singleton
public class SixthGenericTest {

    FirstTest firstTest;

    @Inject
    public SixthGenericTest( FirstTest firstTest )
    {
        this.firstTest = firstTest;
    }

    public String getData()
    {
        return firstTest.getTestName();
    }
}

and remove from component:

 void inject(SixthGenericTest sixthGenericTest);

Upvotes: -2

Related Questions