Error migrating code from Java to Kotlin: "cannot be provided without an @Provides-annotated method"

huangapple go评论64阅读模式
英文:

Error migrating code from Java to Kotlin: "cannot be provided without an @Provides-annotated method"

问题

I am trying to migrate my Java code to Kotlin.

I have problems with a Hilt module:

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Provides
    @Singleton
    fun provideRetrofit(
        gsonConverterFactory: GsonConverterFactory?,
        rxJava3CallAdapterFactory: RxJava3CallAdapterFactory?,
        okHttpClient: OkHttpClient?
    ): Retrofit {
        return Retrofit.Builder().baseUrl(Configuration.URL_API)
            .addConverterFactory(gsonConverterFactory)
            .addCallAdapterFactory(rxJava3CallAdapterFactory)
            .client(okHttpClient)
            .build()
    }

    @Provides
    @Singleton
    fun provideContext(application: Application): Context {
        return application
    }

    @Provides
    @Singleton
    fun provideHttpClient(application: Application?): OkHttpClient {
        val dispatcher = Dispatcher()
        // HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        val client = OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .dispatcher(dispatcher)
        client.addInterceptor(ConnectivityIntecepter(application))
        val interceptor = Interceptor { chain: Interceptor.Chain ->
            val original = chain.request()
            val request = original.newBuilder()
                .method(original.method, original.body)
                .build()
            chain.proceed(request)
        }
        client.addInterceptor(interceptor)
        // client.interceptors().add(logging);
        return client.build()
    }

    @Provides
    @Singleton
    fun provideGson(): Gson {
        return GsonBuilder().setLenient().create()
    }

    @Provides
    @Singleton
    fun providesGsonConverterFactory(): GsonConverterFactory {
        return GsonConverterFactory.create()
    }

    @Provides
    @Singleton
    fun providesRxJavaCallAdapterFactory(): RxJava3CallAdapterFactory {
        return RxJava3CallAdapterFactory.create()
    }

    @Provides
    @Singleton
    fun provideService(retrofit: Retrofit): ApiService {
        return retrofit.create(ApiService::class.java)
    }
}

When i try to compile i get the following error:

> .../app/build/generated/hilt/component_sources/debug/org/my/app/BaseApplication_HiltComponents.java:153:
> error: [Dagger/MissingBinding] org.my.app.data.db.dao.TodayDao cannot
> be provided without an @Provides-annotated method. public abstract
> static class SingletonC implements
> FragmentGetContextFix.FragmentGetContextFixEntryPoint,
> ^ org.my.app.data.db.dao.TodayDao is injected at
> org.my.app.workers.TodayWorker.mTodayDao org.my.app.workers.TodayWorker_AssistedFactory is injected at
> org.my.app.workers.TodayWorker_HiltModule.bind(factory) java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.work.WorkerAssistedFactory<?
> extends androidx.work.ListenableWorker>>> is injected at
> androidx.hilt.work.WorkerFactoryModule.provideFactory(workerFactories)
> androidx.hilt.work.HiltWorkerFactory is injected at
> org.my.app.BaseApplication.workerFactory org.my.app.BaseApplication is injected at
> org.my.app.BaseApplication_GeneratedInjector.injectBaseApplication(org.my.app.BaseApplication)
> It is also requested at:
> org.my.app.repository.BreviarioRepository(…, todayDao, …)
> org.my.app.repository.ComentariosRepository(…, todayDao)
> org.my.app.repository.HomiliasRepository(…, todayDao)
> org.my.app.repository.LecturasRepository(…, mTodayDao)
> org.my.app.repository.SantosRepository(…, todayDao)
> org.my.app.repository.SyncRepository(…, todayDao, …) The following
> other entry points also depend on it:
> dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap()
> [org.my.app.BaseApplication_HiltComponents.SingletonC →
> org.my.app.BaseApplication_HiltComponents.ActivityRetainedC →
> org.my.app.BaseApplication_HiltComponents.ViewModelC]

I can&#39t find what is wrong in my code.

This is the original code in Java:

@Module
@InstallIn(SingletonComponent.class)
public abstract class NetworkModule {

    @Provides
    @Singleton
    static Retrofit provideRetrofit(GsonConverterFactory gsonConverterFactory,
                                    RxJava3CallAdapterFactory rxJava3CallAdapterFactory,
                                    OkHttpClient okHttpClient
    ) {


        return new Retrofit.Builder().baseUrl(URL_API)
                .addConverterFactory(gsonConverterFactory)
                .addCallAdapterFactory(rxJava3CallAdapterFactory)
                .client(okHttpClient)
                .build();
    }

    @Provides
    @Singleton
    static Context provideContext(Application application) {
        return application;
    }

    @Provides
    @Singleton
    static OkHttpClient provideHttpClient(Application application) {
        Dispatcher dispatcher = new Dispatcher();
        // HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .dispatcher(dispatcher);

        client.addInterceptor(new ConnectivityIntecepter(application));
        Interceptor interceptor = chain -&gt; {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        };
        client.addInterceptor(interceptor);
        // client.interceptors().add(logging);
        return client.build();
    }

    @Provides
    @Singleton
    static Gson provideGson() {
        return new GsonBuilder().setLenient().create();
    }

    @Provides
    @Singleton
    static GsonConverterFactory providesGsonConverterFactory() {
        return GsonConverterFactory.create();
    }

    @Provides
    @Singleton
    static RxJava3CallAdapterFactory providesRxJavaCallAdapterFactory() {
        return RxJava3CallAdapterFactory.create();
    }

    @Provides
    @Singleton
    static ApiService provideService(Retrofit retrofit) {
        return retrofit.create(ApiService.class);
    }
}

I have used the Android Studio conversion tool and it should be noted that some of the classes in which the dependencies are injected are still written in Java, I don&#39t know if this could be the cause of the problem. I am partially migrating the files and fixing minor bugs, but here I can&#39t get it to work.

英文:

I am trying to migrate my Java code to Kotlin.

I have problems with a Hilt module:

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
@Singleton
fun provideRetrofit(
gsonConverterFactory: GsonConverterFactory?,
rxJava3CallAdapterFactory: RxJava3CallAdapterFactory?,
okHttpClient: OkHttpClient?
): Retrofit {
return Retrofit.Builder().baseUrl(Configuration.URL_API)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava3CallAdapterFactory)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
fun provideContext(application: Application): Context {
return application
}
@Provides
@Singleton
fun provideHttpClient(application: Application?): OkHttpClient {
val dispatcher = Dispatcher()
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
val client =OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.dispatcher(dispatcher)
client.addInterceptor(ConnectivityIntecepter(application))
val interceptor = Interceptor { chain: Interceptor.Chain -&gt;
val original = chain.request()
val request = original.newBuilder()
.method(original.method, original.body)
.build()
chain.proceed(request)
}
client.addInterceptor(interceptor)
//client.interceptors().add(logging);
return client.build()
}
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder().setLenient().create()
}
@Provides
@Singleton
fun providesGsonConverterFactory(): GsonConverterFactory {
return GsonConverterFactory.create()
}
@Provides
@Singleton
fun providesRxJavaCallAdapterFactory(): RxJava3CallAdapterFactory {
return RxJava3CallAdapterFactory.create()
}
@Provides
@Singleton
fun provideServicee(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}
}

When i try to compile i get the following error:

> .../app/build/generated/hilt/component_sources/debug/org/my/app/BaseApplication_HiltComponents.java:153:
> error: [Dagger/MissingBinding] org.my.app.data.db.dao.TodayDao cannot
> be provided without an @Provides-annotated method. public abstract
> static class SingletonC implements
> FragmentGetContextFix.FragmentGetContextFixEntryPoint,
> ^ org.my.app.data.db.dao.TodayDao is injected at
> org.my.app.workers.TodayWorker.mTodayDao org.my.app.workers.TodayWorker_AssistedFactory is injected at
> org.my.app.workers.TodayWorker_HiltModule.bind(factory) java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.work.WorkerAssistedFactory<?
> extends androidx.work.ListenableWorker>>> is injected at
> androidx.hilt.work.WorkerFactoryModule.provideFactory(workerFactories)
> androidx.hilt.work.HiltWorkerFactory is injected at
> org.my.app.BaseApplication.workerFactory org.my.app.BaseApplication is injected at
> org.my.app.BaseApplication_GeneratedInjector.injectBaseApplication(org.my.app.BaseApplication)
> It is also requested at:
> org.my.app.repository.BreviarioRepository(…, todayDao, …)
> org.my.app.repository.ComentariosRepository(…, todayDao)
> org.my.app.repository.HomiliasRepository(…, todayDao)
> org.my.app.repository.LecturasRepository(…, mTodayDao)
> org.my.app.repository.SantosRepository(…, todayDao)
> org.my.app.repository.SyncRepository(…, todayDao, …) The following
> other entry points also depend on it:
> dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap()
> [org.my.app.BaseApplication_HiltComponents.SingletonC →
> org.my.app.BaseApplication_HiltComponents.ActivityRetainedC →
> org.my.app.BaseApplication_HiltComponents.ViewModelC]

I can't find what is wrong in my code.

This is the original code in Java:

@Module
@InstallIn(SingletonComponent.class)
public abstract class NetworkModule {
@Provides
@Singleton
static Retrofit provideRetrofit(GsonConverterFactory gsonConverterFactory,
RxJava3CallAdapterFactory rxJava3CallAdapterFactory,
OkHttpClient okHttpClient
) {
return new Retrofit.Builder().baseUrl(URL_API)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava3CallAdapterFactory)
.client(okHttpClient)
.build();
}
@Provides
@Singleton
static Context provideContext(Application application) {
return application;
}
@Provides
@Singleton
static OkHttpClient provideHttpClient(Application application) {
Dispatcher dispatcher = new Dispatcher();
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.dispatcher(dispatcher);
client.addInterceptor(new ConnectivityIntecepter(application));
Interceptor interceptor = chain -&gt; {
Request original = chain.request();
Request request = original.newBuilder()
.method(original.method(), original.body())
.build();
return chain.proceed(request);
};
client.addInterceptor(interceptor);
//client.interceptors().add(logging);
return client.build();
}
@Provides
@Singleton
static Gson provideGson() {
return new GsonBuilder().setLenient().create();
}
@Provides
@Singleton
static GsonConverterFactory providesGsonConverterFactory() {
return GsonConverterFactory.create();
}
@Provides
@Singleton
static RxJava3CallAdapterFactory providesRxJavaCallAdapterFactory() {
return RxJava3CallAdapterFactory.create();
}
@Provides
@Singleton
static ApiService provideService(Retrofit retrofit) {
return retrofit.create(ApiService.class);
}
}

I have used the Android Studio conversion tool and it should be noted that some of the classes in which the dependencies are injected are still written in Java, I don't know if this could be the cause of the problem. I am partially migrating the files and fixing minor bugs, but here I can't get it to work.

答案1

得分: 1

请确保在将带注解的Java项目转换为Kotlin时,将 apply plugin: 'kotlin-kapt' 添加到您的模块 build.gradle 文件中。这将允许Kotlin处理这些注解。

英文:

Make sure to add apply plugin: &#39;kotlin-kapt&#39; to your module build.gradle file when converting a java project with annotations to kotlin. This will allow kotlin to process them.

huangapple
  • 本文由 发表于 2023年4月13日 23:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定