I've just added Dagger to my Android project. I've redesigned some of my classes to the Dependency Injection pattern and used the @Inject annotation on their constructors. I've also created corresponding modules and components. When I try to compile it, some of the @Provides methods from the modules give me this error:
error: @Provides methods cannot be static
All the methods that give me this error are @Singleton, static and have no parameters. I also have some @Provides methods that are @Singleton and static but have at least one parameter, and those don't give any errors.
Does anyone know what is the cause of these errors? Dagger 2 should support static @Provides methods, according to the official User's Guide.
@Module
public class DataModule {
@Provides
@Singleton
static MessageDatabaseAdapter provideMessageDatabaseAdapter() { //this method gives error
return new MessageDatabaseAdapter();
}
@Provides
@Singleton //this method seems OK
static MessageDataSource provideMessageDataSource(MessageDatabaseAdapter databaseAdapter) {
return new MessageDataSource(databaseAdapter);
}
@Provides
@Singleton
static ContactsDataSource provideContactsDataSource() { //this method also gives error
return new ContactsDataSource();
}
}
The problem was that I added an old version of Dagger to my project. In my case, it was 2.0, but at the time I wrote this, the newest version was 2.17, for which was the above mentioned User's Guide made.