https://kotlinlang.org logo
#dagger
Title
# dagger
h

Hovhannes

10/01/2021, 5:59 AM
Hello, I've a question. I get this error, but I can't understand it. How can I fix it?  Thanks in advance.  "[Dagger/MissingBinding] @javax.inject.Named("vbttop") retrofit2.Retrofit cannot be provided without an @Provides-annotated method.   public abstract static class SingletonC implements App_GeneratedInjector,                          ^       @javax.inject.Named("vbttop") retrofit2.Retrofit is injected at           com.example.alwayson.di.NetworkModule.provideApiVbtop(retrofit)       com.example.alwayson.data.VbtopApi is injected at           com.example.alwayson.repository.VbtopRepository(vbtopApi)       com.example.alwayson.repository.VbtopRepository is injected at           com.example.alwayson.viewModel.MainVieModel(�, vbtopRepository, �)"
Copy code
@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {


    @Provides
    fun provideBuildlogger(): HttpLoggingInterceptor {
        return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
    }


    @Provides
    @Singleton
    fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build()
    } else {
        OkHttpClient
            .Builder()
            .build()
    }


    @Provides
    @Singleton
    @Named("base")
    fun provideRetrofitBuilder(
          alwaysonUrl: String, okHttpClient: OkHttpClient
    ): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(MoshiConverterFactory.create().asLenient())
            .baseUrl(alwaysonUrl)
            .client(okHttpClient)
            .build()
    }


    @Provides
    @Singleton
    @Named("vbtop")

    fun provideRetrofitBuilderVbttop(
        @Named("vbttop") vbttopUrl: String, okHttpClient: OkHttpClient
    ): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(MoshiConverterFactory.create())
            .baseUrl(vbttopUrl)
            .client(okHttpClient)
            .build()
    }


    @Provides
    @Singleton
    fun provideApi(@Named("base") retrofit: Retrofit): UrlApi {
        return retrofit.create(UrlApi::class.java)
    }


    @Provides
    @Singleton
    fun provideApiVbtop(@Named("vbttop") retrofit: Retrofit): VbtopApi {
        return retrofit.create(VbtopApi::class.java)
    }


    @Provides
    fun provideBase() = BuildConfig.BASE_URL

    @Provides
    @Named("vbttop")
    fun vbttopUrl(): String = ""
}
Copy code
interface UrlApi {
    @GET
    suspend fun getUrls(@Url path: String): Response<JsonData>
   
}
Copy code
interface VbtopApi {
    @GET
    suspend fun getVbtop(@Url url:String): Response<VbtopUrl>
}
Copy code
class MainRepository @Inject constructor(private  val urlApi: UrlApi, private  val vbtopApi: VbtopApi) {
    suspend fun getUrls(path:String) = urlApi.getUrls(path)

    suspend fun getVbtop(url:String)=vbtopApi.getVbtop(url)

}
e

ephemient

10/01/2021, 6:10 AM
Copy code
class MainRepository @Inject constructor(
    private val urlApi: UrlApi,
    @Named("vbtop")
    private val vbtopApi: VbtopApi
)
h

Hovhannes

10/01/2021, 6:14 AM
@ephemient Thanks for your reply. But now I get this error 😔 "error: [Dagger/MissingBinding] @javax.inject.Named("vbtop") com.example.alwayson.data.VbtopApi cannot be provided without an @Provides-annotated method. public abstract static class SingletonC implements App_GeneratedInjector, ^ @javax.inject.Named("vbtop") com.example.alwayson.data.VbtopApi is injected at com.example.alwayson.repository.MainRepository(�, vbtopApi) com.example.alwayson.repository.MainRepository is injected at com.example.alwayson.viewModel.MainVieModel(mainRepository, �) com.example.alwayson.viewModel.MainVieModel is injected at com.example.alwayson.viewModel.MainVieModel_HiltModules.BindsModule.binds(vm)"
e

ephemient

10/01/2021, 6:46 AM
oh I misread which type you were expecting. hmm, not sure
h

Hovhannes

10/01/2021, 7:42 AM
@ephemient I fix this error. But now I get this error "Expected URL scheme 'http' or 'https' but no scheme was found". But I don't know urls for the 2nd Retrofit, it's depends on what url response body the 1st Retrofit gets.
r

Rafal

10/01/2021, 11:11 AM
if your service will only use
suspend fun getVbtop(@Url url:String)
(you don’t add any other methods that will require a base url), you can simply provide anything like
<https://whatever.com/>
and the
@Url
param will override it
so, basically
Copy code
@Provides
@Named("vbttop")
fun vbttopUrl(): String = "<https://whatever.com/>"
should be enough
9 Views