Dagger 2 Named cannot be provided without the @Provides method

Trying to hook dagger 2 and have a problem with named providers. I have a simple setup:

// Module @Module class AppModule(private val app: App) { @Provides @AppScope fun providesApp() = app @Provides @AppScope fun provideSharedPreferences(app: App) = PreferenceManager.getDefaultSharedPreferences(app) @Provides @AppScope @Named("Uri1") fun providesUri1() = Uri.Builder().scheme("https").authority("authory1").build() @Provides @AppScope @Named("Uri2") fun providesUri2() = Uri.Builder().scheme("https").authority("authory2").build() } // Component @AppScope @Component(modules = arrayOf(AppModule::class)) interface AppComponent { fun inject(target: MainActivity) } // MainActivity @Inject @AppScope lateinit var preferences: SharedPreferences @Inject @AppScope @Named("Uri1") lateinit var uri1: Uri @Inject @AppScope @Named("Uri2") lateinit var uri2: Uri 

When restoring my project, they give me:

 Error:Gradle: android.net.Uri cannot be provided without an @Provides- or @Produces-annotated method. 

I do not understand why adding the Named classifier does not work for me. If I delete them, I can get an instance of SharedPreferences without any problems.

Any understanding of what I am doing wrong will be appreciated!

EDIT:

Changes in sentences with the same results as above.

 // New module @Module class AppModule(private val app: App) { @Provides @AppScope fun providesApp() = app @Provides @AppScope fun provideSharedPreferences(app: App) = PreferenceManager.getDefaultSharedPreferences(app) @Provides @AppScope @Tag("Uri1") fun providesUri1(): Uri = Uri.Builder().scheme("https").authority("authority1").build() @Provides @AppScope @Tag("Uri2") fun providesUri2(): Uri = Uri.Builder().scheme("https").authority("authority2").build() } // Tag annotation @Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class Tag(val tag: String = "") // MainActivity @Inject @AppScope lateinit var preferences: SharedPreferences @Inject @AppScope @Tag("Uri1") lateinit var uri1: Uri @Inject @AppScope @Tag("Uri2") lateinit var uri2: Uri 

Project Repo @Github

+7
android dependency-injection kotlin dagger-2
source share
1 answer

I think I found the problem (at least I checked your project and it created the dagger classes correctly). If you need to enter fields annotated with @Named or some @Qualifier annotation, you should use this syntax:

 class MainActivity : AppCompatActivity() { @Inject @AppScope lateinit var preferences: SharedPreferences @Inject @AppScope @field:[Named ("Uri1")] lateinit var uri1: Uri // for @Named annotation or... @Inject @AppScope @field:[Uri2] lateinit var uri2: Uri // ...for @Qualifier annotation override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) app().component.inject(this) println(uri1) println(uri2) } } 

Note that the @Named / qualifier annotation is inside @field: (without @ ).

An idea borrowed from this repo .

+15
source share

All Articles