Dagger2 Qualifier not working with Kotlin?

后端 未结 3 967
独厮守ぢ
独厮守ぢ 2020-12-17 19:24

I have a simple class as below

class MainString(val msg: String)

I want to inject with different argument to it, so I use the @Named<

3条回答
  •  温柔的废话
    2020-12-17 19:52

    1) If you are using a qualifier like following, here 'OmdbService'

    @Qualifier
    public annotation class OmdbService
    

    Then use

    @Inject  @field:OmdbService lateinit var retrofitOmdbService: Retrofit
    

    2) If are using a named provider like following, here 'orangeservice_retrofit'

    @Provides
        @OrangeApplicationScope
        @Named("orangeservice_retrofit")
        fun retrofit(okHttpClient :OkHttpClient, gson : Gson, c :Context): Retrofit {
            return Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(okHttpClient)
                    .baseUrl(c.getString(R.string.base_url))
                    .build()
    }
    

    Then use

    @Inject @field:Named("orangeservice_retrofit") lateinit var retrofitOrangeService: Retrofit
    

提交回复
热议问题