Duenytz Sierra
Duenytz Sierra

Reputation: 25

Kotlin+Dagger2 @Named annotation in Module provider method usage

I’m having a problem using Dagger 2 @Named annotation in Kotlin that is preventing me from migrating Dagger graph to Kotlin. The problem occurs when I need to inject in a Dagger module method a @Named parameter. In this case I’m not injecting it through a constructor or a field. I’ve tried all Kotlin annotation use-sites targets and none of them seems to work in a method parameter. Please, any solution will be very much appreciated. Below is the portion of java code that once converted to Kotlin won't compile:

@Module
public final class MyModule {

   (...)

   @Provides
   @Singleton
   LoginStore provideLoginStore(@Named("main_dao_session") DaoSession mainDaoSession, @Named("demo_dao_session") DaoSession demoDaoSession) {
      return new LoginStoreImpl(mainDaoSession, demoDaoSession);
   }

   (...)

}

Upvotes: 0

Views: 1261

Answers (1)

Kiskae
Kiskae

Reputation: 25603

use-site targets do not apply in this case, since you're dealing with function parameters. The target needs to be specified with constructors because a lot of code is generated in the background for each constructor parameters.

Just use the annotation as you normally would:

@Provides
@Singleton
fun provideLoginStore(@Named("main_dao_session") mainDaoSession: DaoSession, @Named("demo_dao_session") demoDaoSession: DaoSession): LoginStore {
    return LoginStoreImpl(mainDaoSession, demoDaoSession)
}

Upvotes: 1

Related Questions