Dagger: @Named(...) qualifier not working with Firebase or Kotlin

Created on 20 Aug 2017  Â·  3Comments  Â·  Source: google/dagger

According to documentation:

If we need two different objects of the same return type, we can use the @Named qualifier annotation. You will define it both where you provide the singletons (@Provides annotation), and where you inject them (@Inject annotations)

However, with this Application class:

class App : Application() {

lateinit var component: AppComponent

override fun onCreate() {
    super.onCreate()
    component = DaggerAppComponent
            .builder()
            .mainModule(MainModule(this))
            .build()
}

and this module:

@Module
class MainModule(private val application: Application) {

@Provides
fun provideAppContext(): Context = application.applicationContext

@Provides
@Singleton
fun provideFirebaseDatabase(): FirebaseDatabase { // fixme: check if services are enabled
    val mFirebaseDatabase = FirebaseDatabase.getInstance()
    mFirebaseDatabase.setPersistenceEnabled(true)
    return mFirebaseDatabase
} 

@Provides
@Named("users")
@Singleton
fun provideUsersDatabase(context: Context, @Named("users") database: FirebaseDatabase): DatabaseReference = database.getReference(context.getString(R.string.firebase_database_users))

@Provides
@Named("questions")
@Singleton
fun provideQuestionsDatabase(context: Context, @Named("questions") database: FirebaseDatabase): DatabaseReference = database.getReference(context.getString(R.string.firebase_database_questions))

}

and this injecting:
@Inject @Named("users") lateinit var mFirebaseUsers: DatabaseReference @Inject @Named("questions") lateinit var mFirebaseQuestions: DatabaseReference

I get this error:

Error:com.google.firebase.database.DatabaseReference cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.

What am I doing wrong?

Most helpful comment

You need to target the annotation at the field by using @field:Named.

On Sat, Aug 19, 2017, 7:45 PM mskx42 notifications@github.com wrote:

According to documentation:

If we need two different objects of the same return type, we can use the @
Named qualifier annotation. You will define it both where you provide the
singletons (@provides annotation), and where you inject them (@Inject
annotations)

However, with this Application class:

class App : Application() {

lateinit var component: AppComponent

override fun onCreate() {
super.onCreate()
component = DaggerAppComponent
.builder()
.mainModule(MainModule(this))
.build()
}

and this module:

@module https://github.com/module
class MainModule(private val application: Application) {

@Provides
fun provideAppContext(): Context = application.applicationContext

@Provides
@Singleton
fun provideFirebaseDatabase(): FirebaseDatabase { // fixme: check if services are enabled
val mFirebaseDatabase = FirebaseDatabase.getInstance()
mFirebaseDatabase.setPersistenceEnabled(true)
return mFirebaseDatabase
}

@Provides
@Named("users")
@Singleton
fun provideUsersDatabase(context: Context, @Named("users") database: FirebaseDatabase): DatabaseReference = database.getReference(context.getString(R.string.firebase_database_users))

@Provides
@Named("questions")
@Singleton
fun provideQuestionsDatabase(context: Context, @Named("questions") database: FirebaseDatabase): DatabaseReference = database.getReference(context.getString(R.string.firebase_database_questions))

}

and this injecting:
@Inject @Named("users") lateinit var mFirebaseUsers: DatabaseReference
@Inject @Named("questions") lateinit var mFirebaseQuestions:
DatabaseReference

I get this error:

Error:com.google.firebase.database.DatabaseReference cannot be provided
without an @Inject https://github.com/inject constructor or from an
@provides- or @produces-annotated method.

What am I doing wrong?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/google/dagger/issues/848, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEZGSbNATVd1MYQYyk9g3jXiYIiZjks5sZ3OwgaJpZM4O8eRr
.

All 3 comments

You need to target the annotation at the field by using @field:Named.

On Sat, Aug 19, 2017, 7:45 PM mskx42 notifications@github.com wrote:

According to documentation:

If we need two different objects of the same return type, we can use the @
Named qualifier annotation. You will define it both where you provide the
singletons (@provides annotation), and where you inject them (@Inject
annotations)

However, with this Application class:

class App : Application() {

lateinit var component: AppComponent

override fun onCreate() {
super.onCreate()
component = DaggerAppComponent
.builder()
.mainModule(MainModule(this))
.build()
}

and this module:

@module https://github.com/module
class MainModule(private val application: Application) {

@Provides
fun provideAppContext(): Context = application.applicationContext

@Provides
@Singleton
fun provideFirebaseDatabase(): FirebaseDatabase { // fixme: check if services are enabled
val mFirebaseDatabase = FirebaseDatabase.getInstance()
mFirebaseDatabase.setPersistenceEnabled(true)
return mFirebaseDatabase
}

@Provides
@Named("users")
@Singleton
fun provideUsersDatabase(context: Context, @Named("users") database: FirebaseDatabase): DatabaseReference = database.getReference(context.getString(R.string.firebase_database_users))

@Provides
@Named("questions")
@Singleton
fun provideQuestionsDatabase(context: Context, @Named("questions") database: FirebaseDatabase): DatabaseReference = database.getReference(context.getString(R.string.firebase_database_questions))

}

and this injecting:
@Inject @Named("users") lateinit var mFirebaseUsers: DatabaseReference
@Inject @Named("questions") lateinit var mFirebaseQuestions:
DatabaseReference

I get this error:

Error:com.google.firebase.database.DatabaseReference cannot be provided
without an @Inject https://github.com/inject constructor or from an
@provides- or @produces-annotated method.

What am I doing wrong?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/google/dagger/issues/848, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEZGSbNATVd1MYQYyk9g3jXiYIiZjks5sZ3OwgaJpZM4O8eRr
.

You are just awesome, I totally forgot about this!

@Inject @field:Named("users") lateinit var mFirebaseUsers: DatabaseReference

did this case!

I had the same problem, and it worked.
thanks!

Was this page helpful?
0 / 5 - 0 ratings