英文:
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'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 -> {
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.
英文:
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 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 -> {
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: 'kotlin-kapt'
to your module build.gradle file when converting a java project with annotations to kotlin. This will allow kotlin to process them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论